Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cache Structure #2873

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions cache.go
@@ -0,0 +1,47 @@
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()
}
95 changes: 95 additions & 0 deletions cache_test.go
@@ -0,0 +1,95 @@
package redis_test

import (
"log"
"testing"
"time"

"github.com/redis/go-redis/v9"
)

// Testredis.NewCache tests the creation of a new cache instance
func TestNewCache(t *testing.T) {
_, err := redis.NewCache(1000, 1<<20) // 1 MB size
if err != nil {
t.Fatalf("Failed to create cache: %v", err)
}
t.Log("Cache created successfully")
}

// TestCacheSetAndGet tests setting and getting values in the cache
func TestCacheSetAndGet(t *testing.T) {
cache, err := redis.NewCache(1000, 1<<20)
if err != nil {
t.Fatalf("Failed to create cache: %v", err)
}

key, value := "key1", "value1"
setSuccess := cache.Set(key, value, 1)
if !setSuccess {
t.Fatalf("Failed to set key: %s", key)
}
log.Printf("Set operation successful for key: %s", key)

// Allow value to pass through buffers
time.Sleep(10 * time.Millisecond)

getValue, found := cache.Get(key)
if !found || getValue != value {
t.Errorf("Failed to get key: %s, expected value: %s, got: %v", key, value, getValue)
} else {
log.Printf("Get operation successful for key: %s", key)
}
}

// TestCacheClearKey tests the clearing of a specific key from the cache
func TestCacheClearKey(t *testing.T) {
cache, err := redis.NewCache(1000, 1<<20)
if err != nil {
t.Fatalf("Failed to create cache: %v", err)
}

key := "key1"
cache.Set(key, "value1", 1)
log.Printf("Key %s set in cache", key)

time.Sleep(10 * time.Millisecond)

cache.ClearKey(key)
log.Printf("Key %s cleared from cache", key)

time.Sleep(10 * time.Millisecond)

_, found := cache.Get(key)
if found {
t.Errorf("Expected key %s to be cleared", key)
} else {
log.Printf("ClearKey operation successful for key: %s", key)
}
}

// TestCacheClear tests clearing all keys from the cache
func TestCacheClear(t *testing.T) {
cache, err := redis.NewCache(1000, 1<<20)
if err != nil {
t.Fatalf("Failed to create cache: %v", err)
}

key := "key1"
cache.Set(key, "value1", 1)
log.Printf("Key %s set in cache", key)

time.Sleep(10 * time.Millisecond)

cache.Clear()
t.Log("All keys cleared from cache")

time.Sleep(10 * time.Millisecond)

_, found := cache.Get(key)
if found {
t.Errorf("Expected cache to be cleared, but key %s was found", key)
} else {
t.Log("Clear operation successful, cache is empty")
}
}
8 changes: 8 additions & 0 deletions go.mod
Expand Up @@ -8,3 +8,11 @@ require (
github.com/cespare/xxhash/v2 v2.2.0
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f
)

require (
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/golang/glog v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/sys v0.16.0 // indirect
)
22 changes: 22 additions & 0 deletions go.sum
Expand Up @@ -2,7 +2,29 @@ github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8=
github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.2.0 h1:uCdmnmatrKCgMBlM4rMuJZWOkPDqdbZPnrMXDY4gI68=
github.com/golang/glog v1.2.0/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=