OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package dscache | 5 package dscache |
6 | 6 |
7 import ( | 7 import ( |
8 ds "github.com/luci/gae/service/datastore" | 8 ds "github.com/luci/gae/service/datastore" |
9 "github.com/luci/gae/service/info" | 9 "github.com/luci/gae/service/info" |
10 mc "github.com/luci/gae/service/memcache" | 10 mc "github.com/luci/gae/service/memcache" |
11 "github.com/luci/luci-go/common/mathrand" | 11 "github.com/luci/luci-go/common/mathrand" |
12 "golang.org/x/net/context" | 12 "golang.org/x/net/context" |
13 ) | 13 ) |
14 | 14 |
15 type key int | 15 type key int |
16 | 16 |
17 var dsTxnCacheKey key | 17 var dsTxnCacheKey key |
18 | 18 |
19 // FilterRDS installs a caching RawDatastore filter in the context. | 19 // FilterRDS installs a caching RawDatastore filter in the context. |
20 // | 20 // |
| 21 // It does nothing if IsGloballyEnabled returns false. That way it is possible |
| 22 // to disable the cache in runtime (e.g. in case memcache service is having |
| 23 // issues). |
| 24 // |
21 // shardsForKey is a user-controllable function which calculates the number of | 25 // shardsForKey is a user-controllable function which calculates the number of |
22 // shards to use for a certain datastore key. The provided key will always be | 26 // shards to use for a certain datastore key. The provided key will always be |
23 // valid and complete. | 27 // valid and complete. |
24 // | 28 // |
25 // The # of shards returned may be between 1 and 256. Values above this range | 29 // The # of shards returned may be between 1 and 256. Values above this range |
26 // will be clamped into that range. A return value of 0 means that NO cache | 30 // will be clamped into that range. A return value of 0 means that NO cache |
27 // operations should be done for this key, regardless of the dscache.enable | 31 // operations should be done for this key, regardless of the dscache.enable |
28 // setting. | 32 // setting. |
29 // | 33 // |
30 // If shardsForKey is nil, the value of DefaultShards is used for all keys. | 34 // If shardsForKey is nil, the value of DefaultShards is used for all keys. |
31 func FilterRDS(c context.Context, shardsForKey func(*ds.Key) int) context.Contex
t { | 35 func FilterRDS(c context.Context, shardsForKey func(*ds.Key) int) context.Contex
t { |
32 if !IsGloballyEnabled(c) { | 36 if !IsGloballyEnabled(c) { |
33 return c | 37 return c |
34 } | 38 } |
| 39 return AlwaysFilterRDS(c, shardsForKey) |
| 40 } |
35 | 41 |
| 42 // AlwaysFilterRDS installs a caching RawDatastore filter in the context. |
| 43 // |
| 44 // Unlike FilterRDS it doesn't check GlobalConfig via IsGloballyEnabled call, |
| 45 // assuming caller already knows whether filter should be applied or not. |
| 46 func AlwaysFilterRDS(c context.Context, shardsForKey func(*ds.Key) int) context.
Context { |
36 return ds.AddRawFilters(c, func(c context.Context, ds ds.RawInterface) d
s.RawInterface { | 47 return ds.AddRawFilters(c, func(c context.Context, ds ds.RawInterface) d
s.RawInterface { |
37 i := info.Get(c) | 48 i := info.Get(c) |
38 | 49 |
39 sc := &supportContext{ | 50 sc := &supportContext{ |
40 i.AppID(), | 51 i.AppID(), |
41 i.GetNamespace(), | 52 i.GetNamespace(), |
42 c, | 53 c, |
43 mc.Get(c), | 54 mc.Get(c), |
44 mathrand.Get(c), | 55 mathrand.Get(c), |
45 shardsForKey, | 56 shardsForKey, |
46 } | 57 } |
47 | 58 |
48 v := c.Value(dsTxnCacheKey) | 59 v := c.Value(dsTxnCacheKey) |
49 if v == nil { | 60 if v == nil { |
50 return &dsCache{ds, sc} | 61 return &dsCache{ds, sc} |
51 } | 62 } |
52 return &dsTxnCache{ds, v.(*dsTxnState), sc} | 63 return &dsTxnCache{ds, v.(*dsTxnState), sc} |
53 }) | 64 }) |
54 } | 65 } |
OLD | NEW |