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 ¶
// register some cache driver Register(DvrFile, NewFileCache("")) Register(DvrMemory, NewMemoryCache()) Register(DvrRedis, redis.Connect("127.0.0.1:6379", "", 0)) // setting default driver name SetDefName(DvrRedis) // quick use.(it is default driver) // // set Set("name", "cache value", TwoMinutes) // get val := Get("name") // del Del("name") // get: "cache value" fmt.Print(val)
Output:
Index ¶
- Constants
- Variables
- func BindStruct(val interface{}, ptr interface{}) error
- func Clear() error
- 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
- type Cache
- type FileCache
- func (c *FileCache) Clear() 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) DefName() string
- func (m *Manager) Default() Cache
- func (m *Manager) Driver(name string) Cache
- func (m *Manager) Get(name string) Cache
- func (m *Manager) Register(name string, driver Cache) *Manager
- func (m *Manager) SetDefName(driverName string)
- func (m *Manager) Use(driverName string) Cache
- type MarshalFunc
- type MemoryCache
- func (c *MemoryCache) Clear() 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) LastErr() 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 UnmarshalFunc
Examples ¶
Constants ¶
View Source
const ( // 永远存在 FOREVER = 0 // 1 分钟 OneMinutes = 60 * time.Second // 2 分钟 TwoMinutes = 120 * time.Second // 3 分钟 ThreeMinutes = 180 * time.Second // 5 分钟 FiveMinutes = 300 * time.Second // 10 分钟 TenMinutes = 600 * time.Second // 半小时 HalfHour = 1800 * time.Second // 1 小时 OneHour = 3600 * time.Second // 2 小时 TwoHour = 7200 * time.Second // 3 小时 ThreeHour = 10800 * time.Second // 12 小时(半天) HalfDay = 43200 * time.Second // 24 小时(1 天) OneDay = 86400 * time.Second // 2 天 TwoDay = 172800 * time.Second // 3 天 ThreeDay = 259200 * time.Second // 7 天(一周) OneWeek = 604800 * time.Second )
some generic expire time define.
View Source
const ( DvrFile = "file" DvrRedis = "redis" DvrMemory = "memory" DvrMemCached = "memCached" DvrBoltDB = "boltDB" DvrBuntDB = "buntDB" )
default supported cache driver name
Variables ¶
View Source
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
Types ¶
type Cache ¶
type Cache interface { io.Closer // basic op // Has cache key Has(key string) bool // Get value by key Get(key string) interface{} Set(key string, val interface{}, ttl time.Duration) (err error) Del(key string) error // multi op GetMulti(keys []string) map[string]interface{} SetMulti(values map[string]interface{}, ttl time.Duration) (err error) DelMulti(keys []string) error // clear Clear() error }
Cache interface definition
type FileCache ¶
type FileCache struct { // caches in memory MemoryCache // contains filtered or unexported fields }
FileCache definition.
Example ¶
c := NewFileCache("./testdata") key := "name" // set c.Set(key, "cache value", 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 (*Manager) SetDefName ¶
SetDefName set default driver name
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache definition.
Example ¶
c := NewMemoryCache() key := "name" // set c.Set(key, "cache value", 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
Directories ¶
Path | Synopsis |
---|---|
Package leveldb use the https://github.com/dgraph-io/badger as cache driver
|
Package leveldb use the https://github.com/dgraph-io/badger as cache driver |
Package bolt use the github.com/etcd-io/bbolt as cache driver
|
Package bolt use the 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 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. |
Click to show internal directories.
Click to hide internal directories.