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
25 const defaultMaxBlocks = 4
27 // Sweep deletes the least recently used blocks from the cache until
28 // there are no more than MaxBlocks left.
29 func (c *BlockCache) Sweep() {
32 max = defaultMaxBlocks
36 if len(c.cache) <= max {
39 lru := make([]time.Time, 0, len(c.cache))
40 for _, b := range c.cache {
41 lru = append(lru, b.lastUse)
43 sort.Sort(sort.Reverse(timeSlice(lru)))
45 for loc, b := range c.cache {
46 if !b.lastUse.After(threshold) {
52 // ReadAt returns data from the cache, first retrieving it from Keep if
54 func (c *BlockCache) ReadAt(kc *KeepClient, locator string, p []byte, off int) (int, error) {
55 buf, err := c.Get(kc, locator)
60 return 0, io.ErrUnexpectedEOF
62 return copy(p, buf[off:]), nil
65 // Get returns data from the cache, first retrieving it from Keep if
67 func (c *BlockCache) Get(kc *KeepClient, locator string) ([]byte, error) {
68 cacheKey := locator[:32]
71 c.cache = make(map[string]*cacheBlock)
73 b, ok := c.cache[cacheKey]
74 if !ok || b.err != nil {
76 fetched: make(chan struct{}),
81 rdr, size, _, err := kc.Get(locator)
84 data = make([]byte, size, BLOCKSIZE)
85 _, err = io.ReadFull(rdr, data)
92 b.data, b.err = data, err
100 // Wait (with mtx unlocked) for the fetch goroutine to finish,
101 // in case it hasn't already.
105 b.lastUse = time.Now()
110 func (c *BlockCache) Clear() {
116 type timeSlice []time.Time
118 func (ts timeSlice) Len() int { return len(ts) }
120 func (ts timeSlice) Less(i, j int) bool { return ts[i].Before(ts[j]) }
122 func (ts timeSlice) Swap(i, j int) { ts[i], ts[j] = ts[j], ts[i] }
124 type cacheBlock struct {
127 fetched chan struct{}