mirror of
https://github.com/astaxie/beego.git
synced 2024-12-22 22:40:49 +00:00
improve cache modules. support mulit instances
This commit is contained in:
parent
92d157736b
commit
a03fa0fb73
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
|
||||
|
10
cache/file.go
vendored
10
cache/file.go
vendored
@ -29,10 +29,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
Register("file", NewFileCache())
|
||||
}
|
||||
|
||||
// FileCacheItem is basic unit of file cache adapter.
|
||||
// it contains data and expire time.
|
||||
type FileCacheItem struct {
|
||||
@ -59,7 +55,7 @@ type FileCache struct {
|
||||
|
||||
// NewFileCache Create new file cache with no config.
|
||||
// the level and expiry need set in method StartAndGC as config string.
|
||||
func NewFileCache() *FileCache {
|
||||
func NewFileCache() Cache {
|
||||
// return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix}
|
||||
return &FileCache{}
|
||||
}
|
||||
@ -272,3 +268,7 @@ func GobDecode(data []byte, to *FileCacheItem) error {
|
||||
dec := gob.NewDecoder(buf)
|
||||
return dec.Decode(&to)
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register("file", NewFileCache)
|
||||
}
|
||||
|
4
cache/memcache/memcache.go
vendored
4
cache/memcache/memcache.go
vendored
@ -46,7 +46,7 @@ type Cache struct {
|
||||
}
|
||||
|
||||
// NewMemCache create new memcache adapter.
|
||||
func NewMemCache() *Cache {
|
||||
func NewMemCache() cache.Cache {
|
||||
return &Cache{}
|
||||
}
|
||||
|
||||
@ -184,5 +184,5 @@ func (rc *Cache) connectInit() error {
|
||||
}
|
||||
|
||||
func init() {
|
||||
cache.Register("memcache", NewMemCache())
|
||||
cache.Register("memcache", NewMemCache)
|
||||
}
|
||||
|
4
cache/memory.go
vendored
4
cache/memory.go
vendored
@ -44,7 +44,7 @@ type MemoryCache struct {
|
||||
}
|
||||
|
||||
// NewMemoryCache returns a new MemoryCache.
|
||||
func NewMemoryCache() *MemoryCache {
|
||||
func NewMemoryCache() Cache {
|
||||
cache := MemoryCache{items: make(map[string]*MemoryItem)}
|
||||
return &cache
|
||||
}
|
||||
@ -234,5 +234,5 @@ func (bc *MemoryCache) itemExpired(name string) bool {
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register("memory", NewMemoryCache())
|
||||
Register("memory", NewMemoryCache)
|
||||
}
|
||||
|
4
cache/redis/redis.go
vendored
4
cache/redis/redis.go
vendored
@ -55,7 +55,7 @@ type Cache struct {
|
||||
}
|
||||
|
||||
// NewRedisCache create new redis cache with default collection name.
|
||||
func NewRedisCache() *Cache {
|
||||
func NewRedisCache() cache.Cache {
|
||||
return &Cache{key: DefaultKey}
|
||||
}
|
||||
|
||||
@ -236,5 +236,5 @@ func (rc *Cache) connectInit() {
|
||||
}
|
||||
|
||||
func init() {
|
||||
cache.Register("redis", NewRedisCache())
|
||||
cache.Register("redis", NewRedisCache)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user