github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/trie/cache.go (about) 1 package trie 2 3 type Backend interface { 4 Get([]byte) ([]byte, error) 5 Put([]byte, []byte) 6 } 7 8 type Cache struct { 9 store map[string][]byte 10 backend Backend 11 } 12 13 func NewCache(backend Backend) *Cache { 14 return &Cache{make(map[string][]byte), backend} 15 } 16 17 func (self *Cache) Get(key []byte) []byte { 18 data := self.store[string(key)] 19 if data == nil { 20 data, _ = self.backend.Get(key) 21 } 22 23 return data 24 } 25 26 func (self *Cache) Put(key []byte, data []byte) { 27 self.store[string(key)] = data 28 } 29 30 func (self *Cache) Flush() { 31 for k, v := range self.store { 32 self.backend.Put([]byte(k), v) 33 } 34 35 // This will eventually grow too large. We'd could 36 // do a make limit on storage and push out not-so-popular nodes. 37 //self.Reset() 38 } 39 40 func (self *Cache) Copy() *Cache { 41 cache := NewCache(self.backend) 42 for k, v := range self.store { 43 cache.store[k] = v 44 } 45 return cache 46 } 47 48 func (self *Cache) Reset() { 49 //self.store = make(map[string][]byte) 50 }