123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- package cache
- import (
- "fmt"
- "time"
- )
- type Cache interface {
-
- Get(key string) interface{}
-
- GetMulti(keys []string) []interface{}
-
- Put(key string, val interface{}, timeout time.Duration) error
-
- Delete(key string) error
-
- Incr(key string) error
-
- Decr(key string) error
-
- IsExist(key string) bool
-
- ClearAll() error
-
- StartAndGC(config string) error
- }
- type Instance func() Cache
- var adapters = make(map[string]Instance)
- func Register(name string, adapter Instance) {
- if adapter == nil {
- panic("cache: Register adapter is nil")
- }
- if _, ok := adapters[name]; ok {
- panic("cache: Register called twice for adapter " + name)
- }
- adapters[name] = adapter
- }
- func NewCache(adapterName, config string) (adapter Cache, err error) {
- 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
- }
- return
- }
|