Documentation ¶
Overview ¶
Package cache is a generic cache use and cache manager for golang. FileCache is a simple local file system cache implement. MemoryCache is a simple memory cache implement.
Example ¶
package main import ( "fmt" "github.com/gookit/cache" "github.com/gookit/cache/goredis" "github.com/gookit/cache/redis" ) func main() { // register some cache driver cache.Register(cache.DvrFile, cache.NewFileCache("")) cache.Register(cache.DvrMemory, cache.NewMemoryCache()) cache.Register(redis.Name, redis.Connect("127.0.0.1:6379", "", 0)) cache.Register(goredis.Name, goredis.Connect("127.0.0.1:6379", "", 0)) // setting default driver name cache.DefaultUse(goredis.Name) // 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.GetCache(DvrFile) // fc.Set("key", "value", 10) // fc.Get("key") }
Output:
Example (WithOptions) ¶
package main import ( "github.com/gookit/cache" "github.com/gookit/cache/goredis" "github.com/gookit/goutil/dump" ) func main() { gords := goredis.Connect("127.0.0.1:6379", "", 0) gords.WithOptions(cache.WithPrefix("cache_"), cache.WithEncode(true)) // register cache.Register(goredis.Name, gords) // set // real key is: "cache_name" cache.Set("name", "cache value", cache.TwoMinutes) // get: "cache value" val := cache.Get("name") dump.P(val) }
Output:
Index ¶
- Constants
- Variables
- func BindStruct(val interface{}, ptr interface{}) error
- func Clear() error
- func ClearAll() error
- func Close() error
- func DefaultUse(driverName string)
- func Del(key string) error
- func DelMulti(keys []string) error
- func Get(key string) interface{}
- func GetMulti(keys []string) map[string]interface{}
- func GobDecode(bts []byte, ptr interface{}) error
- func GobEncode(val interface{}) (bs []byte, err error)
- func Has(key string) bool
- func Set(key string, val interface{}, ttl time.Duration) error
- func SetDefName(driverName string)
- func SetMulti(mv map[string]interface{}, ttl time.Duration) error
- func Unregister(name string) int
- func UnregisterAll(fn ...func(cache Cache)) int
- func WithDebug(debug bool) func(opt *Option)
- func WithEncode(encode bool) func(opt *Option)
- func WithPrefix(prefix string) func(opt *Option)
- type BaseDriver
- func (l *BaseDriver) BuildKeys(keys []string) []string
- func (l *BaseDriver) Debugf(format string, v ...interface{})
- func (l *BaseDriver) IsDebug() bool
- func (l *BaseDriver) Key(key string) string
- func (l *BaseDriver) LastErr(key string) error
- func (l *BaseDriver) Logf(format string, v ...interface{})
- func (l *BaseDriver) Marshal(val interface{}) (interface{}, error)
- func (l *BaseDriver) MustMarshal(val interface{}) ([]byte, error)
- func (l *BaseDriver) SetLastErr(err error)
- func (l *BaseDriver) Unmarshal(val []byte, err error) interface{}
- func (l *BaseDriver) UnmarshalTo(bts []byte, ptr interface{}) error
- func (l *BaseDriver) WithOptions(optFns ...func(option *Option))
- type Cache
- type FileCache
- func (c *FileCache) Clear() error
- func (c *FileCache) Close() error
- func (c *FileCache) Del(key string) error
- func (c *FileCache) DelMulti(keys []string) error
- func (c *FileCache) Get(key string) interface{}
- func (c *FileCache) GetFilename(key string) string
- func (c *FileCache) GetMulti(keys []string) map[string]interface{}
- func (c *FileCache) Has(key string) bool
- func (c *FileCache) Set(key string, val interface{}, ttl time.Duration) (err error)
- func (c *FileCache) SetMulti(values map[string]interface{}, ttl time.Duration) (err error)
- type Item
- type Manager
- func (m *Manager) Cache(driverName string) Cache
- func (m *Manager) ClearAll() (err error)
- func (m *Manager) Close() (err error)
- func (m *Manager) DefName() string
- func (m *Manager) Default() Cache
- func (m *Manager) DefaultUse(driverName string)
- func (m *Manager) Del(key string) error
- func (m *Manager) DelMulti(keys []string) error
- func (m *Manager) Driver(driverName string) Cache
- func (m *Manager) Get(key string) interface{}
- func (m *Manager) GetMulti(keys []string) map[string]interface{}
- func (m *Manager) Has(key string) bool
- func (m *Manager) Register(name string, driver Cache) *Manager
- func (m *Manager) Set(key string, val interface{}, ttl time.Duration) error
- func (m *Manager) SetDefName(driverName string)
- func (m *Manager) SetMulti(mv map[string]interface{}, ttl time.Duration) error
- func (m *Manager) Unregister(name string) int
- func (m *Manager) UnregisterAll(fn ...func(cache Cache)) int
- func (m *Manager) Use(driverName string) Cache
- type MarshalFunc
- type MemoryCache
- func (c *MemoryCache) Clear() error
- func (c *MemoryCache) Close() error
- func (c *MemoryCache) Count() int
- func (c *MemoryCache) Del(key string) error
- func (c *MemoryCache) DelMulti(keys []string) error
- func (c *MemoryCache) DumpDB(file string) error
- func (c *MemoryCache) Get(key string) interface{}
- func (c *MemoryCache) GetMulti(keys []string) map[string]interface{}
- func (c *MemoryCache) Has(key string) bool
- func (c *MemoryCache) Iter(file string) error
- func (c *MemoryCache) Restore(file string) error
- func (c *MemoryCache) Set(key string, val interface{}, ttl time.Duration) (err error)
- func (c *MemoryCache) SetMulti(values map[string]interface{}, ttl time.Duration) (err error)
- type Option
- type UnmarshalFunc
Examples ¶
Constants ¶
const ( // Forever Always exist Forever = 0 // Seconds1 1 second Seconds1 = time.Second // Seconds2 2 second Seconds2 = 2 * time.Second // Seconds3 3 second Seconds3 = 3 * time.Second // Seconds5 5 second Seconds5 = 5 * time.Second // Seconds6 6 second Seconds6 = 6 * time.Second // Seconds7 7 second Seconds7 = 7 * time.Second // Seconds8 8 second Seconds8 = 8 * time.Second // Seconds9 9 second Seconds9 = 9 * time.Second // Seconds10 10 second Seconds10 = 10 * time.Second // Seconds15 15 second Seconds15 = 15 * time.Second // Seconds20 20 second Seconds20 = 20 * time.Second // Seconds30 30 second Seconds30 = 30 * time.Second // OneMinutes 1 minutes OneMinutes = 60 * time.Second // TwoMinutes 2 minutes TwoMinutes = 120 * time.Second // ThreeMinutes 3 minutes ThreeMinutes = 180 * time.Second // FiveMinutes 5 minutes FiveMinutes = 300 * time.Second // TenMinutes 10 minutes TenMinutes = 600 * time.Second // FifteenMinutes 15 minutes FifteenMinutes = 900 * time.Second // HalfHour half an hour HalfHour = 1800 * time.Second // OneHour 1 hour OneHour = 3600 * time.Second // TwoHour 2 hours TwoHour = 7200 * time.Second // ThreeHour 3 hours ThreeHour = 10800 * time.Second // HalfDay 12 hours(half of the day) HalfDay = 43200 * time.Second // OneDay 24 hours(1 day) OneDay = 86400 * time.Second // TwoDay 2 day TwoDay = 172800 * time.Second // ThreeDay 3 day ThreeDay = 259200 * time.Second // OneWeek 7 day(one week) OneWeek = 604800 * time.Second )
some generic expire time define.
const ( DvrFile = "file" DvrRedis = "redis" DvrMemory = "memory" DvrMemCached = "memCached" DvrBoltDB = "boltDB" DvrBuntDB = "buntDB" )
default supported cache driver name
Variables ¶
var ( Marshal MarshalFunc = json.Marshal Unmarshal UnmarshalFunc = json.Unmarshal )
data (Un)marshal func
Functions ¶
func BindStruct ¶
func BindStruct(val interface{}, ptr interface{}) error
BindStruct get cache value and map to a struct
func DefaultUse ¶ added in v0.1.1
func DefaultUse(driverName string)
DefaultUse set default driver name
func SetDefName ¶
func SetDefName(driverName string)
SetDefName set default driver name. Deprecated
please use DefaultUse() instead it
func UnregisterAll ¶ added in v0.2.0
UnregisterAll cache drivers
func WithEncode ¶ added in v0.2.0
WithEncode add option: encode
func WithPrefix ¶ added in v0.2.0
WithPrefix add option: prefix
Types ¶
type BaseDriver ¶ added in v0.2.0
type BaseDriver struct {
// contains filtered or unexported fields
}
BaseDriver struct
func (*BaseDriver) BuildKeys ¶ added in v0.2.0
func (l *BaseDriver) BuildKeys(keys []string) []string
BuildKeys real cache keys build
func (*BaseDriver) Debugf ¶ added in v0.2.0
func (l *BaseDriver) Debugf(format string, v ...interface{})
Debugf print an debug message
func (*BaseDriver) Key ¶ added in v0.2.0
func (l *BaseDriver) Key(key string) string
Key real cache key build
func (*BaseDriver) LastErr ¶ added in v0.2.0
func (l *BaseDriver) LastErr(key string) error
LastErr get
func (*BaseDriver) Logf ¶ added in v0.2.0
func (l *BaseDriver) Logf(format string, v ...interface{})
Logf print an log message
func (*BaseDriver) Marshal ¶ added in v0.2.0
func (l *BaseDriver) Marshal(val interface{}) (interface{}, error)
Marshal cache value
func (*BaseDriver) MustMarshal ¶ added in v0.2.0
func (l *BaseDriver) MustMarshal(val interface{}) ([]byte, error)
MustMarshal cache value
func (*BaseDriver) SetLastErr ¶ added in v0.2.0
func (l *BaseDriver) SetLastErr(err error)
SetLastErr save last error
func (*BaseDriver) Unmarshal ¶ added in v0.2.0
func (l *BaseDriver) Unmarshal(val []byte, err error) interface{}
Unmarshal cache value
func (*BaseDriver) UnmarshalTo ¶ added in v0.2.0
func (l *BaseDriver) UnmarshalTo(bts []byte, ptr interface{}) error
UnmarshalTo cache value
func (*BaseDriver) WithOptions ¶ added in v0.2.0
func (l *BaseDriver) WithOptions(optFns ...func(option *Option))
WithOptions for driver
type Cache ¶
type Cache = gsr.SimpleCacher
Cache interface definition
type FileCache ¶
type FileCache struct { BaseDriver // caches in memory MemoryCache // DisableMemCache disable cache in memory DisableMemCache bool // contains filtered or unexported fields }
FileCache definition.
Example ¶
package main import ( "fmt" "github.com/gookit/cache" ) func main() { c := cache.NewFileCache("./testdata") key := "name" // set c.Set(key, "cache value", cache.TwoMinutes) fmt.Println(c.Has(key)) // get val := c.Get(key) fmt.Println(val) // del c.Del(key) fmt.Println(c.Has(key)) }
Output: true cache value false
func NewFileCache ¶
NewFileCache create a FileCache instance
func (*FileCache) GetFilename ¶
GetFilename cache file name build
type Item ¶
type Item struct { // Exp expire time Exp int64 // Val cache value storage Val interface{} }
Item for memory cache
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager definition
func DefManager ¶ added in v0.1.1
func DefManager() *Manager
DefManager get default cache manager instance
func (*Manager) DefaultUse ¶ added in v0.1.1
DefaultUse set default driver name
func (*Manager) SetDefName ¶
SetDefName set default driver name. alias of DefaultUse() Deprecated
please use DefaultUse() instead it
func (*Manager) Unregister ¶ added in v0.2.0
Unregister an cache driver
func (*Manager) UnregisterAll ¶ added in v0.2.0
UnregisterAll cache drivers
type MemoryCache ¶
type MemoryCache struct { // CacheSize TODO set max cache size CacheSize int // contains filtered or unexported fields }
MemoryCache definition.
Example ¶
package main import ( "fmt" "github.com/gookit/cache" ) func main() { c := cache.NewMemoryCache() key := "name" // set c.Set(key, "cache value", cache.TwoMinutes) fmt.Println(c.Has(key), c.Count()) // get val := c.Get(key) fmt.Println(val) // del c.Del(key) fmt.Println(c.Has(key), c.Count()) }
Output: true 1 cache value false 0
func NewMemoryCache ¶
func NewMemoryCache() *MemoryCache
NewMemoryCache create a memory cache instance
func (*MemoryCache) DelMulti ¶
func (c *MemoryCache) DelMulti(keys []string) error
DelMulti values by multi key
func (*MemoryCache) GetMulti ¶
func (c *MemoryCache) GetMulti(keys []string) map[string]interface{}
GetMulti values by multi key
func (*MemoryCache) Restore ¶
func (c *MemoryCache) Restore(file string) error
Restore DB from a file
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package badger use the https://github.com/dgraph-io/badger as cache driver
|
Package badger use the https://github.com/dgraph-io/badger as cache driver |
Package bolt use the go.etcd.io/bbolt(github.com/etcd-io/bbolt) as cache driver
|
Package bolt use the go.etcd.io/bbolt(github.com/etcd-io/bbolt) as cache driver |
Package buntdb use the github.com/tidwall/buntdb as cache driver
|
Package buntdb use the github.com/tidwall/buntdb as cache driver |
Package gcache use the github.com/bluele/gcache as cache driver
|
Package gcache use the github.com/bluele/gcache as cache driver |
Package gocache is a memory cache driver implement.
|
Package gocache is a memory cache driver implement. |
Package goredis is a simple redis cache implement.
|
Package goredis is a simple redis cache implement. |
Package leveldb use the https://github.com/syndtr/goleveldb as cache driver
|
Package leveldb use the https://github.com/syndtr/goleveldb as cache driver |
Package memcached use the "github.com/bradfitz/gomemcache/memcache" as cache driver
|
Package memcached use the "github.com/bradfitz/gomemcache/memcache" as cache driver |
Package nutsdb use the https://github.com/xujiajun/nutsdb as cache driver
|
Package nutsdb use the https://github.com/xujiajun/nutsdb as cache driver |
Package redis is a simple redis cache implement.
|
Package redis is a simple redis cache implement. |