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

Side by Side Diff: logdog/common/storage/caching/cache.go

Issue 2435113002: LogDog: Add Storage-layer data caching. (Closed)
Patch Set: Fix byteLimit bug. Created 4 years, 1 month 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
« no previous file with comments | « logdog/common/storage/bigtable/testing.go ('k') | logdog/common/storage/caching/doc.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "bufio"
9 "crypto/sha256"
10 "encoding/hex"
11 "time"
12
13 "golang.org/x/net/context"
14 )
15
16 // Item is a single cache item. An item is uniquely identified by its Schema and
17 // Key sequence.
18 type Item struct {
19 // Schema is the item's schema value. If empty, the item is schemaless.
20 Schema string
21
22 // Type is the item's type identifier. If empty, the item has no type.
23 Type string
24
25 // Key is the item's individual key. It uniquely identifies the Item wit hin
26 // the scope of the Schema and Type.
27 Key string
28
29 // Data is the Item's data. For Put, the contents of this Data will be u sed to
30 // populate the cache. For Get, this value will be non-nil and populated with
31 // the retrieved data, or nil if the item was not present in the cache.
32 Data []byte
33 }
34
35 // Cache is a simple cache interface. It is capable of storage and retrieval.
36 type Cache interface {
37 // Put caches the supplied Items into the cache. If an Item already exis ts in
38 // the cache, it will be overridden.
39 //
40 // If exp, the supplied expiration Duration, is >0, the cache should onl y
41 // store the data if it can expire it after this period of time.
42 //
43 // This method does not return whether or not the caching storage was
44 // successful.
45 Put(c context.Context, exp time.Duration, items ...*Item)
46
47 // Get retrieves a cached entry. If the entry was present, a non-nil val ue
48 // will be returned, even if the data length is zero. If the entry was n ot
49 // present, nil will be returned.
50 //
51 // This method does not distinguish between an error and missing data. E ither
52 // valid data is returned, or it is not.
53 Get(c context.Context, items ...*Item)
54 }
55
56 // HashKey composes a hex-encoded SHA256 string from the supplied parts. The
57 // local schema version and KeyType are included in the hash.
58 func HashKey(parts ...string) string {
59 hash := sha256.New()
60 bio := bufio.NewWriter(hash)
61
62 // Add a full NULL-delimited key sequence segment to our hash.
63 for i, part := range parts {
64 // Write the separator, except for the first key.
65 if i > 0 {
66 if _, err := bio.WriteRune('\x00'); err != nil {
67 panic(err)
68 }
69 }
70
71 if _, err := bio.WriteString(part); err != nil {
72 panic(err)
73 }
74 }
75 if err := bio.Flush(); err != nil {
76 panic(err)
77 }
78
79 // Return the hex-encoded hash sum.
80 return hex.EncodeToString(hash.Sum(nil))
81 }
OLDNEW
« no previous file with comments | « logdog/common/storage/bigtable/testing.go ('k') | logdog/common/storage/caching/doc.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698