2024-01-21 22:54:26 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/dgraph-io/ristretto"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Cache structure
|
|
|
|
type Cache struct {
|
|
|
|
cache *ristretto.Cache
|
|
|
|
}
|
|
|
|
|
2024-03-03 12:20:05 +03:00
|
|
|
type CacheConfig struct {
|
|
|
|
MaxSize int64 // maximum size of the cache in bytes
|
|
|
|
MaxKeys int64 // maximum number of keys to store in the cache
|
|
|
|
// other configuration options:
|
|
|
|
// - ttl (time to live) for cache entries
|
|
|
|
// - eviction policy
|
|
|
|
}
|
|
|
|
|
2024-01-21 22:54:26 +03:00
|
|
|
// 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
|
2024-03-03 12:20:05 +03:00
|
|
|
func (c *Cache) SetKey(key, value interface{}, cost int64) bool {
|
2024-01-21 22:54:26 +03:00
|
|
|
return c.cache.Set(key, value, cost)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves a value from the cache
|
2024-03-03 12:20:05 +03:00
|
|
|
func (c *Cache) GetKey(key interface{}) (interface{}, bool) {
|
2024-01-21 22:54:26 +03:00
|
|
|
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()
|
|
|
|
}
|