Generic cache use and cache manager for golang. Provide a unified usage API by packaging various commonly used drivers.
All cache driver implemented the cache.Cache interface. So, You can add any custom driver.
Supported Drivers:
- file internal driver
- memory internal driver
redis
bygithub.com/gomodule/redigo
memCached
bygithub.com/bradfitz/gomemcache
buntdb
bygithub.com/tidwall/buntdb
boltdb
bygithub.com/etcd-io/bbolt
badger db
bygithub.com/dgraph-io/badger
nutsdb
bygithub.com/xujiajun/nutsdb
goleveldb
bygithub.com/syndtr/goleveldb
- https://github.com/patrickmn/go-cache
- https://github.com/allegro/bigcache
go get github.com/gookit/cache
All cache driver implemented the cache.Cache interface. So, You can add any custom driver.
// Cache interface definition
type Cache interface {
// basic operation
Has(key string) bool
Get(key string) interface{}
Set(key string, val interface{}, ttl time.Duration) (err error)
Del(key string) error
// multi operation
GetMulti(keys []string) map[string]interface{}
SetMulti(values map[string]interface{}, ttl time.Duration) (err error)
DelMulti(keys []string) error
// clear and close
Clear() error
Close() error
}
package main
import (
"fmt"
"github.com/gookit/cache"
"github.com/gookit/cache/redis"
)
func main() {
// register one(or some) cache driver
cache.Register(cache.DvrFile, cache.NewFileCache(""))
cache.Register(cache.DvrMemory, cache.NewMemoryCache())
cache.Register(cache.DvrRedis, redis.Connect("127.0.0.1:6379", "", 0))
// setting default driver name
cache.DefaultUse(cache.DvrRedis)
// quick use.(it is default driver)
//
// set
cache.Set("name", "cache value", cache.TwoMinutes)
// get
val := cache.Get("name")
// del
cache.Del("name")
// get: "cache value"
fmt.Print(val)
// More ...
// fc := cache.Driver(DvrFile)
// fc.Set("key", "value", 10)
// fc.Get("key")
}
MIT