Start of simple camli/lru class for fuse caches.

This commit is contained in:
Brad Fitzpatrick 2011-03-25 18:58:14 -07:00
parent cac9d9ff86
commit 7c1e08c55f
3 changed files with 53 additions and 1 deletions

View File

@ -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

View File

@ -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 {

48
lib/go/camli/lru/cache.go Normal file
View File

@ -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
}