redis/cache.go

48 lines
1.1 KiB
Go

package redis
import (
"github.com/dgraph-io/ristretto"
)
// Cache structure
type Cache struct {
cache *ristretto.Cache
}
// NewCache creates a new Cache instance with the given configuration
func NewCache(numKeys int64, memSize int64) (*Cache, error) {
// Create a new cache with the given configuration
config := &ristretto.Config{
NumCounters: numKeys * 10, // number of keys to track frequency of (10x number of items to cache)
MaxCost: memSize, // maximum cost of cache (in bytes)
BufferItems: 64, // number of keys per Get buffer
}
cache, err := ristretto.NewCache(config)
if err != nil {
return nil, err
}
return &Cache{cache: cache}, nil
}
// Set adds a value to the cache
func (c *Cache) Set(key, value interface{}, cost int64) bool {
return c.cache.Set(key, value, cost)
}
// Get retrieves a value from the cache
func (c *Cache) Get(key interface{}) (interface{}, bool) {
return c.cache.Get(key)
}
// ClearKey clears a specific key from the cache
func (c *Cache) ClearKey(key interface{}) {
c.cache.Del(key)
}
// Clear clears the entire cache
func (c *Cache) Clear() {
c.cache.Clear()
}