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

Unified Diff: filter/dscache/context.go

Issue 1269113005: A transparent cache for datastore, backed by memcache. (Closed) Base URL: https://github.com/luci/gae.git@add_meta
Patch Set: some minor comments Created 5 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | filter/dscache/doc.go » ('j') | filter/dscache/doc.go » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: filter/dscache/context.go
diff --git a/filter/dscache/context.go b/filter/dscache/context.go
new file mode 100644
index 0000000000000000000000000000000000000000..0233b486af9ee8dd0c03841a7d38e2cd73e35d1c
--- /dev/null
+++ b/filter/dscache/context.go
@@ -0,0 +1,54 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package dscache
+
+import (
+ ds "github.com/luci/gae/service/datastore"
+ "github.com/luci/gae/service/info"
+ mc "github.com/luci/gae/service/memcache"
+ "github.com/luci/luci-go/common/mathrand"
+ "golang.org/x/net/context"
+)
+
+type key int
+
+var dsTxnCacheKey key
+
+// FilterRDS installs a caching RawDatastore filter in the context.
+//
+// shardsForKey is a user-controllable function which calculates the number of
+// shards to use for a certain datastore key. The provided key will always be
+// valid and complete.
+//
+// The # of shards returned may be between 1 and 256. Values above this range
+// will be clamped into that range. A return value of 0 means that NO cache
+// operations should be done for this key, regardless of the dscache.enable
+// setting.
+//
+// If shardsForKey is nil, the value of DefaultShards is used for all keys.
+func FilterRDS(c context.Context, shardsForKey func(ds.Key) int) context.Context {
+ if !IsGloballyEnabled(c) {
+ return c
+ }
+
+ return ds.AddRawFilters(c, func(c context.Context, ds ds.RawInterface) ds.RawInterface {
+ i := info.Get(c)
+
+ sc := &supportContext{
+ i.AppID(),
+ i.GetNamespace(),
+ c,
+ mc.Get(c),
+ mathrand.Get(c),
+ shardsForKey,
+ }
+
+ v := c.Value(dsTxnCacheKey)
+ if v == nil {
+ return &dsCache{ds, sc}
+ }
+ return &dsTxnCache{ds, v.(*dsTxnState), sc}
+ })
+}
« no previous file with comments | « no previous file | filter/dscache/doc.go » ('j') | filter/dscache/doc.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698