Initial v2 version, better testing, updates
Some checks failed
continuous-integration/drone Build is failing

This commit is contained in:
2023-02-04 20:22:41 -05:00
parent 68bbfbacd0
commit 41d0ef3d97
27 changed files with 1094 additions and 681 deletions

67
driver/lru/lru.go Normal file
View File

@ -0,0 +1,67 @@
package lru
import (
"github.com/hashicorp/golang-lru"
"meow.tf/go/cacheinterface/v2/driver/memory"
"meow.tf/go/cacheinterface/v2/encoder"
"time"
)
type Options struct {
Encoder encoder.Encoder `query:"encoder" default:"msgpack"`
Size int `query:"size" default:"128"`
}
type Cache struct {
options Options
c *lru.Cache
}
func New(options Options) (*Cache, error) {
c, err := lru.New(options.Size)
if err != nil {
return nil, err
}
return &Cache{
options: options,
c: c,
}, nil
}
func (mc *Cache) Has(key string) bool {
_, exists := mc.c.Get(key)
return exists
}
func (mc *Cache) Get(key string, dst any) error {
item, exists := mc.c.Get(key)
if !exists {
return memory.ErrNotExist
}
return memory.CacheGet(item, dst)
}
func (mc *Cache) GetBytes(key string) ([]byte, error) {
item, exists := mc.c.Get(key)
if !exists {
return nil, memory.ErrNotExist
}
return memory.CacheGetBytes(mc.options.Encoder, item)
}
func (mc *Cache) Set(key string, val any, ttl time.Duration) error {
mc.c.Add(key, val)
return nil
}
func (mc *Cache) Del(key string) error {
mc.c.Remove(key)
return nil
}

View File

@ -0,0 +1,13 @@
package lru_test
import (
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestLru(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Lru Suite")
}

66
driver/lru/lru_test.go Normal file
View File

@ -0,0 +1,66 @@
package lru
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"meow.tf/go/cacheinterface/v2/encoder"
"time"
)
var _ = Describe("LRU driver", func() {
Context("Basic operations", func() {
var (
cache *Cache
)
BeforeEach(func() {
var err error
cache, err = New(Options{
Encoder: encoder.JSON,
Size: 128,
})
Expect(err).To(BeNil())
})
It("Should store a value in the cache", func() {
value := "test"
Expect(cache.Set("test", value, time.Minute)).To(BeNil())
})
It("Should get a value from the cache", func() {
value := "test"
var newValue string
Expect(cache.Set("test", value, time.Minute)).To(BeNil())
Expect(cache.Get("test", &newValue)).To(BeNil())
Expect(newValue).To(Equal(value))
})
It("Should get a value from the cache as bytes", func() {
value := "test"
Expect(cache.Set("test", value, time.Minute)).To(BeNil())
newValue, err := cache.GetBytes("test")
Expect(err).To(BeNil())
Expect(newValue).To(Equal([]byte(value)))
})
It("Should check if the cache has a value", func() {
value := "test"
Expect(cache.Set("test", value, time.Minute)).To(BeNil())
Expect(cache.Has("test")).To(BeTrue())
})
It("Should delete a value from the cache", func() {
value := "test"
Expect(cache.Set("test", value, time.Minute)).To(BeNil())
Expect(cache.Has("test")).To(BeTrue())
Expect(cache.Del("test")).To(BeNil())
Expect(cache.Has("test")).To(BeFalse())
})
})
})