mirror of
https://github.com/gookit/cache.git
synced 2025-09-26 20:21:16 +08:00
add some new drivers
This commit is contained in:
@@ -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
|
||||
|
@@ -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
|
||||
|
||||
|
@@ -4,8 +4,9 @@ package boltdb
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"github.com/etcd-io/bbolt"
|
||||
"time"
|
||||
|
||||
"github.com/etcd-io/bbolt"
|
||||
)
|
||||
|
||||
// BoltDB definition
|
||||
|
@@ -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
|
||||
}
|
||||
|
||||
|
3
cache.go
3
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
|
||||
|
@@ -2,6 +2,7 @@ package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gookit/cache/redis"
|
||||
)
|
||||
|
||||
|
@@ -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
|
||||
}
|
||||
|
47
leveldb/leveldb.go
Normal file
47
leveldb/leveldb.go
Normal file
@@ -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")
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
}
|
||||
|
47
nutsdb/nutsdb.go
Normal file
47
nutsdb/nutsdb.go
Normal file
@@ -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")
|
||||
}
|
||||
|
||||
|
@@ -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 {
|
||||
|
@@ -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)
|
||||
|
0
testdata/.keep
vendored
Normal file
0
testdata/.keep
vendored
Normal file
Reference in New Issue
Block a user