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

Side by Side Diff: luci_config/appengine/backend/memcache/cache.go

Issue 2575383002: Add server/cache support to gaeconfig. (Closed)
Patch Set: Un-collapse. Created 3 years, 11 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 memcache implements a caching config client backend backed by
6 // AppEngine's memcache service.
7 package memcache
8
9 import (
10 "encoding/hex"
11 "time"
12
13 "github.com/luci/luci-go/common/errors"
14 log "github.com/luci/luci-go/common/logging"
15 "github.com/luci/luci-go/luci_config/server/cfgclient/backend"
16 "github.com/luci/luci-go/luci_config/server/cfgclient/backend/caching"
17
18 mc "github.com/luci/gae/service/memcache"
19
20 "golang.org/x/net/context"
21 )
22
23 const (
24 memCacheSchema = "v1"
25 maxMemCacheSize = 1024 * 1024 // 1MB
26 )
27
28 // Backend wraps a backend.B instance with a memcache-backed caching layer whose
29 // entries expire after exp.
30 func Backend(b backend.B, exp time.Duration) backend.B {
31 return &caching.Backend{
32 B: b,
33 CacheGet: func(c context.Context, key caching.Key, l caching.Loa der) (*caching.Value, error) {
34 if key.Authority != backend.AsService {
35 return l(c, key, nil)
36 }
37
38 // Is the item already cached?
39 k := memcacheKey(&key)
40 mci, err := mc.GetKey(c, k)
41 switch err {
42 case nil:
43 // Value was cached, successfully retrieved.
44 v, err := caching.DecodeValue(mci.Value())
45 if err != nil {
46 return nil, errors.Annotate(err).Reason( "failed to decode cache value from %(key)q").
47 D("key", k).Err()
48 }
49 return v, nil
50
51 case mc.ErrCacheMiss:
52 // Value was not cached. Load from Loader and ca che.
53 v, err := l(c, key, nil)
54 if err != nil {
55 return nil, err
56 }
57
58 // Attempt to cache the value. If this fails, we 'll log a warning and
59 // move on.
60 err = func() error {
61 d, err := v.Encode()
62 if err != nil {
63 return errors.Annotate(err).Reas on("failed to encode value").Err()
64 }
65
66 if len(d) > maxMemCacheSize {
67 return errors.Reason("entry exce eds memcache size (%(size)d > %(max)d)").
68 D("size", len(d)).D("max ", maxMemCacheSize).Err()
69 }
70
71 item := mc.NewItem(c, k).SetValue(d).Set Expiration(exp)
72 if err := mc.Set(c, item); err != nil {
73 return errors.Annotate(err).Err( )
74 }
75 return nil
76 }()
77 if err != nil {
78 log.Fields{
79 log.ErrorKey: err,
80 "key": k,
81 }.Warningf(c, "Failed to cache config.")
82 }
83
84 // Return the loaded value.
85 return v, nil
86
87 default:
88 // Unknown memcache error.
89 log.Fields{
90 log.ErrorKey: err,
91 "key": k,
92 }.Warningf(c, "Failed to decode memcached config .")
93 return l(c, key, nil)
94 }
95 },
96 }
97 }
98
99 func memcacheKey(key *caching.Key) string { return hex.EncodeToString(key.ParamH ash()) }
OLDNEW
« no previous file with comments | « logdog/server/collector/coordinator/coordinator.go ('k') | luci_config/appengine/backend/memcache/cache_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698