github.com/ndau/noms@v1.0.5/go/nbs/manifest_cache.go (about)

     1  // Copyright 2016 Attic Labs, Inc. All rights reserved.
     2  // Licensed under the Apache License, version 2.0:
     3  // http://www.apache.org/licenses/LICENSE-2.0
     4  
     5  package nbs
     6  
     7  import (
     8  	"container/list"
     9  	"time"
    10  
    11  	"sync"
    12  
    13  	"github.com/ndau/noms/go/d"
    14  )
    15  
    16  func newManifestCache(maxSize uint64) *manifestCache {
    17  	return &manifestCache{
    18  		maxSize: maxSize,
    19  		cache:   map[string]manifestCacheEntry{},
    20  		mu:      &sync.Mutex{},
    21  	}
    22  }
    23  
    24  type manifestCacheEntry struct {
    25  	lruEntry *list.Element
    26  	contents manifestContents
    27  	t        time.Time
    28  }
    29  
    30  type manifestCache struct {
    31  	totalSize uint64
    32  	maxSize   uint64
    33  	mu        *sync.Mutex
    34  	lru       list.List
    35  	cache     map[string]manifestCacheEntry
    36  }
    37  
    38  // Get() checks the searches the cache for an entry. If it exists, it moves it's
    39  // lru entry to the back of the queue and returns (value, true). Otherwise, it
    40  // returns (nil, false).
    41  func (mc *manifestCache) Get(db string) (contents manifestContents, t time.Time, present bool) {
    42  	mc.mu.Lock()
    43  	defer mc.mu.Unlock()
    44  
    45  	if entry, ok := mc.entry(db); ok {
    46  		contents, t, present = entry.contents, entry.t, true
    47  	}
    48  	return
    49  }
    50  
    51  // entry() checks if the value is in the cache. If not in the cache, it returns an
    52  // empty manifestCacheEntry and false. It it is in the cache, it moves it to
    53  // to the back of lru and returns the entry and true.
    54  func (mc *manifestCache) entry(key string) (manifestCacheEntry, bool) {
    55  	entry, ok := mc.cache[key]
    56  	if !ok {
    57  		return manifestCacheEntry{}, false
    58  	}
    59  	mc.lru.MoveToBack(entry.lruEntry)
    60  	return entry, true
    61  }
    62  
    63  // Put inserts |contents| into the cache with the key |db|, replacing any
    64  // currently cached value. Put() will add this element to the cache at the
    65  // back of the queue as long it's size does not exceed maxSize. If the
    66  // addition of this entry causes the size of the cache to exceed maxSize, the
    67  // necessary entries at the front of the queue will be deleted in order to
    68  // keep the total cache size below maxSize. |t| must be *prior* to initiating
    69  // the call which read/wrote |contents|.
    70  func (mc *manifestCache) Put(db string, contents manifestContents, t time.Time) {
    71  	mc.mu.Lock()
    72  	defer mc.mu.Unlock()
    73  
    74  	if entry, ok := mc.entry(db); ok {
    75  		mc.totalSize -= entry.contents.size()
    76  		mc.lru.Remove(entry.lruEntry)
    77  		delete(mc.cache, db)
    78  	}
    79  
    80  	if contents.size() <= mc.maxSize {
    81  		newEl := mc.lru.PushBack(db)
    82  		ce := manifestCacheEntry{lruEntry: newEl, contents: contents, t: t}
    83  		mc.cache[db] = ce
    84  		mc.totalSize += ce.contents.size()
    85  		for el := mc.lru.Front(); el != nil && mc.totalSize > mc.maxSize; {
    86  			key1 := el.Value.(string)
    87  			ce, ok := mc.cache[key1]
    88  			if !ok {
    89  				d.Panic("manifestCache is missing expected value")
    90  			}
    91  			next := el.Next()
    92  			delete(mc.cache, key1)
    93  			mc.totalSize -= ce.contents.size()
    94  			mc.lru.Remove(el)
    95  			el = next
    96  		}
    97  	}
    98  }