From a03fa0fb736ece41b6be2c30b1c3f92d88e07414 Mon Sep 17 00:00:00 2001 From: astaxie Date: Mon, 4 Jan 2016 10:50:04 +0800 Subject: [PATCH] improve cache modules. support mulit instances --- cache/cache.go | 11 +++++++---- cache/file.go | 10 +++++----- cache/memcache/memcache.go | 4 ++-- cache/memory.go | 4 ++-- cache/redis/redis.go | 4 ++-- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index 6413e210..c9904444 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -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 diff --git a/cache/file.go b/cache/file.go index 9233a852..2d77b567 100644 --- a/cache/file.go +++ b/cache/file.go @@ -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) +} diff --git a/cache/memcache/memcache.go b/cache/memcache/memcache.go index 643fb130..272124da 100644 --- a/cache/memcache/memcache.go +++ b/cache/memcache/memcache.go @@ -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) } diff --git a/cache/memory.go b/cache/memory.go index ce1afa5f..5cc0ab52 100644 --- a/cache/memory.go +++ b/cache/memory.go @@ -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) } diff --git a/cache/redis/redis.go b/cache/redis/redis.go index ed12cf26..60c1702a 100644 --- a/cache/redis/redis.go +++ b/cache/redis/redis.go @@ -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) }