1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-22 22:40:49 +00:00

golint cache package

This commit is contained in:
astaxie 2015-09-09 00:15:03 +08:00
parent 62e528ca4c
commit d7aaf2ebeb
7 changed files with 106 additions and 105 deletions

3
cache/cache.go vendored
View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Package cache provide a Cache interface and some implemetn engine
// Usage:
//
// import(
@ -80,7 +81,7 @@ func Register(name string, adapter Cache) {
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}.
// it will start gc automatically.
func NewCache(adapterName, config string) (adapter Cache, err error) {

12
cache/conv.go vendored
View File

@ -19,7 +19,7 @@ import (
"strconv"
)
// convert interface to string.
// GetString convert interface to string.
func GetString(v interface{}) string {
switch result := v.(type) {
case string:
@ -34,7 +34,7 @@ func GetString(v interface{}) string {
return ""
}
// convert interface to int.
// GetInt convert interface to int.
func GetInt(v interface{}) int {
switch result := v.(type) {
case int:
@ -52,7 +52,7 @@ func GetInt(v interface{}) int {
return 0
}
// convert interface to int64.
// GetInt64 convert interface to int64.
func GetInt64(v interface{}) int64 {
switch result := v.(type) {
case int:
@ -71,7 +71,7 @@ func GetInt64(v interface{}) int64 {
return 0
}
// convert interface to float64.
// GetFloat64 convert interface to float64.
func GetFloat64(v interface{}) float64 {
switch result := v.(type) {
case float64:
@ -85,7 +85,7 @@ func GetFloat64(v interface{}) float64 {
return 0
}
// convert interface to bool.
// GetBool convert interface to bool.
func GetBool(v interface{}) bool {
switch result := v.(type) {
case bool:
@ -99,7 +99,7 @@ func GetBool(v interface{}) bool {
return false
}
// convert interface to byte slice.
// getByteArray convert interface to byte slice.
func getByteArray(v interface{}) []byte {
switch result := v.(type) {
case []byte:

14
cache/conv_test.go vendored
View File

@ -27,7 +27,7 @@ func TestGetString(t *testing.T) {
if "test2" != GetString(t2) {
t.Error("get string from byte array error")
}
var t3 int = 1
var t3 = 1
if "1" != GetString(t3) {
t.Error("get string from int error")
}
@ -35,7 +35,7 @@ func TestGetString(t *testing.T) {
if "1" != GetString(t4) {
t.Error("get string from int64 error")
}
var t5 float64 = 1.1
var t5 = 1.1
if "1.1" != GetString(t5) {
t.Error("get string from float64 error")
}
@ -46,7 +46,7 @@ func TestGetString(t *testing.T) {
}
func TestGetInt(t *testing.T) {
var t1 int = 1
var t1 = 1
if 1 != GetInt(t1) {
t.Error("get int from int error")
}
@ -69,7 +69,7 @@ func TestGetInt(t *testing.T) {
func TestGetInt64(t *testing.T) {
var i int64 = 1
var t1 int = 1
var t1 = 1
if i != GetInt64(t1) {
t.Error("get int64 from int error")
}
@ -91,12 +91,12 @@ func TestGetInt64(t *testing.T) {
}
func TestGetFloat64(t *testing.T) {
var f float64 = 1.11
var f = 1.11
var t1 float32 = 1.11
if f != GetFloat64(t1) {
t.Error("get float64 from float32 error")
}
var t2 float64 = 1.11
var t2 = 1.11
if f != GetFloat64(t2) {
t.Error("get float64 from float64 error")
}
@ -106,7 +106,7 @@ func TestGetFloat64(t *testing.T) {
}
var f2 float64 = 1
var t4 int = 1
var t4 = 1
if f2 != GetFloat64(t4) {
t.Error("get float64 from int error")
}

45
cache/file.go vendored
View File

@ -41,11 +41,12 @@ type FileCacheItem struct {
Expired int64
}
// FileCache Config
var (
FileCachePath string = "cache" // cache directory
FileCacheFileSuffix string = ".bin" // cache file suffix
FileCacheDirectoryLevel int = 2 // cache file deep level if auto generated cache files.
FileCacheEmbedExpiry int64 = 0 // cache expire time, default is no expire forever.
FileCachePath = "cache" // cache directory
FileCacheFileSuffix = ".bin" // cache file suffix
FileCacheDirectoryLevel = 2 // cache file deep level if auto generated cache files.
FileCacheEmbedExpiry int64 // cache expire time, default is no expire forever.
)
// FileCache is cache adapter for file storage.
@ -56,14 +57,14 @@ type FileCache struct {
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.
func NewFileCache() *FileCache {
// return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix}
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}
func (fc *FileCache) StartAndGC(config string) error {
@ -120,12 +121,12 @@ func (fc *FileCache) getCacheFileName(key string) string {
// Get value from file cache.
// if non-exist or expired, return empty string.
func (fc *FileCache) Get(key string) interface{} {
fileData, err := File_get_contents(fc.getCacheFileName(key))
fileData, err := FileGetContents(fc.getCacheFileName(key))
if err != nil {
return ""
}
var to FileCacheItem
Gob_decode(fileData, &to)
GobDecode(fileData, &to)
if to.Expired < time.Now().Unix() {
return ""
}
@ -155,11 +156,11 @@ func (fc *FileCache) Put(key string, val interface{}, timeout int64) error {
item.Expired = time.Now().Unix() + timeout
}
item.Lastaccess = time.Now().Unix()
data, err := Gob_encode(item)
data, err := GobEncode(item)
if err != nil {
return err
}
return File_put_contents(fc.getCacheFileName(key), data)
return FilePutContents(fc.getCacheFileName(key), data)
}
// Delete file cache value.
@ -171,7 +172,7 @@ func (fc *FileCache) Delete(key string) error {
return nil
}
// Increase cached int value.
// Incr will increase cached int value.
// fc value is saving forever unless Delete.
func (fc *FileCache) Incr(key string) error {
data := fc.Get(key)
@ -185,7 +186,7 @@ func (fc *FileCache) Incr(key string) error {
return nil
}
// Decrease cached int value.
// Decr will decrease cached int value.
func (fc *FileCache) Decr(key string) error {
data := fc.Get(key)
var decr int
@ -198,13 +199,13 @@ func (fc *FileCache) Decr(key string) error {
return nil
}
// Check value is exist.
// IsExist check value is exist.
func (fc *FileCache) IsExist(key string) bool {
ret, _ := exists(fc.getCacheFileName(key))
return ret
}
// Clean cached files.
// ClearAll will clean cached files.
// not implemented.
func (fc *FileCache) ClearAll() error {
return nil
@ -222,9 +223,9 @@ func exists(path string) (bool, error) {
return false, err
}
// Get bytes to file.
// FileGetContents Get bytes to 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)
if e != nil {
return
@ -242,9 +243,9 @@ func File_get_contents(filename string) (data []byte, e error) {
return
}
// Put bytes to file.
// FilePutContents Put bytes to 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)
if err != nil {
return err
@ -254,8 +255,8 @@ func File_put_contents(filename string, content []byte) error {
return err
}
// Gob encodes file cache item.
func Gob_encode(data interface{}) ([]byte, error) {
// GobEncode Gob encodes file cache item.
func GobEncode(data interface{}) ([]byte, error) {
buf := bytes.NewBuffer(nil)
enc := gob.NewEncoder(buf)
err := enc.Encode(data)
@ -265,8 +266,8 @@ func Gob_encode(data interface{}) ([]byte, error) {
return buf.Bytes(), err
}
// Gob decodes file cache item.
func Gob_decode(data []byte, to *FileCacheItem) error {
// GobDecode Gob decodes file cache item.
func GobDecode(data []byte, to *FileCacheItem) error {
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)
return dec.Decode(&to)

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// package memcahe for cache provider
// Package memcache for cache provider
//
// depend on github.com/bradfitz/gomemcache/memcache
//
@ -39,19 +39,19 @@ import (
"github.com/astaxie/beego/cache"
)
// Memcache adapter.
type MemcacheCache struct {
// Cache Memcache adapter.
type Cache struct {
conn *memcache.Client
conninfo []string
}
// create new memcache adapter.
func NewMemCache() *MemcacheCache {
return &MemcacheCache{}
// NewMemCache create new memcache adapter.
func NewMemCache() *Cache {
return &Cache{}
}
// get value from memcache.
func (rc *MemcacheCache) Get(key string) interface{} {
// Get get value from memcache.
func (rc *Cache) Get(key string) interface{} {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
@ -63,8 +63,8 @@ func (rc *MemcacheCache) Get(key string) interface{} {
return nil
}
// get value from memcache.
func (rc *MemcacheCache) GetMulti(keys []string) []interface{} {
// GetMulti get value from memcache.
func (rc *Cache) GetMulti(keys []string) []interface{} {
size := len(keys)
var rv []interface{}
if rc.conn == nil {
@ -81,16 +81,15 @@ func (rc *MemcacheCache) GetMulti(keys []string) []interface{} {
rv = append(rv, string(v.Value))
}
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.
func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
// Put put value to memcache. only support string.
func (rc *Cache) Put(key string, val interface{}, timeout int64) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
@ -104,8 +103,8 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
return rc.conn.Set(&item)
}
// delete value in memcache.
func (rc *MemcacheCache) Delete(key string) error {
// Delete delete value in memcache.
func (rc *Cache) Delete(key string) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
@ -114,8 +113,8 @@ func (rc *MemcacheCache) Delete(key string) error {
return rc.conn.Delete(key)
}
// increase counter.
func (rc *MemcacheCache) Incr(key string) error {
// Incr increase counter.
func (rc *Cache) Incr(key string) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
@ -125,8 +124,8 @@ func (rc *MemcacheCache) Incr(key string) error {
return err
}
// decrease counter.
func (rc *MemcacheCache) Decr(key string) error {
// Decr decrease counter.
func (rc *Cache) Decr(key string) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
@ -136,8 +135,8 @@ func (rc *MemcacheCache) Decr(key string) error {
return err
}
// check value exists in memcache.
func (rc *MemcacheCache) IsExist(key string) bool {
// IsExist check value exists in memcache.
func (rc *Cache) IsExist(key string) bool {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return false
@ -150,8 +149,8 @@ func (rc *MemcacheCache) IsExist(key string) bool {
return true
}
// clear all cached in memcache.
func (rc *MemcacheCache) ClearAll() error {
// ClearAll clear all cached in memcache.
func (rc *Cache) ClearAll() error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
@ -160,10 +159,10 @@ func (rc *MemcacheCache) ClearAll() error {
return rc.conn.FlushAll()
}
// start memcache adapter.
// StartAndGC start memcache adapter.
// config string is like {"conn":"connection info"}.
// if connecting error, return.
func (rc *MemcacheCache) StartAndGC(config string) error {
func (rc *Cache) StartAndGC(config string) error {
var cf map[string]string
json.Unmarshal([]byte(config), &cf)
if _, ok := cf["conn"]; !ok {
@ -179,7 +178,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error {
}
// connect to memcache and keep the connection.
func (rc *MemcacheCache) connectInit() error {
func (rc *Cache) connectInit() error {
rc.conn = memcache.New(rc.conninfo...)
return nil
}

26
cache/memory.go vendored
View File

@ -23,18 +23,18 @@ import (
)
var (
// clock time of recycling the expired cache items in memory.
DefaultEvery int = 60 // 1 minute
// DefaultEvery means the clock time of recycling the expired cache items in memory.
DefaultEvery = 60 // 1 minute
)
// Memory cache item.
// MemoryItem store enery cache item.
type MemoryItem struct {
val interface{}
Lastaccess time.Time
expired int64
}
// Memory cache adapter.
// MemoryCache is Memory cache adapter.
// it contains a RW locker for safe map storage.
type MemoryCache struct {
lock sync.RWMutex
@ -87,7 +87,7 @@ func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error
return nil
}
/// Delete cache in memory.
// Delete cache in memory.
func (bc *MemoryCache) Delete(name string) error {
bc.lock.Lock()
defer bc.lock.Unlock()
@ -101,7 +101,7 @@ func (bc *MemoryCache) Delete(name string) error {
return nil
}
// Increase cache counter in memory.
// Incr increase cache counter in memory.
// it supports int,int64,int32,uint,uint64,uint32.
func (bc *MemoryCache) Incr(key string) error {
bc.lock.RLock()
@ -129,7 +129,7 @@ func (bc *MemoryCache) Incr(key string) error {
return nil
}
// Decrease counter in memory.
// Decr decrease counter in memory.
func (bc *MemoryCache) Decr(key string) error {
bc.lock.RLock()
defer bc.lock.RUnlock()
@ -168,7 +168,7 @@ func (bc *MemoryCache) Decr(key string) error {
return nil
}
// check cache exist in memory.
// IsExist check cache exist in memory.
func (bc *MemoryCache) IsExist(name string) bool {
bc.lock.RLock()
defer bc.lock.RUnlock()
@ -176,7 +176,7 @@ func (bc *MemoryCache) IsExist(name string) bool {
return ok
}
// delete all cache in memory.
// ClearAll will delete all cache in memory.
func (bc *MemoryCache) ClearAll() error {
bc.lock.Lock()
defer bc.lock.Unlock()
@ -184,7 +184,7 @@ func (bc *MemoryCache) ClearAll() error {
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 {
var cf map[string]int
json.Unmarshal([]byte(config), &cf)
@ -213,13 +213,13 @@ func (bc *MemoryCache) vaccuum() {
return
}
for name := range bc.items {
bc.item_expired(name)
bc.itemExpired(name)
}
}
}
// item_expired returns true if an item is expired.
func (bc *MemoryCache) item_expired(name string) bool {
// itemExpired returns true if an item is expired.
func (bc *MemoryCache) itemExpired(name string) bool {
bc.lock.Lock()
defer bc.lock.Unlock()
itm, ok := bc.items[name]

52
cache/redis/redis.go vendored
View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// package redis for cache provider
// Package redis for cache provider
//
// depend on github.com/garyburd/redigo/redis
//
@ -41,12 +41,12 @@ import (
)
var (
// the collection name of redis for cache adapter.
DefaultKey string = "beecacheRedis"
// DefaultKey the collection name of redis for cache adapter.
DefaultKey = "beecacheRedis"
)
// Redis cache adapter.
type RedisCache struct {
// Cache is Redis cache adapter.
type Cache struct {
p *redis.Pool // redis connection pool
conninfo string
dbNum int
@ -54,13 +54,13 @@ type RedisCache struct {
password string
}
// create new redis cache with default collection name.
func NewRedisCache() *RedisCache {
return &RedisCache{key: DefaultKey}
// NewRedisCache create new redis cache with default collection name.
func NewRedisCache() *Cache {
return &Cache{key: DefaultKey}
}
// 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()
defer c.Close()
@ -68,7 +68,7 @@ func (rc *RedisCache) do(commandName string, args ...interface{}) (reply interfa
}
// 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 {
return v
}
@ -76,7 +76,7 @@ func (rc *RedisCache) Get(key string) interface{} {
}
// GetMulti get cache from redis.
func (rc *RedisCache) GetMulti(keys []string) []interface{} {
func (rc *Cache) GetMulti(keys []string) []interface{} {
size := len(keys)
var rv []interface{}
c := rc.p.Get()
@ -108,8 +108,8 @@ ERROR:
return rv
}
// put cache to redis.
func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
// Put put cache to redis.
func (rc *Cache) Put(key string, val interface{}, timeout int64) error {
var err error
if _, err = rc.do("SETEX", key, timeout, val); err != nil {
return err
@ -121,8 +121,8 @@ func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
return err
}
// delete cache in redis.
func (rc *RedisCache) Delete(key string) error {
// Delete delete cache in redis.
func (rc *Cache) Delete(key string) error {
var err error
if _, err = rc.do("DEL", key); err != nil {
return err
@ -131,8 +131,8 @@ func (rc *RedisCache) Delete(key string) error {
return err
}
// check cache's existence in redis.
func (rc *RedisCache) IsExist(key string) bool {
// IsExist check cache's existence in redis.
func (rc *Cache) IsExist(key string) bool {
v, err := redis.Bool(rc.do("EXISTS", key))
if err != nil {
return false
@ -145,20 +145,20 @@ func (rc *RedisCache) IsExist(key string) bool {
return v
}
// increase counter in redis.
func (rc *RedisCache) Incr(key string) error {
// Incr increase counter in redis.
func (rc *Cache) Incr(key string) error {
_, err := redis.Bool(rc.do("INCRBY", key, 1))
return err
}
// decrease counter in redis.
func (rc *RedisCache) Decr(key string) error {
// Decr decrease counter in redis.
func (rc *Cache) Decr(key string) error {
_, err := redis.Bool(rc.do("INCRBY", key, -1))
return err
}
// clean all cache in redis. delete this redis collection.
func (rc *RedisCache) ClearAll() error {
// ClearAll clean all cache in redis. delete this redis collection.
func (rc *Cache) ClearAll() error {
cachedKeys, err := redis.Strings(rc.do("HKEYS", rc.key))
if err != nil {
return err
@ -172,11 +172,11 @@ func (rc *RedisCache) ClearAll() error {
return err
}
// start redis cache adapter.
// StartAndGC start redis cache adapter.
// config is like {"key":"collection key","conn":"connection info","dbNum":"0"}
// the cache item in redis are stored forever,
// so no gc operation.
func (rc *RedisCache) StartAndGC(config string) error {
func (rc *Cache) StartAndGC(config string) error {
var cf map[string]string
json.Unmarshal([]byte(config), &cf)
@ -206,7 +206,7 @@ func (rc *RedisCache) StartAndGC(config string) error {
}
// connect to redis.
func (rc *RedisCache) connectInit() {
func (rc *Cache) connectInit() {
dialFunc := func() (c redis.Conn, err error) {
c, err = redis.Dial("tcp", rc.conninfo)
if err != nil {