mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 18:30:56 +00:00
golint cache package
This commit is contained in:
parent
62e528ca4c
commit
d7aaf2ebeb
3
cache/cache.go
vendored
3
cache/cache.go
vendored
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package cache provide a Cache interface and some implemetn engine
|
||||||
// Usage:
|
// Usage:
|
||||||
//
|
//
|
||||||
// import(
|
// import(
|
||||||
@ -80,7 +81,7 @@ func Register(name string, adapter Cache) {
|
|||||||
adapters[name] = adapter
|
adapters[name] = adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new cache driver by adapter name and config string.
|
// NewCache Create a new cache driver by adapter name and config string.
|
||||||
// config need to be correct JSON as string: {"interval":360}.
|
// config need to be correct JSON as string: {"interval":360}.
|
||||||
// it will start gc automatically.
|
// it will start gc automatically.
|
||||||
func NewCache(adapterName, config string) (adapter Cache, err error) {
|
func NewCache(adapterName, config string) (adapter Cache, err error) {
|
||||||
|
12
cache/conv.go
vendored
12
cache/conv.go
vendored
@ -19,7 +19,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// convert interface to string.
|
// GetString convert interface to string.
|
||||||
func GetString(v interface{}) string {
|
func GetString(v interface{}) string {
|
||||||
switch result := v.(type) {
|
switch result := v.(type) {
|
||||||
case string:
|
case string:
|
||||||
@ -34,7 +34,7 @@ func GetString(v interface{}) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert interface to int.
|
// GetInt convert interface to int.
|
||||||
func GetInt(v interface{}) int {
|
func GetInt(v interface{}) int {
|
||||||
switch result := v.(type) {
|
switch result := v.(type) {
|
||||||
case int:
|
case int:
|
||||||
@ -52,7 +52,7 @@ func GetInt(v interface{}) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert interface to int64.
|
// GetInt64 convert interface to int64.
|
||||||
func GetInt64(v interface{}) int64 {
|
func GetInt64(v interface{}) int64 {
|
||||||
switch result := v.(type) {
|
switch result := v.(type) {
|
||||||
case int:
|
case int:
|
||||||
@ -71,7 +71,7 @@ func GetInt64(v interface{}) int64 {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert interface to float64.
|
// GetFloat64 convert interface to float64.
|
||||||
func GetFloat64(v interface{}) float64 {
|
func GetFloat64(v interface{}) float64 {
|
||||||
switch result := v.(type) {
|
switch result := v.(type) {
|
||||||
case float64:
|
case float64:
|
||||||
@ -85,7 +85,7 @@ func GetFloat64(v interface{}) float64 {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert interface to bool.
|
// GetBool convert interface to bool.
|
||||||
func GetBool(v interface{}) bool {
|
func GetBool(v interface{}) bool {
|
||||||
switch result := v.(type) {
|
switch result := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@ -99,7 +99,7 @@ func GetBool(v interface{}) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert interface to byte slice.
|
// getByteArray convert interface to byte slice.
|
||||||
func getByteArray(v interface{}) []byte {
|
func getByteArray(v interface{}) []byte {
|
||||||
switch result := v.(type) {
|
switch result := v.(type) {
|
||||||
case []byte:
|
case []byte:
|
||||||
|
14
cache/conv_test.go
vendored
14
cache/conv_test.go
vendored
@ -27,7 +27,7 @@ func TestGetString(t *testing.T) {
|
|||||||
if "test2" != GetString(t2) {
|
if "test2" != GetString(t2) {
|
||||||
t.Error("get string from byte array error")
|
t.Error("get string from byte array error")
|
||||||
}
|
}
|
||||||
var t3 int = 1
|
var t3 = 1
|
||||||
if "1" != GetString(t3) {
|
if "1" != GetString(t3) {
|
||||||
t.Error("get string from int error")
|
t.Error("get string from int error")
|
||||||
}
|
}
|
||||||
@ -35,7 +35,7 @@ func TestGetString(t *testing.T) {
|
|||||||
if "1" != GetString(t4) {
|
if "1" != GetString(t4) {
|
||||||
t.Error("get string from int64 error")
|
t.Error("get string from int64 error")
|
||||||
}
|
}
|
||||||
var t5 float64 = 1.1
|
var t5 = 1.1
|
||||||
if "1.1" != GetString(t5) {
|
if "1.1" != GetString(t5) {
|
||||||
t.Error("get string from float64 error")
|
t.Error("get string from float64 error")
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@ func TestGetString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetInt(t *testing.T) {
|
func TestGetInt(t *testing.T) {
|
||||||
var t1 int = 1
|
var t1 = 1
|
||||||
if 1 != GetInt(t1) {
|
if 1 != GetInt(t1) {
|
||||||
t.Error("get int from int error")
|
t.Error("get int from int error")
|
||||||
}
|
}
|
||||||
@ -69,7 +69,7 @@ func TestGetInt(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetInt64(t *testing.T) {
|
func TestGetInt64(t *testing.T) {
|
||||||
var i int64 = 1
|
var i int64 = 1
|
||||||
var t1 int = 1
|
var t1 = 1
|
||||||
if i != GetInt64(t1) {
|
if i != GetInt64(t1) {
|
||||||
t.Error("get int64 from int error")
|
t.Error("get int64 from int error")
|
||||||
}
|
}
|
||||||
@ -91,12 +91,12 @@ func TestGetInt64(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetFloat64(t *testing.T) {
|
func TestGetFloat64(t *testing.T) {
|
||||||
var f float64 = 1.11
|
var f = 1.11
|
||||||
var t1 float32 = 1.11
|
var t1 float32 = 1.11
|
||||||
if f != GetFloat64(t1) {
|
if f != GetFloat64(t1) {
|
||||||
t.Error("get float64 from float32 error")
|
t.Error("get float64 from float32 error")
|
||||||
}
|
}
|
||||||
var t2 float64 = 1.11
|
var t2 = 1.11
|
||||||
if f != GetFloat64(t2) {
|
if f != GetFloat64(t2) {
|
||||||
t.Error("get float64 from float64 error")
|
t.Error("get float64 from float64 error")
|
||||||
}
|
}
|
||||||
@ -106,7 +106,7 @@ func TestGetFloat64(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var f2 float64 = 1
|
var f2 float64 = 1
|
||||||
var t4 int = 1
|
var t4 = 1
|
||||||
if f2 != GetFloat64(t4) {
|
if f2 != GetFloat64(t4) {
|
||||||
t.Error("get float64 from int error")
|
t.Error("get float64 from int error")
|
||||||
}
|
}
|
||||||
|
45
cache/file.go
vendored
45
cache/file.go
vendored
@ -41,11 +41,12 @@ type FileCacheItem struct {
|
|||||||
Expired int64
|
Expired int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FileCache Config
|
||||||
var (
|
var (
|
||||||
FileCachePath string = "cache" // cache directory
|
FileCachePath = "cache" // cache directory
|
||||||
FileCacheFileSuffix string = ".bin" // cache file suffix
|
FileCacheFileSuffix = ".bin" // cache file suffix
|
||||||
FileCacheDirectoryLevel int = 2 // cache file deep level if auto generated cache files.
|
FileCacheDirectoryLevel = 2 // cache file deep level if auto generated cache files.
|
||||||
FileCacheEmbedExpiry int64 = 0 // cache expire time, default is no expire forever.
|
FileCacheEmbedExpiry int64 // cache expire time, default is no expire forever.
|
||||||
)
|
)
|
||||||
|
|
||||||
// FileCache is cache adapter for file storage.
|
// FileCache is cache adapter for file storage.
|
||||||
@ -56,14 +57,14 @@ type FileCache struct {
|
|||||||
EmbedExpiry int
|
EmbedExpiry int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new file cache with no config.
|
// NewFileCache Create new file cache with no config.
|
||||||
// the level and expiry need set in method StartAndGC as config string.
|
// the level and expiry need set in method StartAndGC as config string.
|
||||||
func NewFileCache() *FileCache {
|
func NewFileCache() *FileCache {
|
||||||
// return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix}
|
// return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix}
|
||||||
return &FileCache{}
|
return &FileCache{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start and begin gc for file cache.
|
// StartAndGC will start and begin gc for file cache.
|
||||||
// the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}
|
// the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}
|
||||||
func (fc *FileCache) StartAndGC(config string) error {
|
func (fc *FileCache) StartAndGC(config string) error {
|
||||||
|
|
||||||
@ -120,12 +121,12 @@ func (fc *FileCache) getCacheFileName(key string) string {
|
|||||||
// Get value from file cache.
|
// Get value from file cache.
|
||||||
// if non-exist or expired, return empty string.
|
// if non-exist or expired, return empty string.
|
||||||
func (fc *FileCache) Get(key string) interface{} {
|
func (fc *FileCache) Get(key string) interface{} {
|
||||||
fileData, err := File_get_contents(fc.getCacheFileName(key))
|
fileData, err := FileGetContents(fc.getCacheFileName(key))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
var to FileCacheItem
|
var to FileCacheItem
|
||||||
Gob_decode(fileData, &to)
|
GobDecode(fileData, &to)
|
||||||
if to.Expired < time.Now().Unix() {
|
if to.Expired < time.Now().Unix() {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -155,11 +156,11 @@ func (fc *FileCache) Put(key string, val interface{}, timeout int64) error {
|
|||||||
item.Expired = time.Now().Unix() + timeout
|
item.Expired = time.Now().Unix() + timeout
|
||||||
}
|
}
|
||||||
item.Lastaccess = time.Now().Unix()
|
item.Lastaccess = time.Now().Unix()
|
||||||
data, err := Gob_encode(item)
|
data, err := GobEncode(item)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return File_put_contents(fc.getCacheFileName(key), data)
|
return FilePutContents(fc.getCacheFileName(key), data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete file cache value.
|
// Delete file cache value.
|
||||||
@ -171,7 +172,7 @@ func (fc *FileCache) Delete(key string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Increase cached int value.
|
// Incr will increase cached int value.
|
||||||
// fc value is saving forever unless Delete.
|
// fc value is saving forever unless Delete.
|
||||||
func (fc *FileCache) Incr(key string) error {
|
func (fc *FileCache) Incr(key string) error {
|
||||||
data := fc.Get(key)
|
data := fc.Get(key)
|
||||||
@ -185,7 +186,7 @@ func (fc *FileCache) Incr(key string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decrease cached int value.
|
// Decr will decrease cached int value.
|
||||||
func (fc *FileCache) Decr(key string) error {
|
func (fc *FileCache) Decr(key string) error {
|
||||||
data := fc.Get(key)
|
data := fc.Get(key)
|
||||||
var decr int
|
var decr int
|
||||||
@ -198,13 +199,13 @@ func (fc *FileCache) Decr(key string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check value is exist.
|
// IsExist check value is exist.
|
||||||
func (fc *FileCache) IsExist(key string) bool {
|
func (fc *FileCache) IsExist(key string) bool {
|
||||||
ret, _ := exists(fc.getCacheFileName(key))
|
ret, _ := exists(fc.getCacheFileName(key))
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean cached files.
|
// ClearAll will clean cached files.
|
||||||
// not implemented.
|
// not implemented.
|
||||||
func (fc *FileCache) ClearAll() error {
|
func (fc *FileCache) ClearAll() error {
|
||||||
return nil
|
return nil
|
||||||
@ -222,9 +223,9 @@ func exists(path string) (bool, error) {
|
|||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get bytes to file.
|
// FileGetContents Get bytes to file.
|
||||||
// if non-exist, create this file.
|
// if non-exist, create this file.
|
||||||
func File_get_contents(filename string) (data []byte, e error) {
|
func FileGetContents(filename string) (data []byte, e error) {
|
||||||
f, e := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
|
f, e := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return
|
return
|
||||||
@ -242,9 +243,9 @@ func File_get_contents(filename string) (data []byte, e error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put bytes to file.
|
// FilePutContents Put bytes to file.
|
||||||
// if non-exist, create this file.
|
// if non-exist, create this file.
|
||||||
func File_put_contents(filename string, content []byte) error {
|
func FilePutContents(filename string, content []byte) error {
|
||||||
fp, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
|
fp, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -254,8 +255,8 @@ func File_put_contents(filename string, content []byte) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gob encodes file cache item.
|
// GobEncode Gob encodes file cache item.
|
||||||
func Gob_encode(data interface{}) ([]byte, error) {
|
func GobEncode(data interface{}) ([]byte, error) {
|
||||||
buf := bytes.NewBuffer(nil)
|
buf := bytes.NewBuffer(nil)
|
||||||
enc := gob.NewEncoder(buf)
|
enc := gob.NewEncoder(buf)
|
||||||
err := enc.Encode(data)
|
err := enc.Encode(data)
|
||||||
@ -265,8 +266,8 @@ func Gob_encode(data interface{}) ([]byte, error) {
|
|||||||
return buf.Bytes(), err
|
return buf.Bytes(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gob decodes file cache item.
|
// GobDecode Gob decodes file cache item.
|
||||||
func Gob_decode(data []byte, to *FileCacheItem) error {
|
func GobDecode(data []byte, to *FileCacheItem) error {
|
||||||
buf := bytes.NewBuffer(data)
|
buf := bytes.NewBuffer(data)
|
||||||
dec := gob.NewDecoder(buf)
|
dec := gob.NewDecoder(buf)
|
||||||
return dec.Decode(&to)
|
return dec.Decode(&to)
|
||||||
|
59
cache/memcache/memcache.go
vendored
59
cache/memcache/memcache.go
vendored
@ -12,7 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
// package memcahe for cache provider
|
// Package memcache for cache provider
|
||||||
//
|
//
|
||||||
// depend on github.com/bradfitz/gomemcache/memcache
|
// depend on github.com/bradfitz/gomemcache/memcache
|
||||||
//
|
//
|
||||||
@ -39,19 +39,19 @@ import (
|
|||||||
"github.com/astaxie/beego/cache"
|
"github.com/astaxie/beego/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Memcache adapter.
|
// Cache Memcache adapter.
|
||||||
type MemcacheCache struct {
|
type Cache struct {
|
||||||
conn *memcache.Client
|
conn *memcache.Client
|
||||||
conninfo []string
|
conninfo []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// create new memcache adapter.
|
// NewMemCache create new memcache adapter.
|
||||||
func NewMemCache() *MemcacheCache {
|
func NewMemCache() *Cache {
|
||||||
return &MemcacheCache{}
|
return &Cache{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get value from memcache.
|
// Get get value from memcache.
|
||||||
func (rc *MemcacheCache) Get(key string) interface{} {
|
func (rc *Cache) Get(key string) interface{} {
|
||||||
if rc.conn == nil {
|
if rc.conn == nil {
|
||||||
if err := rc.connectInit(); err != nil {
|
if err := rc.connectInit(); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -63,8 +63,8 @@ func (rc *MemcacheCache) Get(key string) interface{} {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// get value from memcache.
|
// GetMulti get value from memcache.
|
||||||
func (rc *MemcacheCache) GetMulti(keys []string) []interface{} {
|
func (rc *Cache) GetMulti(keys []string) []interface{} {
|
||||||
size := len(keys)
|
size := len(keys)
|
||||||
var rv []interface{}
|
var rv []interface{}
|
||||||
if rc.conn == nil {
|
if rc.conn == nil {
|
||||||
@ -81,16 +81,15 @@ func (rc *MemcacheCache) GetMulti(keys []string) []interface{} {
|
|||||||
rv = append(rv, string(v.Value))
|
rv = append(rv, string(v.Value))
|
||||||
}
|
}
|
||||||
return rv
|
return rv
|
||||||
} else {
|
|
||||||
for i := 0; i < size; i++ {
|
|
||||||
rv = append(rv, err)
|
|
||||||
}
|
|
||||||
return rv
|
|
||||||
}
|
}
|
||||||
|
for i := 0; i < size; i++ {
|
||||||
|
rv = append(rv, err)
|
||||||
|
}
|
||||||
|
return rv
|
||||||
}
|
}
|
||||||
|
|
||||||
// put value to memcache. only support string.
|
// Put put value to memcache. only support string.
|
||||||
func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
|
func (rc *Cache) Put(key string, val interface{}, timeout int64) error {
|
||||||
if rc.conn == nil {
|
if rc.conn == nil {
|
||||||
if err := rc.connectInit(); err != nil {
|
if err := rc.connectInit(); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -104,8 +103,8 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
|
|||||||
return rc.conn.Set(&item)
|
return rc.conn.Set(&item)
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete value in memcache.
|
// Delete delete value in memcache.
|
||||||
func (rc *MemcacheCache) Delete(key string) error {
|
func (rc *Cache) Delete(key string) error {
|
||||||
if rc.conn == nil {
|
if rc.conn == nil {
|
||||||
if err := rc.connectInit(); err != nil {
|
if err := rc.connectInit(); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -114,8 +113,8 @@ func (rc *MemcacheCache) Delete(key string) error {
|
|||||||
return rc.conn.Delete(key)
|
return rc.conn.Delete(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// increase counter.
|
// Incr increase counter.
|
||||||
func (rc *MemcacheCache) Incr(key string) error {
|
func (rc *Cache) Incr(key string) error {
|
||||||
if rc.conn == nil {
|
if rc.conn == nil {
|
||||||
if err := rc.connectInit(); err != nil {
|
if err := rc.connectInit(); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -125,8 +124,8 @@ func (rc *MemcacheCache) Incr(key string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// decrease counter.
|
// Decr decrease counter.
|
||||||
func (rc *MemcacheCache) Decr(key string) error {
|
func (rc *Cache) Decr(key string) error {
|
||||||
if rc.conn == nil {
|
if rc.conn == nil {
|
||||||
if err := rc.connectInit(); err != nil {
|
if err := rc.connectInit(); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -136,8 +135,8 @@ func (rc *MemcacheCache) Decr(key string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// check value exists in memcache.
|
// IsExist check value exists in memcache.
|
||||||
func (rc *MemcacheCache) IsExist(key string) bool {
|
func (rc *Cache) IsExist(key string) bool {
|
||||||
if rc.conn == nil {
|
if rc.conn == nil {
|
||||||
if err := rc.connectInit(); err != nil {
|
if err := rc.connectInit(); err != nil {
|
||||||
return false
|
return false
|
||||||
@ -150,8 +149,8 @@ func (rc *MemcacheCache) IsExist(key string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// clear all cached in memcache.
|
// ClearAll clear all cached in memcache.
|
||||||
func (rc *MemcacheCache) ClearAll() error {
|
func (rc *Cache) ClearAll() error {
|
||||||
if rc.conn == nil {
|
if rc.conn == nil {
|
||||||
if err := rc.connectInit(); err != nil {
|
if err := rc.connectInit(); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -160,10 +159,10 @@ func (rc *MemcacheCache) ClearAll() error {
|
|||||||
return rc.conn.FlushAll()
|
return rc.conn.FlushAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
// start memcache adapter.
|
// StartAndGC start memcache adapter.
|
||||||
// config string is like {"conn":"connection info"}.
|
// config string is like {"conn":"connection info"}.
|
||||||
// if connecting error, return.
|
// if connecting error, return.
|
||||||
func (rc *MemcacheCache) StartAndGC(config string) error {
|
func (rc *Cache) StartAndGC(config string) error {
|
||||||
var cf map[string]string
|
var cf map[string]string
|
||||||
json.Unmarshal([]byte(config), &cf)
|
json.Unmarshal([]byte(config), &cf)
|
||||||
if _, ok := cf["conn"]; !ok {
|
if _, ok := cf["conn"]; !ok {
|
||||||
@ -179,7 +178,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// connect to memcache and keep the connection.
|
// connect to memcache and keep the connection.
|
||||||
func (rc *MemcacheCache) connectInit() error {
|
func (rc *Cache) connectInit() error {
|
||||||
rc.conn = memcache.New(rc.conninfo...)
|
rc.conn = memcache.New(rc.conninfo...)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
26
cache/memory.go
vendored
26
cache/memory.go
vendored
@ -23,18 +23,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// clock time of recycling the expired cache items in memory.
|
// DefaultEvery means the clock time of recycling the expired cache items in memory.
|
||||||
DefaultEvery int = 60 // 1 minute
|
DefaultEvery = 60 // 1 minute
|
||||||
)
|
)
|
||||||
|
|
||||||
// Memory cache item.
|
// MemoryItem store enery cache item.
|
||||||
type MemoryItem struct {
|
type MemoryItem struct {
|
||||||
val interface{}
|
val interface{}
|
||||||
Lastaccess time.Time
|
Lastaccess time.Time
|
||||||
expired int64
|
expired int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// Memory cache adapter.
|
// MemoryCache is Memory cache adapter.
|
||||||
// it contains a RW locker for safe map storage.
|
// it contains a RW locker for safe map storage.
|
||||||
type MemoryCache struct {
|
type MemoryCache struct {
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
@ -87,7 +87,7 @@ func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Delete cache in memory.
|
// Delete cache in memory.
|
||||||
func (bc *MemoryCache) Delete(name string) error {
|
func (bc *MemoryCache) Delete(name string) error {
|
||||||
bc.lock.Lock()
|
bc.lock.Lock()
|
||||||
defer bc.lock.Unlock()
|
defer bc.lock.Unlock()
|
||||||
@ -101,7 +101,7 @@ func (bc *MemoryCache) Delete(name string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Increase cache counter in memory.
|
// Incr increase cache counter in memory.
|
||||||
// it supports int,int64,int32,uint,uint64,uint32.
|
// it supports int,int64,int32,uint,uint64,uint32.
|
||||||
func (bc *MemoryCache) Incr(key string) error {
|
func (bc *MemoryCache) Incr(key string) error {
|
||||||
bc.lock.RLock()
|
bc.lock.RLock()
|
||||||
@ -129,7 +129,7 @@ func (bc *MemoryCache) Incr(key string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decrease counter in memory.
|
// Decr decrease counter in memory.
|
||||||
func (bc *MemoryCache) Decr(key string) error {
|
func (bc *MemoryCache) Decr(key string) error {
|
||||||
bc.lock.RLock()
|
bc.lock.RLock()
|
||||||
defer bc.lock.RUnlock()
|
defer bc.lock.RUnlock()
|
||||||
@ -168,7 +168,7 @@ func (bc *MemoryCache) Decr(key string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// check cache exist in memory.
|
// IsExist check cache exist in memory.
|
||||||
func (bc *MemoryCache) IsExist(name string) bool {
|
func (bc *MemoryCache) IsExist(name string) bool {
|
||||||
bc.lock.RLock()
|
bc.lock.RLock()
|
||||||
defer bc.lock.RUnlock()
|
defer bc.lock.RUnlock()
|
||||||
@ -176,7 +176,7 @@ func (bc *MemoryCache) IsExist(name string) bool {
|
|||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete all cache in memory.
|
// ClearAll will delete all cache in memory.
|
||||||
func (bc *MemoryCache) ClearAll() error {
|
func (bc *MemoryCache) ClearAll() error {
|
||||||
bc.lock.Lock()
|
bc.lock.Lock()
|
||||||
defer bc.lock.Unlock()
|
defer bc.lock.Unlock()
|
||||||
@ -184,7 +184,7 @@ func (bc *MemoryCache) ClearAll() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// start memory cache. it will check expiration in every clock time.
|
// StartAndGC start memory cache. it will check expiration in every clock time.
|
||||||
func (bc *MemoryCache) StartAndGC(config string) error {
|
func (bc *MemoryCache) StartAndGC(config string) error {
|
||||||
var cf map[string]int
|
var cf map[string]int
|
||||||
json.Unmarshal([]byte(config), &cf)
|
json.Unmarshal([]byte(config), &cf)
|
||||||
@ -213,13 +213,13 @@ func (bc *MemoryCache) vaccuum() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
for name := range bc.items {
|
for name := range bc.items {
|
||||||
bc.item_expired(name)
|
bc.itemExpired(name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// item_expired returns true if an item is expired.
|
// itemExpired returns true if an item is expired.
|
||||||
func (bc *MemoryCache) item_expired(name string) bool {
|
func (bc *MemoryCache) itemExpired(name string) bool {
|
||||||
bc.lock.Lock()
|
bc.lock.Lock()
|
||||||
defer bc.lock.Unlock()
|
defer bc.lock.Unlock()
|
||||||
itm, ok := bc.items[name]
|
itm, ok := bc.items[name]
|
||||||
|
52
cache/redis/redis.go
vendored
52
cache/redis/redis.go
vendored
@ -12,7 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
// package redis for cache provider
|
// Package redis for cache provider
|
||||||
//
|
//
|
||||||
// depend on github.com/garyburd/redigo/redis
|
// depend on github.com/garyburd/redigo/redis
|
||||||
//
|
//
|
||||||
@ -41,12 +41,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// the collection name of redis for cache adapter.
|
// DefaultKey the collection name of redis for cache adapter.
|
||||||
DefaultKey string = "beecacheRedis"
|
DefaultKey = "beecacheRedis"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Redis cache adapter.
|
// Cache is Redis cache adapter.
|
||||||
type RedisCache struct {
|
type Cache struct {
|
||||||
p *redis.Pool // redis connection pool
|
p *redis.Pool // redis connection pool
|
||||||
conninfo string
|
conninfo string
|
||||||
dbNum int
|
dbNum int
|
||||||
@ -54,13 +54,13 @@ type RedisCache struct {
|
|||||||
password string
|
password string
|
||||||
}
|
}
|
||||||
|
|
||||||
// create new redis cache with default collection name.
|
// NewRedisCache create new redis cache with default collection name.
|
||||||
func NewRedisCache() *RedisCache {
|
func NewRedisCache() *Cache {
|
||||||
return &RedisCache{key: DefaultKey}
|
return &Cache{key: DefaultKey}
|
||||||
}
|
}
|
||||||
|
|
||||||
// actually do the redis cmds
|
// actually do the redis cmds
|
||||||
func (rc *RedisCache) do(commandName string, args ...interface{}) (reply interface{}, err error) {
|
func (rc *Cache) do(commandName string, args ...interface{}) (reply interface{}, err error) {
|
||||||
c := rc.p.Get()
|
c := rc.p.Get()
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
|
|
||||||
@ -68,7 +68,7 @@ func (rc *RedisCache) do(commandName string, args ...interface{}) (reply interfa
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get cache from redis.
|
// Get cache from redis.
|
||||||
func (rc *RedisCache) Get(key string) interface{} {
|
func (rc *Cache) Get(key string) interface{} {
|
||||||
if v, err := rc.do("GET", key); err == nil {
|
if v, err := rc.do("GET", key); err == nil {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
@ -76,7 +76,7 @@ func (rc *RedisCache) Get(key string) interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetMulti get cache from redis.
|
// GetMulti get cache from redis.
|
||||||
func (rc *RedisCache) GetMulti(keys []string) []interface{} {
|
func (rc *Cache) GetMulti(keys []string) []interface{} {
|
||||||
size := len(keys)
|
size := len(keys)
|
||||||
var rv []interface{}
|
var rv []interface{}
|
||||||
c := rc.p.Get()
|
c := rc.p.Get()
|
||||||
@ -108,8 +108,8 @@ ERROR:
|
|||||||
return rv
|
return rv
|
||||||
}
|
}
|
||||||
|
|
||||||
// put cache to redis.
|
// Put put cache to redis.
|
||||||
func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
|
func (rc *Cache) Put(key string, val interface{}, timeout int64) error {
|
||||||
var err error
|
var err error
|
||||||
if _, err = rc.do("SETEX", key, timeout, val); err != nil {
|
if _, err = rc.do("SETEX", key, timeout, val); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -121,8 +121,8 @@ func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete cache in redis.
|
// Delete delete cache in redis.
|
||||||
func (rc *RedisCache) Delete(key string) error {
|
func (rc *Cache) Delete(key string) error {
|
||||||
var err error
|
var err error
|
||||||
if _, err = rc.do("DEL", key); err != nil {
|
if _, err = rc.do("DEL", key); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -131,8 +131,8 @@ func (rc *RedisCache) Delete(key string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// check cache's existence in redis.
|
// IsExist check cache's existence in redis.
|
||||||
func (rc *RedisCache) IsExist(key string) bool {
|
func (rc *Cache) IsExist(key string) bool {
|
||||||
v, err := redis.Bool(rc.do("EXISTS", key))
|
v, err := redis.Bool(rc.do("EXISTS", key))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
@ -145,20 +145,20 @@ func (rc *RedisCache) IsExist(key string) bool {
|
|||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
// increase counter in redis.
|
// Incr increase counter in redis.
|
||||||
func (rc *RedisCache) Incr(key string) error {
|
func (rc *Cache) Incr(key string) error {
|
||||||
_, err := redis.Bool(rc.do("INCRBY", key, 1))
|
_, err := redis.Bool(rc.do("INCRBY", key, 1))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// decrease counter in redis.
|
// Decr decrease counter in redis.
|
||||||
func (rc *RedisCache) Decr(key string) error {
|
func (rc *Cache) Decr(key string) error {
|
||||||
_, err := redis.Bool(rc.do("INCRBY", key, -1))
|
_, err := redis.Bool(rc.do("INCRBY", key, -1))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// clean all cache in redis. delete this redis collection.
|
// ClearAll clean all cache in redis. delete this redis collection.
|
||||||
func (rc *RedisCache) ClearAll() error {
|
func (rc *Cache) ClearAll() error {
|
||||||
cachedKeys, err := redis.Strings(rc.do("HKEYS", rc.key))
|
cachedKeys, err := redis.Strings(rc.do("HKEYS", rc.key))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -172,11 +172,11 @@ func (rc *RedisCache) ClearAll() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// start redis cache adapter.
|
// StartAndGC start redis cache adapter.
|
||||||
// config is like {"key":"collection key","conn":"connection info","dbNum":"0"}
|
// config is like {"key":"collection key","conn":"connection info","dbNum":"0"}
|
||||||
// the cache item in redis are stored forever,
|
// the cache item in redis are stored forever,
|
||||||
// so no gc operation.
|
// so no gc operation.
|
||||||
func (rc *RedisCache) StartAndGC(config string) error {
|
func (rc *Cache) StartAndGC(config string) error {
|
||||||
var cf map[string]string
|
var cf map[string]string
|
||||||
json.Unmarshal([]byte(config), &cf)
|
json.Unmarshal([]byte(config), &cf)
|
||||||
|
|
||||||
@ -206,7 +206,7 @@ func (rc *RedisCache) StartAndGC(config string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// connect to redis.
|
// connect to redis.
|
||||||
func (rc *RedisCache) connectInit() {
|
func (rc *Cache) connectInit() {
|
||||||
dialFunc := func() (c redis.Conn, err error) {
|
dialFunc := func() (c redis.Conn, err error) {
|
||||||
c, err = redis.Dial("tcp", rc.conninfo)
|
c, err = redis.Dial("tcp", rc.conninfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user