cache is a Go cache manager. It can use many cache adapters. The repo is inspired by database/sql .
go get github.com/bcjti/cache
As of now this cache support memory, Memcache and Redis.
First you must import it
import (
"github.com/bcjti/cache"
)
Then init a Cache (example with memory adapter)
bm, err := cache.NewCache("memory", `{"interval":60}`)
Use it like this:
bm.Put("astaxie", 1, 10 * time.Second)
bm.Get("astaxie")
bm.IsExist("astaxie")
bm.Delete("astaxie")
Sets, expiration and conditional writes:
added, err := bm.SAdd("tags", "a", "b") // atomically add members, returns how many were new
removed, err := bm.SRem("tags", "a") // atomically remove members; an emptied set is deleted
members, err := bm.SMembers("tags") // all members, in no particular order
ok, err := bm.Expire("tags", time.Hour) // new TTL for an existing key (false if missing)
ok, err = bm.PutIfExists("k", "v", ttl) // write only if the key exists (false if missing)
Support per adapter:
| Operation | memory | file | redis | memcache | ssdb |
|---|---|---|---|---|---|
| SAdd, SRem, SMembers | yes | yes* | yes (SADD/SREM/SMEMBERS) | no | no |
| Expire | yes | yes* | yes (PEXPIRE) | yes (touch) | yes (expire) |
| PutIfExists | yes | yes* | yes (SET XX) | yes (replace) | no |
Unsupported operations return cache.ErrNotSupported.
* the file adapter serializes these writes within one process only.
Configure memory adapter like this:
{"interval":60}
interval means the gc time. The cache will check at each time interval, whether item has expired.
Memcache adapter use the gomemcache client.
Configure like this:
{"conn":"127.0.0.1:11211"}
Redis adapter use the redigo client.
Configure like this:
{"conn":":6039"}