github.com/astaxie/beego@v1.12.3/cache/cache.go (about) 1 // Copyright 2014 beego Author. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package cache provide a Cache interface and some implement engine 16 // Usage: 17 // 18 // import( 19 // "github.com/astaxie/beego/cache" 20 // ) 21 // 22 // bm, err := cache.NewCache("memory", `{"interval":60}`) 23 // 24 // Use it like this: 25 // 26 // bm.Put("astaxie", 1, 10 * time.Second) 27 // bm.Get("astaxie") 28 // bm.IsExist("astaxie") 29 // bm.Delete("astaxie") 30 // 31 // more docs http://beego.me/docs/module/cache.md 32 package cache 33 34 import ( 35 "fmt" 36 "time" 37 ) 38 39 // Cache interface contains all behaviors for cache adapter. 40 // usage: 41 // cache.Register("file",cache.NewFileCache) // this operation is run in init method of file.go. 42 // c,err := cache.NewCache("file","{....}") 43 // c.Put("key",value, 3600 * time.Second) 44 // v := c.Get("key") 45 // 46 // c.Incr("counter") // now is 1 47 // c.Incr("counter") // now is 2 48 // count := c.Get("counter").(int) 49 type Cache interface { 50 // get cached value by key. 51 Get(key string) interface{} 52 // GetMulti is a batch version of Get. 53 GetMulti(keys []string) []interface{} 54 // set cached value with key and expire time. 55 Put(key string, val interface{}, timeout time.Duration) error 56 // delete cached value by key. 57 Delete(key string) error 58 // increase cached int value by key, as a counter. 59 Incr(key string) error 60 // decrease cached int value by key, as a counter. 61 Decr(key string) error 62 // check if cached value exists or not. 63 IsExist(key string) bool 64 // clear all cache. 65 ClearAll() error 66 // start gc routine based on config string settings. 67 StartAndGC(config string) error 68 } 69 70 // Instance is a function create a new Cache Instance 71 type Instance func() Cache 72 73 var adapters = make(map[string]Instance) 74 75 // Register makes a cache adapter available by the adapter name. 76 // If Register is called twice with the same name or if driver is nil, 77 // it panics. 78 func Register(name string, adapter Instance) { 79 if adapter == nil { 80 panic("cache: Register adapter is nil") 81 } 82 if _, ok := adapters[name]; ok { 83 panic("cache: Register called twice for adapter " + name) 84 } 85 adapters[name] = adapter 86 } 87 88 // NewCache Create a new cache driver by adapter name and config string. 89 // config need to be correct JSON as string: {"interval":360}. 90 // it will start gc automatically. 91 func NewCache(adapterName, config string) (adapter Cache, err error) { 92 instanceFunc, ok := adapters[adapterName] 93 if !ok { 94 err = fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName) 95 return 96 } 97 adapter = instanceFunc() 98 err = adapter.StartAndGC(config) 99 if err != nil { 100 adapter = nil 101 } 102 return 103 }