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

Unified Diff: luci_config/server/cfgclient/backend/caching/proccache.go

Issue 2573403002: server/config: Generic caching backend. (Closed)
Patch Set: Updated interface, rebased 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 side-by-side diff with in-line comments
Download patch
Index: luci_config/server/cfgclient/backend/caching/proccache.go
diff --git a/luci_config/server/cfgclient/backend/caching/proccache.go b/luci_config/server/cfgclient/backend/caching/proccache.go
new file mode 100644
index 0000000000000000000000000000000000000000..1cd87973db8c1807c0c9b3e3802ed2729eefa8df
--- /dev/null
+++ b/luci_config/server/cfgclient/backend/caching/proccache.go
@@ -0,0 +1,56 @@
+// Copyright 2016 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package caching
+
+import (
+ "strings"
+ "time"
+
+ "github.com/luci/luci-go/common/data/caching/proccache"
+ "github.com/luci/luci-go/luci_config/server/cfgclient/backend"
+
+ "golang.org/x/net/context"
+)
+
+// ProcCache returns a backend.B that caches configuration service results in an
+// in-memory proccache instance.
+//
+// This will only cache results for AsService calls; any other Authority will
+// pass through.
+func ProcCache(b backend.B, exp time.Duration) backend.B {
+ return &Backend{
+ B: b,
+ CacheGet: func(c context.Context, key Key, l Loader) (*Value, error) {
+ if key.Authority != backend.AsService {
+ return l(c, key, nil)
+ }
+
+ k := mkProcCacheKey(&key)
+ ret, err := proccache.GetOrMake(c, k, func() (interface{}, time.Duration, error) {
+ v, err := l(c, key, nil)
+ if err != nil {
+ return nil, 0, err
+ }
+ return v, exp, nil
+ })
+ if err != nil {
+ return nil, err
+ }
+ return ret.(*Value), nil
+ },
+ }
+}
+
+type procCacheKey string
+
+func mkProcCacheKey(key *Key) procCacheKey {
+ return procCacheKey(strings.Join([]string{
+ key.Schema,
+ string(key.Op),
+ key.ConfigSet,
+ key.Path,
+ string(key.GetAllTarget),
+ }, ":"))
+}
« no previous file with comments | « luci_config/server/cfgclient/backend/caching/doc.go ('k') | luci_config/server/cfgclient/backend/caching/proccache_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698