Chromium Code Reviews| Index: server/config/caching/proccache.go |
| diff --git a/server/config/caching/proccache.go b/server/config/caching/proccache.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4c2c2852660fff2e1ec6761d0c36b8d77d8e6942 |
| --- /dev/null |
| +++ b/server/config/caching/proccache.go |
| @@ -0,0 +1,55 @@ |
| +// 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/server/config" |
| + |
| + "golang.org/x/net/context" |
| +) |
| + |
| +// ProcCache returns a config.Backend that caches results in proccache. |
|
iannucci
2017/01/07 20:53:17
results? resolves?
dnj
2017/01/10 03:29:17
Done.
|
| +// |
| +// This will only cache results for AsService calls; any other Authority will |
| +// pass through. |
| +func ProcCache(b config.Backend, exp time.Duration) config.Backend { |
| + return &Backend{ |
| + Backend: b, |
| + CacheGet: func(c context.Context, key Key, l Loader) (*Value, error) { |
| + if key.Authority != config.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.GetAllType), |
| + }, ":")) |
| +} |