Initial v2 version, better testing, updates
Some checks failed
continuous-integration/drone Build is failing
Some checks failed
continuous-integration/drone Build is failing
This commit is contained in:
67
driver/lru/lru.go
Normal file
67
driver/lru/lru.go
Normal 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
|
||||
}
|
13
driver/lru/lru_suite_test.go
Normal file
13
driver/lru/lru_suite_test.go
Normal 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
66
driver/lru/lru_test.go
Normal 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())
|
||||
})
|
||||
})
|
||||
})
|
68
driver/memcache/memcache.go
Normal file
68
driver/memcache/memcache.go
Normal file
@ -0,0 +1,68 @@
|
||||
package memcache
|
||||
|
||||
import (
|
||||
"github.com/bradfitz/gomemcache/memcache"
|
||||
"meow.tf/go/cacheinterface/v2/encoder"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Encoder encoder.Encoder `query:"encoder" default:"msgpack"`
|
||||
Servers []string `default:"127.0.0.1:11211"`
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
options Options
|
||||
servers []string
|
||||
client *memcache.Client
|
||||
}
|
||||
|
||||
func New(options Options) (*Cache, error) {
|
||||
client := memcache.New(options.Servers...)
|
||||
|
||||
return &Cache{
|
||||
options: options,
|
||||
servers: options.Servers,
|
||||
client: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (mc *Cache) Has(key string) bool {
|
||||
_, err := mc.client.Get(key)
|
||||
|
||||
return err != nil
|
||||
}
|
||||
|
||||
func (mc *Cache) Get(key string, dst any) error {
|
||||
item, err := mc.client.Get(key)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return encoder.DecodeValue(mc.options.Encoder, item.Value, dst)
|
||||
}
|
||||
|
||||
func (mc *Cache) GetBytes(key string) ([]byte, error) {
|
||||
item, err := mc.client.Get(key)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return item.Value, nil
|
||||
}
|
||||
|
||||
func (mc *Cache) Set(key string, val any, ttl time.Duration) error {
|
||||
v, err := encoder.EncodeValue(mc.options.Encoder, val)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return mc.client.Set(&memcache.Item{Key: key, Value: v, Expiration: int32(ttl.Seconds())})
|
||||
}
|
||||
|
||||
func (mc *Cache) Del(key string) error {
|
||||
return mc.client.Delete(key)
|
||||
}
|
199
driver/memory/memory.go
Normal file
199
driver/memory/memory.go
Normal file
@ -0,0 +1,199 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"meow.tf/go/cacheinterface/v2/encoder"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotExist = errors.New("item does not exist")
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Encoder encoder.Encoder `query:"encoder" default:"msgpack"`
|
||||
DefaultExpiration time.Duration `query:"defaultExpiration"`
|
||||
CleanupTime time.Duration `query:"cleanupTime" default:"5m"`
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
options Options
|
||||
c *cache.Cache
|
||||
}
|
||||
|
||||
func New(options Options) (*Cache, error) {
|
||||
c := cache.New(1*time.Minute, options.CleanupTime)
|
||||
|
||||
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 ErrNotExist
|
||||
}
|
||||
|
||||
return CacheGet(item, dst)
|
||||
}
|
||||
|
||||
func (mc *Cache) GetBytes(key string) ([]byte, error) {
|
||||
item, exists := mc.c.Get(key)
|
||||
|
||||
if !exists {
|
||||
return nil, ErrNotExist
|
||||
}
|
||||
|
||||
return CacheGetBytes(mc.options.Encoder, item)
|
||||
}
|
||||
|
||||
func (mc *Cache) Set(key string, val any, ttl time.Duration) error {
|
||||
mc.c.Set(key, val, ttl)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mc *Cache) Del(key string) error {
|
||||
mc.c.Delete(key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func CacheGetBytes(encoder encoder.Encoder, item any) ([]byte, error) {
|
||||
switch item.(type) {
|
||||
case string:
|
||||
return []byte(item.(string)), nil
|
||||
case []byte:
|
||||
return item.([]byte), nil
|
||||
}
|
||||
|
||||
return encoder.Marshal(item)
|
||||
}
|
||||
|
||||
func CacheGet(item any, v any) error {
|
||||
switch v := v.(type) {
|
||||
case *string:
|
||||
if v != nil {
|
||||
*v = item.(string)
|
||||
return nil
|
||||
}
|
||||
case *[]byte:
|
||||
if v != nil {
|
||||
*v = item.([]byte)
|
||||
return nil
|
||||
}
|
||||
case *int:
|
||||
if v != nil {
|
||||
*v = item.(int)
|
||||
return nil
|
||||
}
|
||||
case *int8:
|
||||
if v != nil {
|
||||
*v = item.(int8)
|
||||
return nil
|
||||
}
|
||||
case *int16:
|
||||
if v != nil {
|
||||
*v = item.(int16)
|
||||
return nil
|
||||
}
|
||||
case *int32:
|
||||
if v != nil {
|
||||
*v = item.(int32)
|
||||
return nil
|
||||
}
|
||||
case *int64:
|
||||
if v != nil {
|
||||
*v = item.(int64)
|
||||
return nil
|
||||
}
|
||||
case *uint:
|
||||
if v != nil {
|
||||
*v = item.(uint)
|
||||
return nil
|
||||
}
|
||||
case *uint8:
|
||||
if v != nil {
|
||||
*v = item.(uint8)
|
||||
return nil
|
||||
}
|
||||
case *uint16:
|
||||
if v != nil {
|
||||
*v = item.(uint16)
|
||||
return nil
|
||||
}
|
||||
case *uint32:
|
||||
if v != nil {
|
||||
*v = item.(uint32)
|
||||
return nil
|
||||
}
|
||||
case *uint64:
|
||||
if v != nil {
|
||||
*v = item.(uint64)
|
||||
return nil
|
||||
}
|
||||
case *bool:
|
||||
if v != nil {
|
||||
*v = item.(bool)
|
||||
return nil
|
||||
}
|
||||
case *float32:
|
||||
if v != nil {
|
||||
*v = item.(float32)
|
||||
return nil
|
||||
}
|
||||
case *float64:
|
||||
if v != nil {
|
||||
*v = item.(float64)
|
||||
return nil
|
||||
}
|
||||
case *[]string:
|
||||
*v = item.([]string)
|
||||
return nil
|
||||
case *map[string]string:
|
||||
*v = item.(map[string]string)
|
||||
return nil
|
||||
case *map[string]any:
|
||||
*v = item.(map[string]any)
|
||||
return nil
|
||||
case *time.Duration:
|
||||
if v != nil {
|
||||
*v = item.(time.Duration)
|
||||
return nil
|
||||
}
|
||||
case *time.Time:
|
||||
if v != nil {
|
||||
*v = item.(time.Time)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
vv := reflect.ValueOf(v)
|
||||
|
||||
if !vv.IsValid() {
|
||||
return errors.New("dst pointer is not valid")
|
||||
}
|
||||
|
||||
if vv.Kind() != reflect.Ptr {
|
||||
return errors.New("dst pointer is not a pointer")
|
||||
}
|
||||
|
||||
vv = vv.Elem()
|
||||
|
||||
if !vv.IsValid() {
|
||||
return errors.New("dst pointer is not a valid element")
|
||||
}
|
||||
|
||||
vv.Set(reflect.ValueOf(item))
|
||||
|
||||
return nil
|
||||
}
|
13
driver/memory/memory_suite_test.go
Normal file
13
driver/memory/memory_suite_test.go
Normal file
@ -0,0 +1,13 @@
|
||||
package memory_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestMemory(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Memory Suite")
|
||||
}
|
65
driver/memory/memory_test.go
Normal file
65
driver/memory/memory_test.go
Normal file
@ -0,0 +1,65 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"meow.tf/go/cacheinterface/v2/encoder"
|
||||
"time"
|
||||
)
|
||||
|
||||
var _ = Describe("Memory driver", func() {
|
||||
Context("Basic operations", func() {
|
||||
var (
|
||||
cache *Cache
|
||||
)
|
||||
BeforeEach(func() {
|
||||
var err error
|
||||
|
||||
cache, err = New(Options{
|
||||
Encoder: encoder.JSON,
|
||||
})
|
||||
|
||||
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())
|
||||
})
|
||||
})
|
||||
})
|
73
driver/redis/redis.go
Normal file
73
driver/redis/redis.go
Normal file
@ -0,0 +1,73 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"github.com/hoisie/redis"
|
||||
"meow.tf/go/cacheinterface/v2/encoder"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Encoder encoder.Encoder `query:"encoder" default:"msgpack"`
|
||||
Address string `default:"127.0.0.1"`
|
||||
DB int `default:"0" query:"db"`
|
||||
Password string `query:"password"`
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
options Options
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
func New(options Options) (*Cache, error) {
|
||||
rc := &redis.Client{
|
||||
Addr: options.Address,
|
||||
Db: options.DB,
|
||||
Password: options.Password,
|
||||
}
|
||||
|
||||
return &Cache{
|
||||
options: options,
|
||||
client: rc,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (rc *Cache) Has(key string) bool {
|
||||
b, _ := rc.client.Exists(key)
|
||||
return b
|
||||
}
|
||||
|
||||
func (rc *Cache) Get(key string, dst any) error {
|
||||
b, err := rc.client.Get(key)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return encoder.DecodeValue(rc.options.Encoder, b, dst)
|
||||
}
|
||||
|
||||
func (rc *Cache) GetBytes(key string) ([]byte, error) {
|
||||
b, err := rc.client.Get(key)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (rc *Cache) Set(key string, val any, ttl time.Duration) error {
|
||||
v, err := encoder.EncodeValue(rc.options.Encoder, val)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return rc.client.Setex(key, int64(ttl.Seconds()), v)
|
||||
}
|
||||
|
||||
func (rc *Cache) Del(key string) error {
|
||||
_, err := rc.client.Del(key)
|
||||
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user