diff --git a/build.pl b/build.pl index 166a32f93..95b8879ad 100755 --- a/build.pl +++ b/build.pl @@ -403,6 +403,7 @@ TARGET: lib/go/camli/blobserver/localdisk TARGET: lib/go/camli/client TARGET: lib/go/camli/httputil TARGET: lib/go/camli/jsonsign +TARGET: lib/go/camli/lru TARGET: lib/go/camli/magic TARGET: lib/go/camli/misc/httprange TARGET: lib/go/camli/mysqlindexer diff --git a/clients/go/cammount/fs.go b/clients/go/cammount/fs.go index 9a90ed6a9..9e8956c5b 100644 --- a/clients/go/cammount/fs.go +++ b/clients/go/cammount/fs.go @@ -27,6 +27,7 @@ import ( "sync" "camli/blobref" + "camli/lru" "camli/schema" "camli/third_party/github.com/hanwen/go-fuse/fuse" ) @@ -40,8 +41,10 @@ type CamliFileSystem struct { fetcher blobref.Fetcher root *blobref.BlobRef + schemaBlob *lru.Cache + lk sync.Mutex - nameToBlob map[string]*blobref.BlobRef + nameToBlob map[string]*blobref.BlobRef // TODO: bound this? } func NewCamliFileSystem(fetcher blobref.Fetcher, root *blobref.BlobRef) *CamliFileSystem { diff --git a/lib/go/camli/lru/cache.go b/lib/go/camli/lru/cache.go new file mode 100644 index 000000000..31e7d8a66 --- /dev/null +++ b/lib/go/camli/lru/cache.go @@ -0,0 +1,48 @@ +/* +Copyright 2011 Google Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package lru + +import ( + "container/list" + "sync" +) + +type Cache struct { + maxEntries int + + lk sync.Mutex + ll *list.List + cache map[string]*list.Element +} + +func New(maxEntries int) *Cache { + return &Cache{ + maxEntries: maxEntries, + cache: make(map[string]*list.Element), + } +} + +func (c *Cache) Add(key string, value interface{}) { + c.lk.Lock() + defer c.lk.Unlock() + if ee, ok := c.cache[key]; ok { + c.ll.MoveToFront(ee) + ee.Value = value + return + } + // TODO: check size, add new element, etc +}