Skip to content

Commit

Permalink
Use go-cache for hashcache
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Sep 29, 2018
1 parent f6812c1 commit 89f0682
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 31 deletions.
50 changes: 50 additions & 0 deletions common/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package common

import (
"time"

gocache "github.com/patrickmn/go-cache"
)

// Cache is an anstract cache layer
type Cache interface {
Add([]byte, interface{}) error
Get([]byte) (interface{}, bool)
Set([]byte, interface{}) error
}

// GoCache is the caching layer implemented by go-cache.
type GoCache struct {
cache *gocache.Cache
}

// NewGoCache creates a go-cache cache with a given default expiration duration
// and cleanup interval.
func NewGoCache(defaultExpiration, cleanupInterval time.Duration) *GoCache {
return &GoCache{
cache: gocache.New(defaultExpiration, cleanupInterval),
}
}

func (gc *GoCache) byteKeyToStringKey(byteKey []byte) string {
return string(byteKey)
}

// Add adds an item to the cache only if an item doesn't already exist for the
// given key, or if the existing item has expired. Returns an error otherwise.
func (gc *GoCache) Add(key []byte, value interface{}) error {
return gc.cache.Add(gc.byteKeyToStringKey(key), value, 0)
}

// Get gets an item from the cache. Returns the item or nil, and a bool
// indicating whether the key was found.
func (gc *GoCache) Get(key []byte) (interface{}, bool) {
return gc.cache.Get(gc.byteKeyToStringKey(key))
}

// Set adds an item to the cache, replacing any existing item, using the default
// expiration.
func (gc *GoCache) Set(key []byte, value interface{}) error {
gc.cache.SetDefault(gc.byteKeyToStringKey(key), value)
return nil
}
6 changes: 4 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ import:
- package: github.com/jpillora/backoff
version: 1.0.0
- package: github.com/nknorg/go-portscanner
- package: github.com/patrickmn/go-cache
41 changes: 13 additions & 28 deletions net/node/hashcache.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,32 @@
package node

import (
"sync"
"time"

. "github.com/nknorg/nkn/common"
"github.com/nknorg/nkn/common"
)

const (
HashCacheCap = 1000
HashCacheExpiration = 60 * time.Second
HashCacheCleanupInterval = 1 * time.Second
)

type hashCache struct {
sync.RWMutex
cap int
list map[Uint256]struct{}
hashes []Uint256
cache common.Cache
}

func NewHashCache(cap int) *hashCache {
func NewHashCache() *hashCache {
return &hashCache{
cap: cap,
list: make(map[Uint256]struct{}),
cache: common.NewGoCache(HashCacheExpiration, HashCacheCleanupInterval),
}
}

func (hc *hashCache) addHash(hash Uint256) {
if len(hc.hashes) <= HashCacheCap {
hc.hashes = append(hc.hashes, hash)
} else {
delete(hc.list, hc.hashes[0])
hc.hashes = append(hc.hashes[1:], hash)
}
hc.list[hash] = struct{}{}
func (hc *hashCache) addHash(hash common.Uint256) {
hc.cache.Set(hash.ToArray(), true)
}

func (hc *hashCache) ExistHash(hash Uint256) bool {
hc.Lock()
defer hc.Unlock()

if _, ok := hc.list[hash]; ok {
return true
}
hc.addHash(hash)

return false
func (hc *hashCache) ExistHash(hash common.Uint256) bool {
_, ok := hc.cache.Get(hash.ToArray())
hc.cache.Set(hash.ToArray(), true)
return ok
}
2 changes: 1 addition & 1 deletion net/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func InitNode(pubKey *crypto.PubKey, ring *chord.Ring) Noder {
n.msgHandlerChan = MakeChanQueue(MaxMsgChanNum)
n.quit = make(chan bool, 1)
n.eventQueue.init()
n.hashCache = NewHashCache(HashCacheCap)
n.hashCache = NewHashCache()
n.nodeDisconnectSubscriber = n.eventQueue.GetEvent("disconnect").Subscribe(events.EventNodeDisconnect, n.NodeDisconnected)
n.ring = ring

Expand Down

0 comments on commit 89f0682

Please sign in to comment.