go get -u github.com/no-src/nscache
First, you need to import the cache driver, then create your cache component instance with the specified connection string and use it.
Current support following cache drivers
Driver | Import Driver Package | Connection String Example |
---|---|---|
Memory | github.com/no-src/nscache/memory |
memory: |
Redis | github.com/no-src/nscache/redis |
redis:https://127.0.0.1:6379 |
BuntDB | github.com/no-src/nscache/buntdb |
buntdb:https://:memory: or buntdb:https://buntdb.db |
Etcd | github.com/no-src/nscache/etcd |
etcd:https://127.0.0.1:2379?dial_timeout=5s |
For example, initial a memory cache and write, read and remove data.
package main
import (
"time"
_ "github.com/no-src/nscache/memory"
"github.com/no-src/log"
"github.com/no-src/nscache"
)
func main() {
// initial cache driver
c, err := nscache.NewCache("memory:")
if err != nil {
log.Error(err, "init cache error")
return
}
// write data
k := "hello"
c.Set(k, "world", time.Minute)
// read data
var v string
if err = c.Get(k, &v); err != nil {
log.Error(err, "get cache error")
return
}
log.Info("key=%s value=%s", k, v)
// remove data
if err = c.Remove(k); err != nil {
log.Error(err, "remove cache error")
return
}
}