mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 11:54:01 +03:00
Use ristretto instead of bigcache
This commit is contained in:
+21
-9
@@ -1,26 +1,38 @@
|
||||
package antireplay
|
||||
|
||||
import "github.com/allegro/bigcache"
|
||||
import "github.com/dgraph-io/ristretto"
|
||||
|
||||
var (
|
||||
prefixObfuscated2 = []byte{0x00}
|
||||
prefixTLS = []byte{0x01}
|
||||
)
|
||||
|
||||
type cache struct {
|
||||
obfuscated2 *bigcache.BigCache
|
||||
tls *bigcache.BigCache
|
||||
data *ristretto.Cache
|
||||
}
|
||||
|
||||
func (c *cache) AddObfuscated2(data []byte) {
|
||||
c.obfuscated2.Set(string(data), nil) // nolint: errcheck
|
||||
c.data.Set(keyObfuscated2(data), nil, int64(len(data)))
|
||||
}
|
||||
|
||||
func (c *cache) AddTLS(data []byte) {
|
||||
c.tls.Set(string(data), nil) // nolint: errcheck
|
||||
c.data.Set(keyTLS(data), nil, int64(len(data)))
|
||||
}
|
||||
|
||||
func (c *cache) HasObfuscated2(data []byte) bool {
|
||||
_, err := c.obfuscated2.Get(string(data))
|
||||
return err == nil
|
||||
_, ok := c.data.Get(keyObfuscated2(data))
|
||||
return ok
|
||||
}
|
||||
|
||||
func (c *cache) HasTLS(data []byte) bool {
|
||||
_, err := c.tls.Get(string(data))
|
||||
return err == nil
|
||||
_, ok := c.data.Get(keyTLS(data))
|
||||
return ok
|
||||
}
|
||||
|
||||
func keyObfuscated2(data []byte) string {
|
||||
return string(append(prefixObfuscated2, data...))
|
||||
}
|
||||
|
||||
func keyTLS(data []byte) string {
|
||||
return string(append(prefixTLS, data...))
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package antireplay
|
||||
|
||||
import "github.com/cespare/xxhash"
|
||||
|
||||
type hasher struct{}
|
||||
|
||||
func (h hasher) Sum64(value string) uint64 {
|
||||
return xxhash.Sum64String(value)
|
||||
}
|
||||
+12
-21
@@ -1,42 +1,33 @@
|
||||
package antireplay
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sync"
|
||||
|
||||
"github.com/9seconds/mtg/config"
|
||||
"github.com/allegro/bigcache"
|
||||
"github.com/dgraph-io/ristretto"
|
||||
)
|
||||
|
||||
var (
|
||||
Cache *cache
|
||||
Cache cache
|
||||
initOnce sync.Once
|
||||
)
|
||||
|
||||
func Init() {
|
||||
initOnce.Do(func() {
|
||||
c1, err := bigcache.NewBigCache(bigcache.Config{
|
||||
Shards: 1024,
|
||||
LifeWindow: config.C.AntiReplayEvictionTime,
|
||||
Hasher: hasher{},
|
||||
HardMaxCacheSize: config.C.AntiReplayMaxSize,
|
||||
cost := float64(config.C.AntiReplayMaxSize) / 32.0
|
||||
cost = math.Ceil(cost)
|
||||
|
||||
c, err := ristretto.NewCache(&ristretto.Config{
|
||||
NumCounters: int64(cost) * 10,
|
||||
MaxCost: config.C.AntiReplayMaxSize,
|
||||
BufferItems: 64,
|
||||
Metrics: false,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
c2, err := bigcache.NewBigCache(bigcache.Config{
|
||||
Shards: 1024,
|
||||
LifeWindow: config.C.AntiReplayEvictionTime,
|
||||
Hasher: hasher{},
|
||||
HardMaxCacheSize: config.C.AntiReplayMaxSize,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Cache = &cache{
|
||||
obfuscated2: c1,
|
||||
tls: c2,
|
||||
}
|
||||
Cache.data = c
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user