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

Side by Side Diff: logdog/common/storage/memory/cache.go

Issue 2435113002: LogDog: Add Storage-layer data caching. (Closed)
Patch Set: Fix byteLimit bug. Created 4 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « logdog/common/storage/caching/doc.go ('k') | logdog/common/storage/memory/memory.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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} }
OLDNEW
« no previous file with comments | « logdog/common/storage/caching/doc.go ('k') | logdog/common/storage/memory/memory.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698