| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package memory |
| 6 |
| 7 import ( |
| 8 "sync" |
| 9 "time" |
| 10 |
| 11 "github.com/luci/luci-go/logdog/common/storage/caching" |
| 12 |
| 13 "golang.org/x/net/context" |
| 14 ) |
| 15 |
| 16 // Cache is an in-memory caching.Cache implementation. |
| 17 type Cache struct { |
| 18 mu sync.Mutex |
| 19 cacheMap map[cacheKey]caching.Item |
| 20 } |
| 21 |
| 22 var _ caching.Cache = (*Cache)(nil) |
| 23 |
| 24 type cacheKey struct { |
| 25 schema string |
| 26 typ string |
| 27 key string |
| 28 } |
| 29 |
| 30 // Put implements caching.Cache. |
| 31 func (c *Cache) Put(ctx context.Context, exp time.Duration, items ...*caching.It
em) { |
| 32 c.mu.Lock() |
| 33 defer c.mu.Unlock() |
| 34 |
| 35 if c.cacheMap == nil { |
| 36 c.cacheMap = make(map[cacheKey]caching.Item) |
| 37 } |
| 38 |
| 39 for _, itm := range items { |
| 40 c.cacheMap[c.keyForItem(itm)] = *itm |
| 41 } |
| 42 } |
| 43 |
| 44 // Get implements caching.Cache. |
| 45 func (c *Cache) Get(ctx context.Context, items ...*caching.Item) { |
| 46 c.mu.Lock() |
| 47 defer c.mu.Unlock() |
| 48 |
| 49 for _, itm := range items { |
| 50 if cacheItem, ok := c.cacheMap[c.keyForItem(itm)]; ok { |
| 51 itm.Data = append([]byte{}, cacheItem.Data...) |
| 52 } else { |
| 53 itm.Data = nil |
| 54 } |
| 55 } |
| 56 } |
| 57 |
| 58 // Delete deletes a set of keys from the cache. |
| 59 func (c *Cache) Delete(schema, typ string, key ...string) { |
| 60 c.mu.Lock() |
| 61 defer c.mu.Unlock() |
| 62 |
| 63 for _, k := range key { |
| 64 delete(c.cacheMap, cacheKey{schema, typ, k}) |
| 65 } |
| 66 } |
| 67 |
| 68 func (*Cache) keyForItem(itm *caching.Item) cacheKey { return cacheKey{itm.Schem
a, itm.Type, itm.Key} } |
| OLD | NEW |