OSDN Git Service

add thread safe lru (#179)
authormuscle_boy <shenao.78@163.com>
Fri, 14 Jun 2019 07:37:06 +0000 (15:37 +0800)
committerPaladz <yzhu101@uottawa.ca>
Fri, 14 Jun 2019 07:37:06 +0000 (15:37 +0800)
common/concurrent_lru.go [new file with mode: 0644]
protocol/protocol.go

diff --git a/common/concurrent_lru.go b/common/concurrent_lru.go
new file mode 100644 (file)
index 0000000..8e9a344
--- /dev/null
@@ -0,0 +1,62 @@
+package common
+
+import (
+       "sync"
+
+       "github.com/golang/groupcache/lru"
+)
+
+// Cache is an LRU cache. It is safe for concurrent access.
+type Cache struct {
+       cache  *lru.Cache
+       sync.RWMutex
+}
+
+// NewCache creates a new Cache.
+// If maxEntries is zero, the cache has no limit and it's assumed
+// that eviction is done by the caller.
+func NewCache(maxEntries int) *Cache {
+       return &Cache{cache: lru.New(maxEntries)}
+}
+
+// Add adds a value to the cache.
+func (c *Cache) Add(key, value interface{}) {
+       c.Lock()
+       defer c.Unlock()
+       c.cache.Add(key, value)
+}
+
+// Get looks up a key's value from the cache.
+func (c *Cache) Get(key interface{}) (value interface{}, ok bool) {
+       c.RLock()
+       defer c.RUnlock()
+       return c.cache.Get(key)
+}
+
+// Remove removes the provided key from the cache.
+func (c *Cache) Remove(key interface{}) {
+       c.Lock()
+       defer c.Unlock()
+       c.cache.Remove(key)
+}
+
+// RemoveOldest removes the oldest item from the cache.
+func (c *Cache) RemoveOldest() {
+       c.Lock()
+       defer c.Unlock()
+       c.cache.RemoveOldest()
+}
+
+// Len returns the number of items in the cache.
+func (c *Cache) Len() int {
+       c.RLock()
+       defer c.RUnlock()
+       return c.cache.Len()
+}
+
+// Clear purges all stored items from the cache.
+func (c *Cache) Clear() {
+       c.Lock()
+       defer c.Unlock()
+       c.cache.Clear()
+}
index ee3114e..98b285b 100644 (file)
@@ -3,7 +3,6 @@ package protocol
 import (
        "sync"
 
-       "github.com/golang/groupcache/lru"
        log "github.com/sirupsen/logrus"
 
        "github.com/vapor/config"
@@ -11,6 +10,7 @@ import (
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
        "github.com/vapor/protocol/state"
+       "github.com/vapor/common"
 )
 
 const maxProcessBlockChSize = 1024
@@ -24,7 +24,7 @@ type Chain struct {
        processBlockCh chan *processBlockMsg
 
        consensusNodeManager *consensusNodeManager
-       signatureCache       *lru.Cache
+       signatureCache       *common.Cache
        eventDispatcher      *event.Dispatcher
 
        cond                 sync.Cond
@@ -38,7 +38,7 @@ func NewChain(store Store, txPool *TxPool, eventDispatcher *event.Dispatcher) (*
                orphanManage:    NewOrphanManage(),
                txPool:          txPool,
                store:           store,
-               signatureCache:  lru.New(maxSignatureCacheSize),
+               signatureCache:  common.NewCache(maxSignatureCacheSize),
                eventDispatcher: eventDispatcher,
                processBlockCh:  make(chan *processBlockMsg, maxProcessBlockChSize),
        }