diff --git a/.travis.yml b/.travis.yml index 000b437..245be03 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,5 +10,5 @@ before_install: - go get golang.org/x/tools/cmd/cover script: -# - go test -v -cover + # - go test -v -cover - $HOME/gopath/bin/goveralls -v -service=travis-ci diff --git a/README_cn.md b/README_cn.md index 2d24625..29ccbb9 100644 --- a/README_cn.md +++ b/README_cn.md @@ -15,6 +15,9 @@ Go下通用的缓存使用库,通过包装各种常用的驱动,来提供统 - memCached powered by `github.com/bradfitz/gomemcache` - buntdb powered by `github.com/tidwall/buntdb` - boltdb powered by `github.com/etcd-io/bbolt` +- badger db https://github.com/dgraph-io/badger +- nutsdb https://github.com/xujiajun/nutsdb +- goleveldb https://github.com/syndtr/goleveldb ## GoDoc diff --git a/boltdb/boltdb.go b/boltdb/boltdb.go index e2b40bd..60d04c9 100644 --- a/boltdb/boltdb.go +++ b/boltdb/boltdb.go @@ -4,8 +4,9 @@ package boltdb import ( "bytes" "encoding/gob" - "github.com/etcd-io/bbolt" "time" + + "github.com/etcd-io/bbolt" ) // BoltDB definition diff --git a/buntdb/buntdb.go b/buntdb/buntdb.go index ce08a16..a803176 100644 --- a/buntdb/buntdb.go +++ b/buntdb/buntdb.go @@ -2,9 +2,10 @@ package buntdb import ( + "time" + "github.com/gookit/cache" "github.com/tidwall/buntdb" - "time" ) // Memory open a file that does not persist to disk. @@ -75,7 +76,6 @@ func (c *BuntDB) Get(key string) interface{} { if err != nil { return nil } - return val } diff --git a/cache.go b/cache.go index 02ed141..5424b0f 100644 --- a/cache.go +++ b/cache.go @@ -5,11 +5,14 @@ package cache import ( "encoding/json" + "io" "time" ) // Cache interface definition type Cache interface { + io.Closer + // basic op // Has cache key Has(key string) bool diff --git a/example_test.go b/example_test.go index cef9e19..994ed0d 100644 --- a/example_test.go +++ b/example_test.go @@ -2,6 +2,7 @@ package cache import ( "fmt" + "github.com/gookit/cache/redis" ) diff --git a/file_cache.go b/file_cache.go index 088b1ed..071950c 100644 --- a/file_cache.go +++ b/file_cache.go @@ -86,7 +86,7 @@ func (c *FileCache) Get(key string) interface{} { } // has been expired. delete it. - c.Del(key) + c.lastErr = c.Del(key) return nil } @@ -129,7 +129,7 @@ func (c *FileCache) Set(key string, val interface{}, ttl time.Duration) (err err // Del value by key func (c *FileCache) Del(key string) error { - c.MemoryCache.Del(key) + c.lastErr = c.MemoryCache.Del(key) c.lock.RLock() defer c.lock.RUnlock() @@ -166,7 +166,7 @@ func (c *FileCache) SetMulti(values map[string]interface{}, ttl time.Duration) ( // DelMulti values by multi key func (c *FileCache) DelMulti(keys []string) error { for _, key := range keys { - c.Del(key) + _= c.Del(key) } return nil } diff --git a/leveldb/leveldb.go b/leveldb/leveldb.go new file mode 100644 index 0000000..db6708b --- /dev/null +++ b/leveldb/leveldb.go @@ -0,0 +1,47 @@ +// Package leveldb use the https://github.com/syndtr/goleveldb as cache driver +package leveldb + +import "time" + +// LevelDB definition +type LevelDB struct { + +} + +func (c *LevelDB) Has(key string) bool { + panic("implement me") +} + +func (c *LevelDB) Get(key string) interface{} { + panic("implement me") +} + +func (c *LevelDB) Set(key string, val interface{}, ttl time.Duration) (err error) { + panic("implement me") +} + +func (c *LevelDB) Del(key string) error { + panic("implement me") +} + +func (c *LevelDB) GetMulti(keys []string) map[string]interface{} { + panic("implement me") +} + +func (c *LevelDB) SetMulti(values map[string]interface{}, ttl time.Duration) (err error) { + panic("implement me") +} + +func (c *LevelDB) DelMulti(keys []string) error { + panic("implement me") +} + +func (c *LevelDB) Clear() error { + panic("implement me") +} + +func (c *LevelDB) Close() error { + panic("implement me") +} + + diff --git a/memcached/memcached.go b/memcached/memcached.go index 374069f..c29a252 100644 --- a/memcached/memcached.go +++ b/memcached/memcached.go @@ -2,9 +2,10 @@ package memcached import ( + "time" + "github.com/bradfitz/gomemcache/memcache" "github.com/gookit/cache" - "time" ) // MemCached definition @@ -124,6 +125,11 @@ func (c *MemCached) Clear() error { return c.client.DeleteAll() } +// Close driver +func (*MemCached) Close() error { + return nil +} + // Client get func (c *MemCached) Client() *memcache.Client { return c.client diff --git a/memory_cache.go b/memory_cache.go index f45166f..6703591 100644 --- a/memory_cache.go +++ b/memory_cache.go @@ -49,7 +49,7 @@ func (c *MemoryCache) Get(key string) interface{} { } // has been expired. delete it. - c.Del(key) + _= c.Del(key) } c.lock.RUnlock() @@ -107,7 +107,7 @@ func (c *MemoryCache) SetMulti(values map[string]interface{}, ttl time.Duration) // DelMulti values by multi key func (c *MemoryCache) DelMulti(keys []string) error { for _, key := range keys { - c.Del(key) + _= c.Del(key) } return nil } diff --git a/nutsdb/nutsdb.go b/nutsdb/nutsdb.go new file mode 100644 index 0000000..2828547 --- /dev/null +++ b/nutsdb/nutsdb.go @@ -0,0 +1,47 @@ +// Package nutsdb use the https://github.com/xujiajun/nutsdb as cache driver +package nutsdb + +import "time" + +// NutsDB definition +type NutsDB struct { + +} + +func (c *NutsDB) Has(key string) bool { + panic("implement me") +} + +func (c *NutsDB) Get(key string) interface{} { + panic("implement me") +} + +func (c *NutsDB) Set(key string, val interface{}, ttl time.Duration) (err error) { + panic("implement me") +} + +func (c *NutsDB) Del(key string) error { + panic("implement me") +} + +func (c *NutsDB) GetMulti(keys []string) map[string]interface{} { + panic("implement me") +} + +func (c *NutsDB) SetMulti(values map[string]interface{}, ttl time.Duration) (err error) { + panic("implement me") +} + +func (c *NutsDB) DelMulti(keys []string) error { + panic("implement me") +} + +func (c *NutsDB) Clear() error { + panic("implement me") +} + +func (c *NutsDB) Close() error { + panic("implement me") +} + + diff --git a/redis/redis.go b/redis/redis.go index 4fd862e..4286f38 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -5,9 +5,10 @@ package redis import ( "errors" "fmt" - "github.com/gomodule/redigo/redis" "log" "time" + + "github.com/gomodule/redigo/redis" ) // RedisCache definition. @@ -148,6 +149,11 @@ func (c *RedisCache) DelMulti(keys []string) (err error) { return } +// Close connections +func (c *RedisCache) Close() error { + return c.pool.Close() +} + // Clear all caches func (c *RedisCache) Clear() error { conn := c.pool.Get() @@ -233,11 +239,11 @@ func newPool(url, password string, dbNum int) *redis.Pool { if password != "" { _, err := c.Do("AUTH", password) if err != nil { - c.Close() + _= c.Close() return nil, err } } - c.Do("SELECT", dbNum) + _,_ = c.Do("SELECT", dbNum) return c, err }, TestOnBorrow: func(c redis.Conn, t time.Time) error { diff --git a/redis/redis_test.go b/redis/redis_test.go index 9e20525..d9131cd 100644 --- a/redis/redis_test.go +++ b/redis/redis_test.go @@ -7,11 +7,13 @@ func Example() { c := Connect("127.0.0.1:6379", "", 0) // set - c.Set("name", "cache value", 60) + _= c.Set("name", "cache value", 60) + // get val := c.Get("name") + // del - c.Del("name") + _= c.Del("name") // get: "cache value" fmt.Print(val) diff --git a/testdata/.keep b/testdata/.keep new file mode 100644 index 0000000..e69de29