1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
14 var DefaultBlockCache = &BlockCache{}
16 type BlockCache struct {
17 // Maximum number of blocks to keep in the cache. If 0, a
18 // default size (currently 4) is used instead.
21 cache map[string]*cacheBlock
26 const defaultMaxBlocks = 4
28 // Sweep deletes the least recently used blocks from the cache until
29 // there are no more than MaxBlocks left.
30 func (c *BlockCache) Sweep() {
32 if max < defaultMaxBlocks {
33 max = defaultMaxBlocks
37 if len(c.cache) <= max {
40 lru := make([]time.Time, 0, len(c.cache))
41 for _, b := range c.cache {
42 lru = append(lru, b.lastUse)
44 sort.Sort(sort.Reverse(timeSlice(lru)))
46 for loc, b := range c.cache {
47 if !b.lastUse.After(threshold) {
53 // Get returns data from the cache, first retrieving it from Keep if
55 func (c *BlockCache) Get(kc *KeepClient, locator string) ([]byte, error) {
56 c.setupOnce.Do(c.setup)
57 cacheKey := locator[:32]
59 b, ok := c.cache[cacheKey]
60 if !ok || b.err != nil {
62 fetched: make(chan struct{}),
67 rdr, _, _, err := kc.Get(locator)
70 data, err = ioutil.ReadAll(rdr)
77 b.data, b.err = data, err
85 // Wait (with mtx unlocked) for the fetch goroutine to finish,
86 // in case it hasn't already.
90 b.lastUse = time.Now()
95 func (c *BlockCache) setup() {
96 c.cache = make(map[string]*cacheBlock)
99 type timeSlice []time.Time
101 func (ts timeSlice) Len() int { return len(ts) }
103 func (ts timeSlice) Less(i, j int) bool { return ts[i].Before(ts[j]) }
105 func (ts timeSlice) Swap(i, j int) { ts[i], ts[j] = ts[j], ts[i] }
107 type cacheBlock struct {
110 fetched chan struct{}