Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(209)

Unified Diff: go/util/caching.go

Issue 1401563003: Enable clear/purge for failed digests (Closed) Base URL: https://skia.googlesource.com/buildbot@master
Patch Set: Added unit test to FileDiffStore Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « go/redisutil/redisutil_test.go ('k') | go/util/caching_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: go/util/caching.go
diff --git a/go/util/caching.go b/go/util/caching.go
index da820115cbbff5a8583729d9dff4ec659360b9a7..72bdd230fad4ff1a75081074a7d0335a9d0ed4b0 100644
--- a/go/util/caching.go
+++ b/go/util/caching.go
@@ -22,6 +22,9 @@ type LRUCache interface {
// Remove removes a key value pair from the cache.
Remove(key interface{})
+
+ // Keys returns all the keys in the LRUCache.
+ Keys() []interface{}
}
// LRUCodec converts serializes/deserializes an instance of a type to/from
@@ -37,19 +40,41 @@ type LRUCodec interface {
type MemLRUCache struct {
cache *lru.Cache
+ keys map[string]bool
mutex sync.RWMutex
}
func NewMemLRUCache(maxEntries int) *MemLRUCache {
- return &MemLRUCache{
+ ret := &MemLRUCache{
cache: lru.New(maxEntries),
+ keys: map[string]bool{},
+ }
+
+ ret.cache.OnEvicted = func(key lru.Key, value interface{}) {
+ delete(ret.keys, getStringKey(key))
}
+
+ return ret
+}
+
+func getStringKey(key interface{}) string {
+ k := ""
+ switch key := key.(type) {
+ case string:
+ k = key
+ case []byte:
+ if key != nil {
+ k = string(key)
+ }
+ }
+ return k
}
func (m *MemLRUCache) Add(key, value interface{}) bool {
m.mutex.Lock()
defer m.mutex.Unlock()
m.cache.Add(key, value)
+ m.keys[getStringKey(key)] = true
return true
}
@@ -68,6 +93,18 @@ func (m *MemLRUCache) Len() int {
func (m *MemLRUCache) Remove(key interface{}) {
m.mutex.Lock()
defer m.mutex.Unlock()
+ m.cache.Remove(key)
+ delete(m.keys, getStringKey(key))
+}
+
+func (m *MemLRUCache) Keys() []interface{} {
+ m.mutex.RLock()
+ defer m.mutex.RUnlock()
+ ret := make([]interface{}, 0, len(m.keys))
+ for k := range m.keys {
+ ret = append(ret, k)
+ }
+ return ret
}
type jsonCodec struct {
« no previous file with comments | « go/redisutil/redisutil_test.go ('k') | go/util/caching_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698