Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
d7aaf2eb
Commit
d7aaf2eb
authored
Sep 08, 2015
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
golint cache package
parent
62e528ca
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
106 additions
and
105 deletions
+106
-105
cache.go
cache/cache.go
+2
-1
conv.go
cache/conv.go
+6
-6
conv_test.go
cache/conv_test.go
+7
-7
file.go
cache/file.go
+23
-22
memcache.go
cache/memcache/memcache.go
+29
-30
memory.go
cache/memory.go
+13
-13
redis.go
cache/redis/redis.go
+26
-26
No files found.
cache/cache.go
View file @
d7aaf2eb
...
...
@@ -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
)
{
...
...
cache/conv.go
View file @
d7aaf2eb
...
...
@@ -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
:
...
...
cache/conv_test.go
View file @
d7aaf2eb
...
...
@@ -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"
)
}
...
...
cache/file.go
View file @
d7aaf2eb
...
...
@@ -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.
// Start
AndGC 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_c
ontents
(
fc
.
getCacheFileName
(
key
))
fileData
,
err
:=
File
GetC
ontents
(
fc
.
getCacheFileName
(
key
))
if
err
!=
nil
{
return
""
}
var
to
FileCacheItem
Gob
_d
ecode
(
fileData
,
&
to
)
Gob
D
ecode
(
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
_e
ncode
(
item
)
data
,
err
:=
Gob
E
ncode
(
item
)
if
err
!=
nil
{
return
err
}
return
File
_put_c
ontents
(
fc
.
getCacheFileName
(
key
),
data
)
return
File
PutC
ontents
(
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 incr
ease 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 decr
ease 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
}
//
C
heck value is exist.
//
IsExist c
heck value is exist.
func
(
fc
*
FileCache
)
IsExist
(
key
string
)
bool
{
ret
,
_
:=
exists
(
fc
.
getCacheFileName
(
key
))
return
ret
}
// Clean cached files.
// Clea
rAll will clea
n 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_c
ontents
(
filename
string
)
(
data
[]
byte
,
e
error
)
{
func
File
GetC
ontents
(
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_c
ontents
(
filename
string
,
content
[]
byte
)
error
{
func
File
PutC
ontents
(
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
_e
ncode
(
data
interface
{})
([]
byte
,
error
)
{
// Gob
Encode Gob
encodes file cache item.
func
Gob
E
ncode
(
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
_d
ecode
(
data
[]
byte
,
to
*
FileCacheItem
)
error
{
// Gob
Decode Gob
decodes file cache item.
func
Gob
D
ecode
(
data
[]
byte
,
to
*
FileCacheItem
)
error
{
buf
:=
bytes
.
NewBuffer
(
data
)
dec
:=
gob
.
NewDecoder
(
buf
)
return
dec
.
Decode
(
&
to
)
...
...
cache/memcache/memcache.go
View file @
d7aaf2eb
...
...
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
package memca
he for cache provider
//
Package memcac
he for cache provider
//
// depend on github.com/bradfitz/gomemcache/memcache
//
...
...
@@ -39,19 +39,19 @@ import (
"github.com/astaxie/beego/cache"
)
// Memcache adapter.
type
Memcache
Cache
struct
{
//
Cache
Memcache adapter.
type
Cache
struct
{
conn
*
memcache
.
Client
conninfo
[]
string
}
// create new memcache adapter.
func
NewMemCache
()
*
Memcache
Cache
{
return
&
Memcache
Cache
{}
//
NewMemCache
create new memcache adapter.
func
NewMemCache
()
*
Cache
{
return
&
Cache
{}
}
// get value from memcache.
func
(
rc
*
Memcache
Cache
)
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
*
Memcache
Cache
)
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
*
Memcache
Cache
)
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
*
Memcache
Cache
)
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
*
Memcache
Cache
)
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
*
Memcache
Cache
)
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
*
Memcache
Cache
)
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
*
Memcache
Cache
)
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
*
Memcache
Cache
)
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
*
Memcache
Cache
)
connectInit
()
error
{
func
(
rc
*
Cache
)
connectInit
()
error
{
rc
.
conn
=
memcache
.
New
(
rc
.
conninfo
...
)
return
nil
}
...
...
cache/memory.go
View file @
d7aaf2eb
...
...
@@ -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.
// Memory
Item store enery
cache item.
type
MemoryItem
struct
{
val
interface
{}
Lastaccess
time
.
Time
expired
int64
}
// Memory cache adapter.
// Memory
Cache 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
incr
ease 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
decr
ease 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
_e
xpired
(
name
)
bc
.
item
E
xpired
(
name
)
}
}
}
// item
_e
xpired returns true if an item is expired.
func
(
bc
*
MemoryCache
)
item
_e
xpired
(
name
string
)
bool
{
// item
E
xpired returns true if an item is expired.
func
(
bc
*
MemoryCache
)
item
E
xpired
(
name
string
)
bool
{
bc
.
lock
.
Lock
()
defer
bc
.
lock
.
Unlock
()
itm
,
ok
:=
bc
.
items
[
name
]
...
...
cache/redis/redis.go
View file @
d7aaf2eb
...
...
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
p
ackage redis for cache provider
//
P
ackage 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
Redis
Cache
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
()
*
Redis
Cache
{
return
&
Redis
Cache
{
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
*
Redis
Cache
)
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
*
Redis
Cache
)
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
*
Redis
Cache
)
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
*
Redis
Cache
)
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
*
Redis
Cache
)
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
*
Redis
Cache
)
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
*
Redis
Cache
)
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
*
Redis
Cache
)
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
*
Redis
Cache
)
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
*
Redis
Cache
)
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
*
Redis
Cache
)
connectInit
()
{
func
(
rc
*
Cache
)
connectInit
()
{
dialFunc
:=
func
()
(
c
redis
.
Conn
,
err
error
)
{
c
,
err
=
redis
.
Dial
(
"tcp"
,
rc
.
conninfo
)
if
err
!=
nil
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment