Rework antireplay

This commit is contained in:
9seconds
2019-10-07 17:51:21 +03:00
parent 413cafeeb6
commit d44474012a
3 changed files with 42 additions and 26 deletions
+9 -24
View File
@@ -1,31 +1,16 @@
package antireplay
import (
"github.com/allegro/bigcache"
import "github.com/allegro/bigcache"
"github.com/9seconds/mtg/config"
)
var cache *bigcache.BigCache
func Add(data []byte) {
cache.Set(string(data), nil) // nolint: errcheck
type cache struct {
cache *bigcache.BigCache
}
func Has(data []byte) bool {
_, err := cache.Get(string(data))
func (c *cache) Add(data []byte) {
c.cache.Set(string(data), nil) // nolint: errcheck
}
func (c *cache) Has(data []byte) bool {
_, err := c.cache.Get(string(data))
return err == nil
}
func Init() {
c, err := bigcache.NewBigCache(bigcache.Config{
Shards: 1024,
LifeWindow: config.C.AntiReplay.EvictionTime,
Hasher: hasher{},
HardMaxCacheSize: config.C.AntiReplay.MaxSize,
})
if err != nil {
panic(err)
}
cache = c
}