Merge pull request #404 from fuxiaohei/master
add comments in cache package
Showing
9 changed files
with
114 additions
and
19 deletions
| ... | @@ -4,14 +4,32 @@ import ( | ... | @@ -4,14 +4,32 @@ import ( |
| 4 | "fmt" | 4 | "fmt" |
| 5 | ) | 5 | ) |
| 6 | 6 | ||
| 7 | // Cache interface contains all behaviors for cache adapter. | ||
| 8 | // usage: | ||
| 9 | // cache.Register("file",cache.NewFileCache()) // this operation is run in init method of file.go. | ||
| 10 | // c := cache.NewCache("file","{....}") | ||
| 11 | // c.Put("key",value,3600) | ||
| 12 | // v := c.Get("key") | ||
| 13 | // | ||
| 14 | // c.Incr("counter") // now is 1 | ||
| 15 | // c.Incr("counter") // now is 2 | ||
| 16 | // count := c.Get("counter").(int) | ||
| 7 | type Cache interface { | 17 | type Cache interface { |
| 18 | // get cached value by key. | ||
| 8 | Get(key string) interface{} | 19 | Get(key string) interface{} |
| 20 | // set cached value with key and expire time. | ||
| 9 | Put(key string, val interface{}, timeout int64) error | 21 | Put(key string, val interface{}, timeout int64) error |
| 22 | // delete cached value by key. | ||
| 10 | Delete(key string) error | 23 | Delete(key string) error |
| 24 | // increase cached int value by key, as a counter. | ||
| 11 | Incr(key string) error | 25 | Incr(key string) error |
| 26 | // decrease cached int value by key, as a counter. | ||
| 12 | Decr(key string) error | 27 | Decr(key string) error |
| 28 | // check cached value is existed or not. | ||
| 13 | IsExist(key string) bool | 29 | IsExist(key string) bool |
| 30 | // clear all cache. | ||
| 14 | ClearAll() error | 31 | ClearAll() error |
| 32 | // start gc routine via config string setting. | ||
| 15 | StartAndGC(config string) error | 33 | StartAndGC(config string) error |
| 16 | } | 34 | } |
| 17 | 35 | ||
| ... | @@ -30,7 +48,9 @@ func Register(name string, adapter Cache) { | ... | @@ -30,7 +48,9 @@ func Register(name string, adapter Cache) { |
| 30 | adapters[name] = adapter | 48 | adapters[name] = adapter |
| 31 | } | 49 | } |
| 32 | 50 | ||
| 33 | // config need to be correct JSON as string: {"interval":360} | 51 | // Create a new cache driver by adapter and config string. |
| 52 | // config need to be correct JSON as string: {"interval":360}. | ||
| 53 | // it will start gc automatically. | ||
| 34 | func NewCache(adapterName, config string) (Cache, error) { | 54 | func NewCache(adapterName, config string) (Cache, error) { |
| 35 | adapter, ok := adapters[adapterName] | 55 | adapter, ok := adapters[adapterName] |
| 36 | if !ok { | 56 | if !ok { | ... | ... |
| ... | @@ -5,6 +5,7 @@ import ( | ... | @@ -5,6 +5,7 @@ import ( |
| 5 | "strconv" | 5 | "strconv" |
| 6 | ) | 6 | ) |
| 7 | 7 | ||
| 8 | // convert interface to string. | ||
| 8 | func GetString(v interface{}) string { | 9 | func GetString(v interface{}) string { |
| 9 | switch result := v.(type) { | 10 | switch result := v.(type) { |
| 10 | case string: | 11 | case string: |
| ... | @@ -20,6 +21,7 @@ func GetString(v interface{}) string { | ... | @@ -20,6 +21,7 @@ func GetString(v interface{}) string { |
| 20 | } | 21 | } |
| 21 | } | 22 | } |
| 22 | 23 | ||
| 24 | // convert interface to int. | ||
| 23 | func GetInt(v interface{}) int { | 25 | func GetInt(v interface{}) int { |
| 24 | switch result := v.(type) { | 26 | switch result := v.(type) { |
| 25 | case int: | 27 | case int: |
| ... | @@ -40,6 +42,7 @@ func GetInt(v interface{}) int { | ... | @@ -40,6 +42,7 @@ func GetInt(v interface{}) int { |
| 40 | return 0 | 42 | return 0 |
| 41 | } | 43 | } |
| 42 | 44 | ||
| 45 | // convert interface to int64. | ||
| 43 | func GetInt64(v interface{}) int64 { | 46 | func GetInt64(v interface{}) int64 { |
| 44 | switch result := v.(type) { | 47 | switch result := v.(type) { |
| 45 | case int: | 48 | case int: |
| ... | @@ -60,6 +63,7 @@ func GetInt64(v interface{}) int64 { | ... | @@ -60,6 +63,7 @@ func GetInt64(v interface{}) int64 { |
| 60 | return 0 | 63 | return 0 |
| 61 | } | 64 | } |
| 62 | 65 | ||
| 66 | // convert interface to float64. | ||
| 63 | func GetFloat64(v interface{}) float64 { | 67 | func GetFloat64(v interface{}) float64 { |
| 64 | switch result := v.(type) { | 68 | switch result := v.(type) { |
| 65 | case float64: | 69 | case float64: |
| ... | @@ -76,6 +80,7 @@ func GetFloat64(v interface{}) float64 { | ... | @@ -76,6 +80,7 @@ func GetFloat64(v interface{}) float64 { |
| 76 | return 0 | 80 | return 0 |
| 77 | } | 81 | } |
| 78 | 82 | ||
| 83 | // convert interface to bool. | ||
| 79 | func GetBool(v interface{}) bool { | 84 | func GetBool(v interface{}) bool { |
| 80 | switch result := v.(type) { | 85 | switch result := v.(type) { |
| 81 | case bool: | 86 | case bool: |
| ... | @@ -92,6 +97,7 @@ func GetBool(v interface{}) bool { | ... | @@ -92,6 +97,7 @@ func GetBool(v interface{}) bool { |
| 92 | return false | 97 | return false |
| 93 | } | 98 | } |
| 94 | 99 | ||
| 100 | // convert interface to byte slice. | ||
| 95 | func getByteArray(v interface{}) []byte { | 101 | func getByteArray(v interface{}) []byte { |
| 96 | switch result := v.(type) { | 102 | switch result := v.(type) { |
| 97 | case []byte: | 103 | case []byte: | ... | ... |
| ... | @@ -24,6 +24,8 @@ func init() { | ... | @@ -24,6 +24,8 @@ func init() { |
| 24 | Register("file", NewFileCache()) | 24 | Register("file", NewFileCache()) |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | // FileCacheItem is basic unit of file cache adapter. | ||
| 28 | // it contains data and expire time. | ||
| 27 | type FileCacheItem struct { | 29 | type FileCacheItem struct { |
| 28 | Data interface{} | 30 | Data interface{} |
| 29 | Lastaccess int64 | 31 | Lastaccess int64 |
| ... | @@ -31,15 +33,13 @@ type FileCacheItem struct { | ... | @@ -31,15 +33,13 @@ type FileCacheItem struct { |
| 31 | } | 33 | } |
| 32 | 34 | ||
| 33 | var ( | 35 | var ( |
| 34 | FileCachePath string = "cache" | 36 | FileCachePath string = "cache" // cache directory |
| 35 | FileCacheFileSuffix string = ".bin" | 37 | FileCacheFileSuffix string = ".bin" // cache file suffix |
| 36 | FileCacheDirectoryLevel int = 2 | 38 | FileCacheDirectoryLevel int = 2 // cache file deep level if auto generated cache files. |
| 37 | /** | 39 | FileCacheEmbedExpiry int64 = 0 // cache expire time, default is no expire forever. |
| 38 | * 默认永不过期 | ||
| 39 | */ | ||
| 40 | FileCacheEmbedExpiry int64 = 0 | ||
| 41 | ) | 40 | ) |
| 42 | 41 | ||
| 42 | // FileCache is cache adapter for file storage. | ||
| 43 | type FileCache struct { | 43 | type FileCache struct { |
| 44 | CachePath string | 44 | CachePath string |
| 45 | FileSuffix string | 45 | FileSuffix string |
| ... | @@ -47,11 +47,14 @@ type FileCache struct { | ... | @@ -47,11 +47,14 @@ type FileCache struct { |
| 47 | EmbedExpiry int | 47 | EmbedExpiry int |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | // Create new file cache with default directory and suffix. | ||
| 51 | // the level and expiry need set in method StartAndGC as config string. | ||
| 50 | func NewFileCache() *FileCache { | 52 | func NewFileCache() *FileCache { |
| 51 | // return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix} | 53 | return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix} |
| 52 | return &FileCache{} | ||
| 53 | } | 54 | } |
| 54 | 55 | ||
| 56 | // Start and begin gc for file cache. | ||
| 57 | // the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0} | ||
| 55 | func (this *FileCache) StartAndGC(config string) error { | 58 | func (this *FileCache) StartAndGC(config string) error { |
| 56 | 59 | ||
| 57 | var cfg map[string]string | 60 | var cfg map[string]string |
| ... | @@ -78,6 +81,7 @@ func (this *FileCache) StartAndGC(config string) error { | ... | @@ -78,6 +81,7 @@ func (this *FileCache) StartAndGC(config string) error { |
| 78 | return nil | 81 | return nil |
| 79 | } | 82 | } |
| 80 | 83 | ||
| 84 | // Init will make new dir for file cache if not exist. | ||
| 81 | func (this *FileCache) Init() { | 85 | func (this *FileCache) Init() { |
| 82 | app := filepath.Dir(os.Args[0]) | 86 | app := filepath.Dir(os.Args[0]) |
| 83 | this.CachePath = filepath.Join(app, this.CachePath) | 87 | this.CachePath = filepath.Join(app, this.CachePath) |
| ... | @@ -93,6 +97,7 @@ func (this *FileCache) Init() { | ... | @@ -93,6 +97,7 @@ func (this *FileCache) Init() { |
| 93 | //fmt.Println(this.getCacheFileName("123456")); | 97 | //fmt.Println(this.getCacheFileName("123456")); |
| 94 | } | 98 | } |
| 95 | 99 | ||
| 100 | // get cached file name. it's md5 encoded. | ||
| 96 | func (this *FileCache) getCacheFileName(key string) string { | 101 | func (this *FileCache) getCacheFileName(key string) string { |
| 97 | m := md5.New() | 102 | m := md5.New() |
| 98 | io.WriteString(m, key) | 103 | io.WriteString(m, key) |
| ... | @@ -119,6 +124,8 @@ func (this *FileCache) getCacheFileName(key string) string { | ... | @@ -119,6 +124,8 @@ func (this *FileCache) getCacheFileName(key string) string { |
| 119 | return filepath.Join(cachePath, fmt.Sprintf("%s%s", keyMd5, this.FileSuffix)) | 124 | return filepath.Join(cachePath, fmt.Sprintf("%s%s", keyMd5, this.FileSuffix)) |
| 120 | } | 125 | } |
| 121 | 126 | ||
| 127 | // Get value from file cache. | ||
| 128 | // if non-exist or expired, return empty string. | ||
| 122 | func (this *FileCache) Get(key string) interface{} { | 129 | func (this *FileCache) Get(key string) interface{} { |
| 123 | filename := this.getCacheFileName(key) | 130 | filename := this.getCacheFileName(key) |
| 124 | filedata, err := File_get_contents(filename) | 131 | filedata, err := File_get_contents(filename) |
| ... | @@ -134,6 +141,8 @@ func (this *FileCache) Get(key string) interface{} { | ... | @@ -134,6 +141,8 @@ func (this *FileCache) Get(key string) interface{} { |
| 134 | return to.Data | 141 | return to.Data |
| 135 | } | 142 | } |
| 136 | 143 | ||
| 144 | // Put value into file cache. | ||
| 145 | // timeout means how long to keep this file, unit of second. | ||
| 137 | func (this *FileCache) Put(key string, val interface{}, timeout int64) error { | 146 | func (this *FileCache) Put(key string, val interface{}, timeout int64) error { |
| 138 | filename := this.getCacheFileName(key) | 147 | filename := this.getCacheFileName(key) |
| 139 | var item FileCacheItem | 148 | var item FileCacheItem |
| ... | @@ -152,6 +161,7 @@ func (this *FileCache) Put(key string, val interface{}, timeout int64) error { | ... | @@ -152,6 +161,7 @@ func (this *FileCache) Put(key string, val interface{}, timeout int64) error { |
| 152 | return err | 161 | return err |
| 153 | } | 162 | } |
| 154 | 163 | ||
| 164 | // Delete file cache value. | ||
| 155 | func (this *FileCache) Delete(key string) error { | 165 | func (this *FileCache) Delete(key string) error { |
| 156 | filename := this.getCacheFileName(key) | 166 | filename := this.getCacheFileName(key) |
| 157 | if ok, _ := exists(filename); ok { | 167 | if ok, _ := exists(filename); ok { |
| ... | @@ -160,6 +170,8 @@ func (this *FileCache) Delete(key string) error { | ... | @@ -160,6 +170,8 @@ func (this *FileCache) Delete(key string) error { |
| 160 | return nil | 170 | return nil |
| 161 | } | 171 | } |
| 162 | 172 | ||
| 173 | // Increase cached int value. | ||
| 174 | // this value is saving forever unless Delete. | ||
| 163 | func (this *FileCache) Incr(key string) error { | 175 | func (this *FileCache) Incr(key string) error { |
| 164 | data := this.Get(key) | 176 | data := this.Get(key) |
| 165 | var incr int | 177 | var incr int |
| ... | @@ -173,6 +185,7 @@ func (this *FileCache) Incr(key string) error { | ... | @@ -173,6 +185,7 @@ func (this *FileCache) Incr(key string) error { |
| 173 | return nil | 185 | return nil |
| 174 | } | 186 | } |
| 175 | 187 | ||
| 188 | // Decrease cached int value. | ||
| 176 | func (this *FileCache) Decr(key string) error { | 189 | func (this *FileCache) Decr(key string) error { |
| 177 | data := this.Get(key) | 190 | data := this.Get(key) |
| 178 | var decr int | 191 | var decr int |
| ... | @@ -185,18 +198,22 @@ func (this *FileCache) Decr(key string) error { | ... | @@ -185,18 +198,22 @@ func (this *FileCache) Decr(key string) error { |
| 185 | return nil | 198 | return nil |
| 186 | } | 199 | } |
| 187 | 200 | ||
| 201 | // Check value is exist. | ||
| 188 | func (this *FileCache) IsExist(key string) bool { | 202 | func (this *FileCache) IsExist(key string) bool { |
| 189 | filename := this.getCacheFileName(key) | 203 | filename := this.getCacheFileName(key) |
| 190 | ret, _ := exists(filename) | 204 | ret, _ := exists(filename) |
| 191 | return ret | 205 | return ret |
| 192 | } | 206 | } |
| 193 | 207 | ||
| 208 | // Clean cached files. | ||
| 209 | // not implemented. | ||
| 194 | func (this *FileCache) ClearAll() error { | 210 | func (this *FileCache) ClearAll() error { |
| 195 | //this.CachePath .递归删除 | 211 | //this.CachePath .递归删除 |
| 196 | 212 | ||
| 197 | return nil | 213 | return nil |
| 198 | } | 214 | } |
| 199 | 215 | ||
| 216 | // check file exist. | ||
| 200 | func exists(path string) (bool, error) { | 217 | func exists(path string) (bool, error) { |
| 201 | _, err := os.Stat(path) | 218 | _, err := os.Stat(path) |
| 202 | if err == nil { | 219 | if err == nil { |
| ... | @@ -208,7 +225,9 @@ func exists(path string) (bool, error) { | ... | @@ -208,7 +225,9 @@ func exists(path string) (bool, error) { |
| 208 | return false, err | 225 | return false, err |
| 209 | } | 226 | } |
| 210 | 227 | ||
| 211 | func File_get_contents(filename string) ([]byte, error) { //文件不存在时自动创建 | 228 | // Get bytes to file. |
| 229 | // if non-exist, create this file. | ||
| 230 | func File_get_contents(filename string) ([]byte, error) { | ||
| 212 | f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm) | 231 | f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm) |
| 213 | if err != nil { | 232 | if err != nil { |
| 214 | return []byte(""), err | 233 | return []byte(""), err |
| ... | @@ -226,6 +245,8 @@ func File_get_contents(filename string) ([]byte, error) { //文件不存在时 | ... | @@ -226,6 +245,8 @@ func File_get_contents(filename string) ([]byte, error) { //文件不存在时 |
| 226 | return []byte(""), err | 245 | return []byte(""), err |
| 227 | } | 246 | } |
| 228 | 247 | ||
| 248 | // Put bytes to file. | ||
| 249 | // if non-exist, create this file. | ||
| 229 | func File_put_contents(filename string, content []byte) error { | 250 | func File_put_contents(filename string, content []byte) error { |
| 230 | fp, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm) | 251 | fp, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm) |
| 231 | if err != nil { | 252 | if err != nil { |
| ... | @@ -235,6 +256,8 @@ func File_put_contents(filename string, content []byte) error { | ... | @@ -235,6 +256,8 @@ func File_put_contents(filename string, content []byte) error { |
| 235 | _, err = fp.Write(content) | 256 | _, err = fp.Write(content) |
| 236 | return err | 257 | return err |
| 237 | } | 258 | } |
| 259 | |||
| 260 | // Gob encodes file cache item. | ||
| 238 | func Gob_encode(data interface{}) ([]byte, error) { | 261 | func Gob_encode(data interface{}) ([]byte, error) { |
| 239 | buf := bytes.NewBuffer(nil) | 262 | buf := bytes.NewBuffer(nil) |
| 240 | enc := gob.NewEncoder(buf) | 263 | enc := gob.NewEncoder(buf) |
| ... | @@ -245,6 +268,7 @@ func Gob_encode(data interface{}) ([]byte, error) { | ... | @@ -245,6 +268,7 @@ func Gob_encode(data interface{}) ([]byte, error) { |
| 245 | return buf.Bytes(), err | 268 | return buf.Bytes(), err |
| 246 | } | 269 | } |
| 247 | 270 | ||
| 271 | // Gob decodes file cache item. | ||
| 248 | func Gob_decode(data []byte, to interface{}) error { | 272 | func Gob_decode(data []byte, to interface{}) error { |
| 249 | buf := bytes.NewBuffer(data) | 273 | buf := bytes.NewBuffer(data) |
| 250 | dec := gob.NewDecoder(buf) | 274 | dec := gob.NewDecoder(buf) | ... | ... |
| ... | @@ -7,15 +7,18 @@ import ( | ... | @@ -7,15 +7,18 @@ import ( |
| 7 | "github.com/beego/memcache" | 7 | "github.com/beego/memcache" |
| 8 | ) | 8 | ) |
| 9 | 9 | ||
| 10 | // Memcache adapter. | ||
| 10 | type MemcacheCache struct { | 11 | type MemcacheCache struct { |
| 11 | c *memcache.Connection | 12 | c *memcache.Connection |
| 12 | conninfo string | 13 | conninfo string |
| 13 | } | 14 | } |
| 14 | 15 | ||
| 16 | // create new memcache adapter. | ||
| 15 | func NewMemCache() *MemcacheCache { | 17 | func NewMemCache() *MemcacheCache { |
| 16 | return &MemcacheCache{} | 18 | return &MemcacheCache{} |
| 17 | } | 19 | } |
| 18 | 20 | ||
| 21 | // get value from memcache. | ||
| 19 | func (rc *MemcacheCache) Get(key string) interface{} { | 22 | func (rc *MemcacheCache) Get(key string) interface{} { |
| 20 | if rc.c == nil { | 23 | if rc.c == nil { |
| 21 | rc.c = rc.connectInit() | 24 | rc.c = rc.connectInit() |
| ... | @@ -33,6 +36,7 @@ func (rc *MemcacheCache) Get(key string) interface{} { | ... | @@ -33,6 +36,7 @@ func (rc *MemcacheCache) Get(key string) interface{} { |
| 33 | return contain | 36 | return contain |
| 34 | } | 37 | } |
| 35 | 38 | ||
| 39 | // put value to memcache. only support string. | ||
| 36 | func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { | 40 | func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { |
| 37 | if rc.c == nil { | 41 | if rc.c == nil { |
| 38 | rc.c = rc.connectInit() | 42 | rc.c = rc.connectInit() |
| ... | @@ -48,6 +52,7 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { | ... | @@ -48,6 +52,7 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { |
| 48 | return err | 52 | return err |
| 49 | } | 53 | } |
| 50 | 54 | ||
| 55 | // delete value in memcache. | ||
| 51 | func (rc *MemcacheCache) Delete(key string) error { | 56 | func (rc *MemcacheCache) Delete(key string) error { |
| 52 | if rc.c == nil { | 57 | if rc.c == nil { |
| 53 | rc.c = rc.connectInit() | 58 | rc.c = rc.connectInit() |
| ... | @@ -56,14 +61,19 @@ func (rc *MemcacheCache) Delete(key string) error { | ... | @@ -56,14 +61,19 @@ func (rc *MemcacheCache) Delete(key string) error { |
| 56 | return err | 61 | return err |
| 57 | } | 62 | } |
| 58 | 63 | ||
| 64 | // [Not Support] | ||
| 65 | // increase counter. | ||
| 59 | func (rc *MemcacheCache) Incr(key string) error { | 66 | func (rc *MemcacheCache) Incr(key string) error { |
| 60 | return errors.New("not support in memcache") | 67 | return errors.New("not support in memcache") |
| 61 | } | 68 | } |
| 62 | 69 | ||
| 70 | // [Not Support] | ||
| 71 | // decrease counter. | ||
| 63 | func (rc *MemcacheCache) Decr(key string) error { | 72 | func (rc *MemcacheCache) Decr(key string) error { |
| 64 | return errors.New("not support in memcache") | 73 | return errors.New("not support in memcache") |
| 65 | } | 74 | } |
| 66 | 75 | ||
| 76 | // check value exists in memcache. | ||
| 67 | func (rc *MemcacheCache) IsExist(key string) bool { | 77 | func (rc *MemcacheCache) IsExist(key string) bool { |
| 68 | if rc.c == nil { | 78 | if rc.c == nil { |
| 69 | rc.c = rc.connectInit() | 79 | rc.c = rc.connectInit() |
| ... | @@ -80,6 +90,7 @@ func (rc *MemcacheCache) IsExist(key string) bool { | ... | @@ -80,6 +90,7 @@ func (rc *MemcacheCache) IsExist(key string) bool { |
| 80 | return true | 90 | return true |
| 81 | } | 91 | } |
| 82 | 92 | ||
| 93 | // clear all cached in memcache. | ||
| 83 | func (rc *MemcacheCache) ClearAll() error { | 94 | func (rc *MemcacheCache) ClearAll() error { |
| 84 | if rc.c == nil { | 95 | if rc.c == nil { |
| 85 | rc.c = rc.connectInit() | 96 | rc.c = rc.connectInit() |
| ... | @@ -88,6 +99,9 @@ func (rc *MemcacheCache) ClearAll() error { | ... | @@ -88,6 +99,9 @@ func (rc *MemcacheCache) ClearAll() error { |
| 88 | return err | 99 | return err |
| 89 | } | 100 | } |
| 90 | 101 | ||
| 102 | // start memcache adapter. | ||
| 103 | // config string is like {"conn":"connection info"}. | ||
| 104 | // if connecting error, return. | ||
| 91 | func (rc *MemcacheCache) StartAndGC(config string) error { | 105 | func (rc *MemcacheCache) StartAndGC(config string) error { |
| 92 | var cf map[string]string | 106 | var cf map[string]string |
| 93 | json.Unmarshal([]byte(config), &cf) | 107 | json.Unmarshal([]byte(config), &cf) |
| ... | @@ -102,6 +116,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error { | ... | @@ -102,6 +116,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error { |
| 102 | return nil | 116 | return nil |
| 103 | } | 117 | } |
| 104 | 118 | ||
| 119 | // connect to memcache and keep the connection. | ||
| 105 | func (rc *MemcacheCache) connectInit() *memcache.Connection { | 120 | func (rc *MemcacheCache) connectInit() *memcache.Connection { |
| 106 | c, err := memcache.Connect(rc.conninfo) | 121 | c, err := memcache.Connect(rc.conninfo) |
| 107 | if err != nil { | 122 | if err != nil { | ... | ... |
| ... | @@ -9,28 +9,34 @@ import ( | ... | @@ -9,28 +9,34 @@ import ( |
| 9 | ) | 9 | ) |
| 10 | 10 | ||
| 11 | var ( | 11 | var ( |
| 12 | // clock time of recycling the expired cache items in memory. | ||
| 12 | DefaultEvery int = 60 // 1 minute | 13 | DefaultEvery int = 60 // 1 minute |
| 13 | ) | 14 | ) |
| 14 | 15 | ||
| 16 | // Memory cache item. | ||
| 15 | type MemoryItem struct { | 17 | type MemoryItem struct { |
| 16 | val interface{} | 18 | val interface{} |
| 17 | Lastaccess time.Time | 19 | Lastaccess time.Time |
| 18 | expired int64 | 20 | expired int64 |
| 19 | } | 21 | } |
| 20 | 22 | ||
| 23 | // Memory cache adapter. | ||
| 24 | // it contains a RW locker for safe map storage. | ||
| 21 | type MemoryCache struct { | 25 | type MemoryCache struct { |
| 22 | lock sync.RWMutex | 26 | lock sync.RWMutex |
| 23 | dur time.Duration | 27 | dur time.Duration |
| 24 | items map[string]*MemoryItem | 28 | items map[string]*MemoryItem |
| 25 | Every int // Run an expiration check Every seconds | 29 | Every int // run an expiration check Every cloc; time |
| 26 | } | 30 | } |
| 27 | 31 | ||
| 28 | // NewDefaultCache returns a new FileCache with sane defaults. | 32 | // NewMemoryCache returns a new MemoryCache. |
| 29 | func NewMemoryCache() *MemoryCache { | 33 | func NewMemoryCache() *MemoryCache { |
| 30 | cache := MemoryCache{items: make(map[string]*MemoryItem)} | 34 | cache := MemoryCache{items: make(map[string]*MemoryItem)} |
| 31 | return &cache | 35 | return &cache |
| 32 | } | 36 | } |
| 33 | 37 | ||
| 38 | // Get cache from memory. | ||
| 39 | // if non-existed or expired, return nil. | ||
| 34 | func (bc *MemoryCache) Get(name string) interface{} { | 40 | func (bc *MemoryCache) Get(name string) interface{} { |
| 35 | bc.lock.RLock() | 41 | bc.lock.RLock() |
| 36 | defer bc.lock.RUnlock() | 42 | defer bc.lock.RUnlock() |
| ... | @@ -45,6 +51,7 @@ func (bc *MemoryCache) Get(name string) interface{} { | ... | @@ -45,6 +51,7 @@ func (bc *MemoryCache) Get(name string) interface{} { |
| 45 | return itm.val | 51 | return itm.val |
| 46 | } | 52 | } |
| 47 | 53 | ||
| 54 | // Put cache to memory. | ||
| 48 | func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error { | 55 | func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error { |
| 49 | bc.lock.Lock() | 56 | bc.lock.Lock() |
| 50 | defer bc.lock.Unlock() | 57 | defer bc.lock.Unlock() |
| ... | @@ -57,6 +64,7 @@ func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error | ... | @@ -57,6 +64,7 @@ func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error |
| 57 | return nil | 64 | return nil |
| 58 | } | 65 | } |
| 59 | 66 | ||
| 67 | /// Delete cache in memory. | ||
| 60 | func (bc *MemoryCache) Delete(name string) error { | 68 | func (bc *MemoryCache) Delete(name string) error { |
| 61 | bc.lock.Lock() | 69 | bc.lock.Lock() |
| 62 | defer bc.lock.Unlock() | 70 | defer bc.lock.Unlock() |
| ... | @@ -71,6 +79,8 @@ func (bc *MemoryCache) Delete(name string) error { | ... | @@ -71,6 +79,8 @@ func (bc *MemoryCache) Delete(name string) error { |
| 71 | return nil | 79 | return nil |
| 72 | } | 80 | } |
| 73 | 81 | ||
| 82 | // Increase cache counter in memory. | ||
| 83 | // it supports int,int64,int32,uint,uint64,uint32. | ||
| 74 | func (bc *MemoryCache) Incr(key string) error { | 84 | func (bc *MemoryCache) Incr(key string) error { |
| 75 | bc.lock.RLock() | 85 | bc.lock.RLock() |
| 76 | defer bc.lock.RUnlock() | 86 | defer bc.lock.RUnlock() |
| ... | @@ -97,6 +107,7 @@ func (bc *MemoryCache) Incr(key string) error { | ... | @@ -97,6 +107,7 @@ func (bc *MemoryCache) Incr(key string) error { |
| 97 | return nil | 107 | return nil |
| 98 | } | 108 | } |
| 99 | 109 | ||
| 110 | // Decrease counter in memory. | ||
| 100 | func (bc *MemoryCache) Decr(key string) error { | 111 | func (bc *MemoryCache) Decr(key string) error { |
| 101 | bc.lock.RLock() | 112 | bc.lock.RLock() |
| 102 | defer bc.lock.RUnlock() | 113 | defer bc.lock.RUnlock() |
| ... | @@ -135,6 +146,7 @@ func (bc *MemoryCache) Decr(key string) error { | ... | @@ -135,6 +146,7 @@ func (bc *MemoryCache) Decr(key string) error { |
| 135 | return nil | 146 | return nil |
| 136 | } | 147 | } |
| 137 | 148 | ||
| 149 | // check cache exist in memory. | ||
| 138 | func (bc *MemoryCache) IsExist(name string) bool { | 150 | func (bc *MemoryCache) IsExist(name string) bool { |
| 139 | bc.lock.RLock() | 151 | bc.lock.RLock() |
| 140 | defer bc.lock.RUnlock() | 152 | defer bc.lock.RUnlock() |
| ... | @@ -142,6 +154,7 @@ func (bc *MemoryCache) IsExist(name string) bool { | ... | @@ -142,6 +154,7 @@ func (bc *MemoryCache) IsExist(name string) bool { |
| 142 | return ok | 154 | return ok |
| 143 | } | 155 | } |
| 144 | 156 | ||
| 157 | // delete all cache in memory. | ||
| 145 | func (bc *MemoryCache) ClearAll() error { | 158 | func (bc *MemoryCache) ClearAll() error { |
| 146 | bc.lock.Lock() | 159 | bc.lock.Lock() |
| 147 | defer bc.lock.Unlock() | 160 | defer bc.lock.Unlock() |
| ... | @@ -149,7 +162,7 @@ func (bc *MemoryCache) ClearAll() error { | ... | @@ -149,7 +162,7 @@ func (bc *MemoryCache) ClearAll() error { |
| 149 | return nil | 162 | return nil |
| 150 | } | 163 | } |
| 151 | 164 | ||
| 152 | // Start activates the file cache; it will | 165 | // start memory cache. it will check expiration in every clock time. |
| 153 | func (bc *MemoryCache) StartAndGC(config string) error { | 166 | func (bc *MemoryCache) StartAndGC(config string) error { |
| 154 | var cf map[string]int | 167 | var cf map[string]int |
| 155 | json.Unmarshal([]byte(config), &cf) | 168 | json.Unmarshal([]byte(config), &cf) |
| ... | @@ -167,6 +180,7 @@ func (bc *MemoryCache) StartAndGC(config string) error { | ... | @@ -167,6 +180,7 @@ func (bc *MemoryCache) StartAndGC(config string) error { |
| 167 | return nil | 180 | return nil |
| 168 | } | 181 | } |
| 169 | 182 | ||
| 183 | // check expiration. | ||
| 170 | func (bc *MemoryCache) vaccuum() { | 184 | func (bc *MemoryCache) vaccuum() { |
| 171 | if bc.Every < 1 { | 185 | if bc.Every < 1 { |
| 172 | return | 186 | return | ... | ... |
| ... | @@ -8,19 +8,23 @@ import ( | ... | @@ -8,19 +8,23 @@ import ( |
| 8 | ) | 8 | ) |
| 9 | 9 | ||
| 10 | var ( | 10 | var ( |
| 11 | // the collection name of redis for cache adapter. | ||
| 11 | DefaultKey string = "beecacheRedis" | 12 | DefaultKey string = "beecacheRedis" |
| 12 | ) | 13 | ) |
| 13 | 14 | ||
| 15 | // Redis cache adapter. | ||
| 14 | type RedisCache struct { | 16 | type RedisCache struct { |
| 15 | c redis.Conn | 17 | c redis.Conn |
| 16 | conninfo string | 18 | conninfo string |
| 17 | key string | 19 | key string |
| 18 | } | 20 | } |
| 19 | 21 | ||
| 22 | // create new redis cache with default collection name. | ||
| 20 | func NewRedisCache() *RedisCache { | 23 | func NewRedisCache() *RedisCache { |
| 21 | return &RedisCache{key: DefaultKey} | 24 | return &RedisCache{key: DefaultKey} |
| 22 | } | 25 | } |
| 23 | 26 | ||
| 27 | // Get cache from redis. | ||
| 24 | func (rc *RedisCache) Get(key string) interface{} { | 28 | func (rc *RedisCache) Get(key string) interface{} { |
| 25 | if rc.c == nil { | 29 | if rc.c == nil { |
| 26 | var err error | 30 | var err error |
| ... | @@ -36,6 +40,8 @@ func (rc *RedisCache) Get(key string) interface{} { | ... | @@ -36,6 +40,8 @@ func (rc *RedisCache) Get(key string) interface{} { |
| 36 | return v | 40 | return v |
| 37 | } | 41 | } |
| 38 | 42 | ||
| 43 | // put cache to redis. | ||
| 44 | // timeout is ignored. | ||
| 39 | func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error { | 45 | func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error { |
| 40 | if rc.c == nil { | 46 | if rc.c == nil { |
| 41 | var err error | 47 | var err error |
| ... | @@ -48,6 +54,7 @@ func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error { | ... | @@ -48,6 +54,7 @@ func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error { |
| 48 | return err | 54 | return err |
| 49 | } | 55 | } |
| 50 | 56 | ||
| 57 | // delete cache in redis. | ||
| 51 | func (rc *RedisCache) Delete(key string) error { | 58 | func (rc *RedisCache) Delete(key string) error { |
| 52 | if rc.c == nil { | 59 | if rc.c == nil { |
| 53 | var err error | 60 | var err error |
| ... | @@ -60,6 +67,7 @@ func (rc *RedisCache) Delete(key string) error { | ... | @@ -60,6 +67,7 @@ func (rc *RedisCache) Delete(key string) error { |
| 60 | return err | 67 | return err |
| 61 | } | 68 | } |
| 62 | 69 | ||
| 70 | // check cache exist in redis. | ||
| 63 | func (rc *RedisCache) IsExist(key string) bool { | 71 | func (rc *RedisCache) IsExist(key string) bool { |
| 64 | if rc.c == nil { | 72 | if rc.c == nil { |
| 65 | var err error | 73 | var err error |
| ... | @@ -75,6 +83,7 @@ func (rc *RedisCache) IsExist(key string) bool { | ... | @@ -75,6 +83,7 @@ func (rc *RedisCache) IsExist(key string) bool { |
| 75 | return v | 83 | return v |
| 76 | } | 84 | } |
| 77 | 85 | ||
| 86 | // increase counter in redis. | ||
| 78 | func (rc *RedisCache) Incr(key string) error { | 87 | func (rc *RedisCache) Incr(key string) error { |
| 79 | if rc.c == nil { | 88 | if rc.c == nil { |
| 80 | var err error | 89 | var err error |
| ... | @@ -90,6 +99,7 @@ func (rc *RedisCache) Incr(key string) error { | ... | @@ -90,6 +99,7 @@ func (rc *RedisCache) Incr(key string) error { |
| 90 | return nil | 99 | return nil |
| 91 | } | 100 | } |
| 92 | 101 | ||
| 102 | // decrease counter in redis. | ||
| 93 | func (rc *RedisCache) Decr(key string) error { | 103 | func (rc *RedisCache) Decr(key string) error { |
| 94 | if rc.c == nil { | 104 | if rc.c == nil { |
| 95 | var err error | 105 | var err error |
| ... | @@ -105,6 +115,7 @@ func (rc *RedisCache) Decr(key string) error { | ... | @@ -105,6 +115,7 @@ func (rc *RedisCache) Decr(key string) error { |
| 105 | return nil | 115 | return nil |
| 106 | } | 116 | } |
| 107 | 117 | ||
| 118 | // clean all cache in redis. delete this redis collection. | ||
| 108 | func (rc *RedisCache) ClearAll() error { | 119 | func (rc *RedisCache) ClearAll() error { |
| 109 | if rc.c == nil { | 120 | if rc.c == nil { |
| 110 | var err error | 121 | var err error |
| ... | @@ -117,6 +128,10 @@ func (rc *RedisCache) ClearAll() error { | ... | @@ -117,6 +128,10 @@ func (rc *RedisCache) ClearAll() error { |
| 117 | return err | 128 | return err |
| 118 | } | 129 | } |
| 119 | 130 | ||
| 131 | // start redis cache adapter. | ||
| 132 | // config is like {"key":"collection key","conn":"connection info"} | ||
| 133 | // the cache item in redis are stored forever, | ||
| 134 | // so no gc operation. | ||
| 120 | func (rc *RedisCache) StartAndGC(config string) error { | 135 | func (rc *RedisCache) StartAndGC(config string) error { |
| 121 | var cf map[string]string | 136 | var cf map[string]string |
| 122 | json.Unmarshal([]byte(config), &cf) | 137 | json.Unmarshal([]byte(config), &cf) |
| ... | @@ -139,6 +154,7 @@ func (rc *RedisCache) StartAndGC(config string) error { | ... | @@ -139,6 +154,7 @@ func (rc *RedisCache) StartAndGC(config string) error { |
| 139 | return nil | 154 | return nil |
| 140 | } | 155 | } |
| 141 | 156 | ||
| 157 | // connect to redis. | ||
| 142 | func (rc *RedisCache) connectInit() (redis.Conn, error) { | 158 | func (rc *RedisCache) connectInit() (redis.Conn, error) { |
| 143 | c, err := redis.Dial("tcp", rc.conninfo) | 159 | c, err := redis.Dial("tcp", rc.conninfo) |
| 144 | if err != nil { | 160 | if err != nil { | ... | ... |
| ... | @@ -241,7 +241,7 @@ func (c *Controller) ServeJson(encoding ...bool) { | ... | @@ -241,7 +241,7 @@ func (c *Controller) ServeJson(encoding ...bool) { |
| 241 | c.Ctx.Output.Json(c.Data["json"], hasIndent, hasencoding) | 241 | c.Ctx.Output.Json(c.Data["json"], hasIndent, hasencoding) |
| 242 | } | 242 | } |
| 243 | 243 | ||
| 244 | // ServeJson sends a jsonp response. | 244 | // ServeJsonp sends a jsonp response. |
| 245 | func (c *Controller) ServeJsonp() { | 245 | func (c *Controller) ServeJsonp() { |
| 246 | var hasIndent bool | 246 | var hasIndent bool |
| 247 | if RunMode == "prod" { | 247 | if RunMode == "prod" { |
| ... | @@ -252,7 +252,7 @@ func (c *Controller) ServeJsonp() { | ... | @@ -252,7 +252,7 @@ func (c *Controller) ServeJsonp() { |
| 252 | c.Ctx.Output.Jsonp(c.Data["jsonp"], hasIndent) | 252 | c.Ctx.Output.Jsonp(c.Data["jsonp"], hasIndent) |
| 253 | } | 253 | } |
| 254 | 254 | ||
| 255 | // ServeJson sends xml response. | 255 | // ServeXml sends xml response. |
| 256 | func (c *Controller) ServeXml() { | 256 | func (c *Controller) ServeXml() { |
| 257 | var hasIndent bool | 257 | var hasIndent bool |
| 258 | if RunMode == "prod" { | 258 | if RunMode == "prod" { | ... | ... |
| ... | @@ -17,7 +17,7 @@ import ( | ... | @@ -17,7 +17,7 @@ import ( |
| 17 | 17 | ||
| 18 | var ( | 18 | var ( |
| 19 | beegoTplFuncMap template.FuncMap | 19 | beegoTplFuncMap template.FuncMap |
| 20 | // beego template caching map ans supported template file extensions. | 20 | // beego template caching map and supported template file extensions. |
| 21 | BeeTemplates map[string]*template.Template | 21 | BeeTemplates map[string]*template.Template |
| 22 | BeeTemplateExt []string | 22 | BeeTemplateExt []string |
| 23 | ) | 23 | ) |
| ... | @@ -89,7 +89,7 @@ func (self *templatefile) visit(paths string, f os.FileInfo, err error) error { | ... | @@ -89,7 +89,7 @@ func (self *templatefile) visit(paths string, f os.FileInfo, err error) error { |
| 89 | return nil | 89 | return nil |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | // return this path has supported template extension of beego or not. | 92 | // return this path contains supported template extension of beego or not. |
| 93 | func HasTemplateExt(paths string) bool { | 93 | func HasTemplateExt(paths string) bool { |
| 94 | for _, v := range BeeTemplateExt { | 94 | for _, v := range BeeTemplateExt { |
| 95 | if strings.HasSuffix(paths, "."+v) { | 95 | if strings.HasSuffix(paths, "."+v) { | ... | ... |
| ... | @@ -203,7 +203,7 @@ func AssetsJs(src string) template.HTML { | ... | @@ -203,7 +203,7 @@ func AssetsJs(src string) template.HTML { |
| 203 | return template.HTML(text) | 203 | return template.HTML(text) |
| 204 | } | 204 | } |
| 205 | 205 | ||
| 206 | // returns stylesheet link tag with str string. | 206 | // returns stylesheet link tag with src string. |
| 207 | func AssetsCss(src string) template.HTML { | 207 | func AssetsCss(src string) template.HTML { |
| 208 | text := string(src) | 208 | text := string(src) |
| 209 | 209 | ... | ... |
-
Please register or sign in to post a comment