-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
69 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters