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

Side by Side Diff: logdog/common/storage/bigtable/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
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 bigtable
6
7 import (
8 "bytes"
9 "encoding/binary"
10 "time"
11
12 "github.com/luci/luci-go/common/config"
13 log "github.com/luci/luci-go/common/logging"
14 "github.com/luci/luci-go/logdog/common/storage/caching"
15 "github.com/luci/luci-go/logdog/common/types"
16
17 "golang.org/x/net/context"
18 )
19
20 // cacheSchema represents the cache schema used by this version of the tail
21 // cache. If the underlying data format changes, this value must also be
22 // updated.
23 const cacheSchema = "v1"
24
25 // lastTailIndexCacheDuration is the amount of time that the last tail index
26 // should be cached.
27 const lastTailIndexCacheDuration = 1 * time.Hour
28
29 // getLastTailIndex will return the cached last tail index of a given stream.
30 //
31 // If there was an error, or if the item was not cached, 0 (first index) will be
32 // returned.
33 func getLastTailIndex(c context.Context, cache caching.Cache, project config.Pro jectName, path types.StreamPath) int64 {
34 itm := mkLastTailItem(project, path)
35 cache.Get(c, itm)
36 if itm.Data == nil {
37 return 0
38 }
39
40 v, err := binary.ReadVarint(bytes.NewReader(itm.Data))
41 if err != nil {
42 log.Fields{
43 log.ErrorKey: err,
44 "project": project,
45 "path": path,
46 }.Warningf(c, "Could not decode last tail cache.")
47 return 0
48 }
49
50 return v
51 }
52
53 func putLastTailIndex(c context.Context, cache caching.Cache, project config.Pro jectName, path types.StreamPath, v int64) {
54 buf := make([]byte, binary.MaxVarintLen64)
55 buf = buf[:binary.PutVarint(buf, v)]
56
57 itm := mkLastTailItem(project, path)
58 itm.Data = buf
59 cache.Put(c, lastTailIndexCacheDuration, itm)
60 }
61
62 func mkLastTailItem(project config.ProjectName, path types.StreamPath) *caching. Item {
63 return &caching.Item{
64 Schema: cacheSchema,
65 Type: "bt_tail_idx",
66 Key: caching.HashKey(string(project), string(path)),
67 }
68 }
OLDNEW
« no previous file with comments | « logdog/common/storage/bigtable/bigtable_test.go ('k') | logdog/common/storage/bigtable/initialize.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698