| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package dscache | 5 package dscache |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "crypto/sha1" | 9 "crypto/sha1" |
| 10 "encoding/base64" | 10 "encoding/base64" |
| 11 "fmt" | 11 "fmt" |
| 12 "time" | 12 "time" |
| 13 | 13 |
| 14 "github.com/luci/gae/service/datastore" | 14 "github.com/luci/gae/service/datastore" |
| 15 "github.com/luci/gae/service/datastore/serialize" |
| 15 ) | 16 ) |
| 16 | 17 |
| 17 var ( | 18 var ( |
| 18 // InstanceEnabledStatic allows you to statically (e.g. in an init() fun
ction) | 19 // InstanceEnabledStatic allows you to statically (e.g. in an init() fun
ction) |
| 19 // bypass this filter by setting it to false. This takes effect when the | 20 // bypass this filter by setting it to false. This takes effect when the |
| 20 // application calls IsGloballyEnabled. | 21 // application calls IsGloballyEnabled. |
| 21 InstanceEnabledStatic = true | 22 InstanceEnabledStatic = true |
| 22 | 23 |
| 23 // LockTimeSeconds is the number of seconds that a "lock" memcache entry
will | 24 // LockTimeSeconds is the number of seconds that a "lock" memcache entry
will |
| 24 // have its expiration set to. It's set to just over half of the fronten
d | 25 // have its expiration set to. It's set to just over half of the fronten
d |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 ItemUKNONWN FlagValue = iota | 99 ItemUKNONWN FlagValue = iota |
| 99 ItemHasData | 100 ItemHasData |
| 100 ItemHasLock | 101 ItemHasLock |
| 101 ) | 102 ) |
| 102 | 103 |
| 103 func MakeMemcacheKey(shard int, k datastore.Key) string { | 104 func MakeMemcacheKey(shard int, k datastore.Key) string { |
| 104 return fmt.Sprintf(KeyFormat, shard, HashKey(k)) | 105 return fmt.Sprintf(KeyFormat, shard, HashKey(k)) |
| 105 } | 106 } |
| 106 | 107 |
| 107 func HashKey(k datastore.Key) string { | 108 func HashKey(k datastore.Key) string { |
| 108 » // errs can't happen, since we're using a byte buffer. | 109 » dgst := sha1.Sum(serialize.ToBytes(k)) |
| 109 buf := bytes.Buffer{} | 110 buf := bytes.Buffer{} |
| 110 _ = datastore.WriteKey(&buf, datastore.WithoutContext, k) | |
| 111 dgst := sha1.Sum(buf.Bytes()) | |
| 112 buf.Reset() | |
| 113 enc := base64.NewEncoder(base64.StdEncoding, &buf) | 111 enc := base64.NewEncoder(base64.StdEncoding, &buf) |
| 114 _, _ = enc.Write(dgst[:]) | 112 _, _ = enc.Write(dgst[:]) |
| 115 enc.Close() | 113 enc.Close() |
| 116 return buf.String()[:buf.Len()-Sha1B64Padding] | 114 return buf.String()[:buf.Len()-Sha1B64Padding] |
| 117 } | 115 } |
| OLD | NEW |