mirror of
https://github.com/astaxie/beego.git
synced 2025-07-10 02:30:18 +00:00
improve cache modules. support mulit instances
This commit is contained in:
11
cache/cache.go
vendored
11
cache/cache.go
vendored
@ -37,7 +37,7 @@ import (
|
||||
|
||||
// Cache interface contains all behaviors for cache adapter.
|
||||
// usage:
|
||||
// cache.Register("file",cache.NewFileCache()) // this operation is run in init method of file.go.
|
||||
// cache.Register("file",cache.NewFileCache) // this operation is run in init method of file.go.
|
||||
// c,err := cache.NewCache("file","{....}")
|
||||
// c.Put("key",value,3600)
|
||||
// v := c.Get("key")
|
||||
@ -66,12 +66,14 @@ type Cache interface {
|
||||
StartAndGC(config string) error
|
||||
}
|
||||
|
||||
var adapters = make(map[string]Cache)
|
||||
type CacheInstance func() Cache
|
||||
|
||||
var adapters = make(map[string]CacheInstance)
|
||||
|
||||
// Register makes a cache adapter available by the adapter name.
|
||||
// If Register is called twice with the same name or if driver is nil,
|
||||
// it panics.
|
||||
func Register(name string, adapter Cache) {
|
||||
func Register(name string, adapter CacheInstance) {
|
||||
if adapter == nil {
|
||||
panic("cache: Register adapter is nil")
|
||||
}
|
||||
@ -85,11 +87,12 @@ func Register(name string, adapter Cache) {
|
||||
// config need to be correct JSON as string: {"interval":360}.
|
||||
// it will start gc automatically.
|
||||
func NewCache(adapterName, config string) (adapter Cache, err error) {
|
||||
adapter, ok := adapters[adapterName]
|
||||
instanceFunc, ok := adapters[adapterName]
|
||||
if !ok {
|
||||
err = fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName)
|
||||
return
|
||||
}
|
||||
adapter = instanceFunc()
|
||||
err = adapter.StartAndGC(config)
|
||||
if err != nil {
|
||||
adapter = nil
|
||||
|
Reference in New Issue
Block a user