| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package caching |
| 6 |
| 7 import ( |
| 8 "strings" |
| 9 "time" |
| 10 |
| 11 "github.com/luci/luci-go/common/data/caching/proccache" |
| 12 "github.com/luci/luci-go/luci_config/server/cfgclient/backend" |
| 13 |
| 14 "golang.org/x/net/context" |
| 15 ) |
| 16 |
| 17 // ProcCache returns a backend.B that caches configuration service results in an |
| 18 // in-memory proccache instance. |
| 19 // |
| 20 // This will only cache results for AsService calls; any other Authority will |
| 21 // pass through. |
| 22 func ProcCache(b backend.B, exp time.Duration) backend.B { |
| 23 return &Backend{ |
| 24 B: b, |
| 25 CacheGet: func(c context.Context, key Key, l Loader) (*Value, er
ror) { |
| 26 if key.Authority != backend.AsService { |
| 27 return l(c, key, nil) |
| 28 } |
| 29 |
| 30 k := mkProcCacheKey(&key) |
| 31 ret, err := proccache.GetOrMake(c, k, func() (interface{
}, time.Duration, error) { |
| 32 v, err := l(c, key, nil) |
| 33 if err != nil { |
| 34 return nil, 0, err |
| 35 } |
| 36 return v, exp, nil |
| 37 }) |
| 38 if err != nil { |
| 39 return nil, err |
| 40 } |
| 41 return ret.(*Value), nil |
| 42 }, |
| 43 } |
| 44 } |
| 45 |
| 46 type procCacheKey string |
| 47 |
| 48 func mkProcCacheKey(key *Key) procCacheKey { |
| 49 return procCacheKey(strings.Join([]string{ |
| 50 key.Schema, |
| 51 string(key.Op), |
| 52 key.ConfigSet, |
| 53 key.Path, |
| 54 string(key.GetAllTarget), |
| 55 }, ":")) |
| 56 } |
| OLD | NEW |