X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/a8d59274b32098abcc6b36b46ae354de3dcd13ad..4412d92e672d4df12ffc04c2b747b750e313cae9:/sdk/go/keepclient/block_cache.go diff --git a/sdk/go/keepclient/block_cache.go b/sdk/go/keepclient/block_cache.go index ca19f41bdc..e841a00fa1 100644 --- a/sdk/go/keepclient/block_cache.go +++ b/sdk/go/keepclient/block_cache.go @@ -18,9 +18,8 @@ type BlockCache struct { // default size (currently 4) is used instead. MaxBlocks int - cache map[string]*cacheBlock - mtx sync.Mutex - setupOnce sync.Once + cache map[string]*cacheBlock + mtx sync.Mutex } const defaultMaxBlocks = 4 @@ -53,9 +52,11 @@ func (c *BlockCache) Sweep() { // Get returns data from the cache, first retrieving it from Keep if // necessary. func (c *BlockCache) Get(kc *KeepClient, locator string) ([]byte, error) { - c.setupOnce.Do(c.setup) cacheKey := locator[:32] c.mtx.Lock() + if c.cache == nil { + c.cache = make(map[string]*cacheBlock) + } b, ok := c.cache[cacheKey] if !ok || b.err != nil { b = &cacheBlock{ @@ -67,7 +68,7 @@ func (c *BlockCache) Get(kc *KeepClient, locator string) ([]byte, error) { rdr, size, _, err := kc.Get(locator) var data []byte if err == nil { - data := make([]byte, size, BLOCKSIZE) + data = make([]byte, size, BLOCKSIZE) _, err = io.ReadFull(rdr, data) err2 := rdr.Close() if err == nil { @@ -75,7 +76,7 @@ func (c *BlockCache) Get(kc *KeepClient, locator string) ([]byte, error) { } } c.mtx.Lock() - b.data, b.err = data.Bytes(), err + b.data, b.err = data, err c.mtx.Unlock() close(b.fetched) go c.Sweep() @@ -93,13 +94,9 @@ func (c *BlockCache) Get(kc *KeepClient, locator string) ([]byte, error) { return b.data, b.err } -func (c *BlockCache) setup() { - c.cache = make(map[string]*cacheBlock) -} - func (c *BlockCache) Clear() { c.mtx.Lock() - c.setup() + c.cache = nil c.mtx.Unlock() }