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

Side by Side Diff: filter/dscache/dscache.go

Issue 1269113005: A transparent cache for datastore, backed by memcache. (Closed) Base URL: https://github.com/luci/gae.git@add_meta
Patch Set: add test for per-model expiration 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package dscache
6
7 import (
8 "bytes"
9 "crypto/sha1"
10 "encoding/base64"
11 "fmt"
12
13 ds "github.com/luci/gae/service/datastore"
14 )
15
16 var (
17 // LockTimeSeconds is the number of seconds that a "lock" memcache entry will
18 // have its expiration set to. It's set to just over half of the fronten d
19 // request handler timeout (currently 60 seconds).
20 LockTimeSeconds = 31
21
22 // CacheTimeSeconds is the default number of seconds that a positively c ached
23 // entity will be retained (memcache contention notwithstanding). A valu e of
24 // 0 is infinite.
25 CacheTimeSeconds int64 = 0
Vadim Sh. 2015/08/06 01:23:34 I vote for "1 day" or something. To keep memcache
iannucci 2015/08/06 02:37:33 Sure. Done.
26
27 // CompressionThreshold is the number of bytes of entity value after whi ch
28 // compression kicks in.
29 CompressionThreshold = 860
30
31 // DefaultShards is the default number of key sharding to do.
32 DefaultShards int = 1
33
34 // DefaultEnable indicates whether or not caching is globally enabled or
35 // disabled by default. Can still be overridden by CacheEnableMeta.
36 DefaultEnabled = true
37 )
38
39 const (
40 MemcacheVersion = "1"
dnj 2015/08/05 18:32:18 Do all of these need to be exported?
iannucci 2015/08/06 01:54:01 no :D but I like them to be :P
41
42 // KeyFormat is the format string used to generate memcache keys. It's
43 // gae:<version>:<shard#>:<base64_std_nopad(sha1(datastore.Key))>
44 KeyFormat = "gae:" + MemcacheVersion + ":%x:%s"
45 Sha1B64Padding = 1
46 Sha1B64Size = 28 - Sha1B64Padding
47
48 MaxShards = 256
49 MaxShardsLen = len("ff")
50 InternalGAEPadding = 96
51 ValueSizeLimit = (1000 * 1000) - InternalGAEPadding - MaxShardsLen
52
53 CacheEnableMeta = "dscache.enable"
54 CacheExpirationMeta = "dscache.expiration"
55
56 // NonceUint32s is the number of 32 bit uints to use in the 'lock' nonce .
57 NonceUint32s = 2
58 )
59
60 // internalValueSizeLimit is a var for testing purposes.
61 var internalValueSizeLimit = ValueSizeLimit
62
63 type CompressionType byte
64
65 const (
66 NoCompression CompressionType = iota
67 ZlibCompression
68 )
69
70 func (c CompressionType) String() string {
71 switch c {
72 case NoCompression:
73 return "NoCompression"
74 case ZlibCompression:
75 return "ZlibCompression"
76 default:
77 return fmt.Sprintf("UNKNOWN_CompressionType(%d)", c)
78 }
79 }
80
81 // FlagValue is used to indicate if a memcache entry currently contains an
82 // item or a lock.
83 type FlagValue uint32
84
85 const (
86 ItemUKNONWN FlagValue = iota
87 ItemHasData
88 ItemHasLock
89 )
90
91 func MakeMemcacheKey(shard int, k ds.Key) string {
92 return fmt.Sprintf(KeyFormat, shard, HashKey(k))
93 }
94
95 func HashKey(k ds.Key) string {
96 // errs can't happen, since we're using a byte buffer.
97 buf := bytes.Buffer{}
98 _ = ds.WriteKey(&buf, ds.WithoutContext, k)
99 dgst := sha1.Sum(buf.Bytes())
100 buf.Reset()
101 enc := base64.NewEncoder(base64.StdEncoding, &buf)
102 _, _ = enc.Write(dgst[:])
103 enc.Close()
104 return buf.String()[:buf.Len()-Sha1B64Padding]
105 }
106
107 // TODO(riannucci): Should there be a way to purge the cache entries for a range
108 // of datastore keys?
Vadim Sh. 2015/08/06 01:23:34 why?
iannucci 2015/08/06 02:37:33 Not sure... that's why a todo question :D The tho
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698