| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Package memcache implements a caching config client backend backed by | 5 // Package memcache implements a caching config client backend backed by |
| 6 // AppEngine's memcache service. | 6 // AppEngine's memcache service. |
| 7 package memcache | 7 package memcache |
| 8 | 8 |
| 9 import ( | 9 import ( |
| 10 "encoding/hex" | 10 "encoding/hex" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 } | 36 } |
| 37 | 37 |
| 38 // Is the item already cached? | 38 // Is the item already cached? |
| 39 k := memcacheKey(&key) | 39 k := memcacheKey(&key) |
| 40 mci, err := mc.GetKey(c, k) | 40 mci, err := mc.GetKey(c, k) |
| 41 switch err { | 41 switch err { |
| 42 case nil: | 42 case nil: |
| 43 // Value was cached, successfully retrieved. | 43 // Value was cached, successfully retrieved. |
| 44 v, err := caching.DecodeValue(mci.Value()) | 44 v, err := caching.DecodeValue(mci.Value()) |
| 45 if err != nil { | 45 if err != nil { |
| 46 » » » » » return nil, errors.Annotate(err).Reason(
"failed to decode cache value from %(key)q"). | 46 » » » » » return nil, errors.Annotate(err, "failed
to decode cache value from %q", k).Err() |
| 47 » » » » » » D("key", k).Err() | |
| 48 } | 47 } |
| 49 return v, nil | 48 return v, nil |
| 50 | 49 |
| 51 case mc.ErrCacheMiss: | 50 case mc.ErrCacheMiss: |
| 52 // Value was not cached. Load from Loader and ca
che. | 51 // Value was not cached. Load from Loader and ca
che. |
| 53 v, err := l(c, key, nil) | 52 v, err := l(c, key, nil) |
| 54 if err != nil { | 53 if err != nil { |
| 55 return nil, err | 54 return nil, err |
| 56 } | 55 } |
| 57 | 56 |
| 58 // Attempt to cache the value. If this fails, we
'll log a warning and | 57 // Attempt to cache the value. If this fails, we
'll log a warning and |
| 59 // move on. | 58 // move on. |
| 60 err = func() error { | 59 err = func() error { |
| 61 d, err := v.Encode() | 60 d, err := v.Encode() |
| 62 if err != nil { | 61 if err != nil { |
| 63 » » » » » » return errors.Annotate(err).Reas
on("failed to encode value").Err() | 62 » » » » » » return errors.Annotate(err, "fai
led to encode value").Err() |
| 64 } | 63 } |
| 65 | 64 |
| 66 if len(d) > maxMemCacheSize { | 65 if len(d) > maxMemCacheSize { |
| 67 » » » » » » return errors.Reason("entry exce
eds memcache size (%(size)d > %(max)d)"). | 66 » » » » » » return errors.Reason("entry exce
eds memcache size (%d > %d)", len(d), maxMemCacheSize).Err() |
| 68 » » » » » » » D("size", len(d)).D("max
", maxMemCacheSize).Err() | |
| 69 } | 67 } |
| 70 | 68 |
| 71 item := mc.NewItem(c, k).SetValue(d).Set
Expiration(exp) | 69 item := mc.NewItem(c, k).SetValue(d).Set
Expiration(exp) |
| 72 if err := mc.Set(c, item); err != nil { | 70 if err := mc.Set(c, item); err != nil { |
| 73 » » » » » » return errors.Annotate(err).Err(
) | 71 » » » » » » return errors.Annotate(err, "").
Err() |
| 74 } | 72 } |
| 75 return nil | 73 return nil |
| 76 }() | 74 }() |
| 77 if err != nil { | 75 if err != nil { |
| 78 log.Fields{ | 76 log.Fields{ |
| 79 log.ErrorKey: err, | 77 log.ErrorKey: err, |
| 80 "key": k, | 78 "key": k, |
| 81 }.Warningf(c, "Failed to cache config.") | 79 }.Warningf(c, "Failed to cache config.") |
| 82 } | 80 } |
| 83 | 81 |
| 84 // Return the loaded value. | 82 // Return the loaded value. |
| 85 return v, nil | 83 return v, nil |
| 86 | 84 |
| 87 default: | 85 default: |
| 88 // Unknown memcache error. | 86 // Unknown memcache error. |
| 89 log.Fields{ | 87 log.Fields{ |
| 90 log.ErrorKey: err, | 88 log.ErrorKey: err, |
| 91 "key": k, | 89 "key": k, |
| 92 }.Warningf(c, "Failed to decode memcached config
.") | 90 }.Warningf(c, "Failed to decode memcached config
.") |
| 93 return l(c, key, nil) | 91 return l(c, key, nil) |
| 94 } | 92 } |
| 95 }, | 93 }, |
| 96 } | 94 } |
| 97 } | 95 } |
| 98 | 96 |
| 99 func memcacheKey(key *caching.Key) string { return hex.EncodeToString(key.ParamH
ash()) } | 97 func memcacheKey(key *caching.Key) string { return hex.EncodeToString(key.ParamH
ash()) } |
| OLD | NEW |