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
77c40e6f
Commit
77c40e6f
authored
Jul 12, 2014
by
fuxiaohei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
code style simplify
parent
ea2ed90a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
38 deletions
+32
-38
redis.go
cache/redis/redis.go
+25
-31
redis_test.go
cache/redis/redis_test.go
+7
-7
No files found.
cache/redis/redis.go
View file @
77c40e6f
...
@@ -7,7 +7,7 @@
...
@@ -7,7 +7,7 @@
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @license http://github.com/astaxie/beego/blob/master/LICENSE
//
//
// @authors astaxie
// @authors astaxie
package
cache
package
redis
import
(
import
(
"encoding/json"
"encoding/json"
...
@@ -46,23 +46,21 @@ func (rc *RedisCache) do(commandName string, args ...interface{}) (reply interfa
...
@@ -46,23 +46,21 @@ 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
*
RedisCache
)
Get
(
key
string
)
interface
{}
{
v
,
err
:=
rc
.
do
(
"GET"
,
key
)
if
v
,
err
:=
rc
.
do
(
"GET"
,
key
);
err
==
nil
{
if
err
!=
nil
{
return
v
return
nil
}
}
return
nil
return
v
}
}
// put cache to redis.
// put cache to redis.
func
(
rc
*
RedisCache
)
Put
(
key
string
,
val
interface
{},
timeout
int64
)
error
{
func
(
rc
*
RedisCache
)
Put
(
key
string
,
val
interface
{},
timeout
int64
)
error
{
_
,
err
:=
rc
.
do
(
"SET"
,
key
,
val
)
var
err
error
if
err
!=
nil
{
if
_
,
err
=
rc
.
do
(
"SET"
,
key
,
val
);
err
!=
nil
{
return
nil
return
err
}
}
_
,
err
=
rc
.
do
(
"HSET"
,
rc
.
key
,
key
,
true
)
if
err
!=
nil
{
if
_
,
err
=
rc
.
do
(
"HSET"
,
rc
.
key
,
key
,
true
);
err
!=
nil
{
return
nil
return
err
}
}
_
,
err
=
rc
.
do
(
"EXPIRE"
,
key
,
timeout
)
_
,
err
=
rc
.
do
(
"EXPIRE"
,
key
,
timeout
)
return
err
return
err
...
@@ -70,9 +68,9 @@ func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
...
@@ -70,9 +68,9 @@ func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
// delete cache in redis.
// delete cache in redis.
func
(
rc
*
RedisCache
)
Delete
(
key
string
)
error
{
func
(
rc
*
RedisCache
)
Delete
(
key
string
)
error
{
_
,
err
:=
rc
.
do
(
"DEL"
,
key
)
var
err
error
if
err
!=
nil
{
if
_
,
err
=
rc
.
do
(
"DEL"
,
key
);
err
!=
nil
{
return
nil
return
err
}
}
_
,
err
=
rc
.
do
(
"HDEL"
,
rc
.
key
,
key
)
_
,
err
=
rc
.
do
(
"HDEL"
,
rc
.
key
,
key
)
return
err
return
err
...
@@ -85,8 +83,7 @@ func (rc *RedisCache) IsExist(key string) bool {
...
@@ -85,8 +83,7 @@ func (rc *RedisCache) IsExist(key string) bool {
return
false
return
false
}
}
if
v
==
false
{
if
v
==
false
{
_
,
err
:=
rc
.
do
(
"HDEL"
,
rc
.
key
,
key
)
if
_
,
err
=
rc
.
do
(
"HDEL"
,
rc
.
key
,
key
);
err
!=
nil
{
if
err
!=
nil
{
return
false
return
false
}
}
}
}
...
@@ -108,10 +105,12 @@ func (rc *RedisCache) Decr(key string) error {
...
@@ -108,10 +105,12 @@ func (rc *RedisCache) Decr(key string) error {
// clean all cache in redis. delete this redis collection.
// clean all cache in redis. delete this redis collection.
func
(
rc
*
RedisCache
)
ClearAll
()
error
{
func
(
rc
*
RedisCache
)
ClearAll
()
error
{
cachedKeys
,
err
:=
redis
.
Strings
(
rc
.
do
(
"HKEYS"
,
rc
.
key
))
cachedKeys
,
err
:=
redis
.
Strings
(
rc
.
do
(
"HKEYS"
,
rc
.
key
))
if
err
!=
nil
{
return
err
}
for
_
,
str
:=
range
cachedKeys
{
for
_
,
str
:=
range
cachedKeys
{
_
,
err
:=
rc
.
do
(
"DEL"
,
str
)
if
_
,
err
=
rc
.
do
(
"DEL"
,
str
);
err
!=
nil
{
if
err
!=
nil
{
return
err
return
nil
}
}
}
}
_
,
err
=
rc
.
do
(
"DEL"
,
rc
.
key
)
_
,
err
=
rc
.
do
(
"DEL"
,
rc
.
key
)
...
@@ -140,26 +139,21 @@ func (rc *RedisCache) StartAndGC(config string) error {
...
@@ -140,26 +139,21 @@ func (rc *RedisCache) StartAndGC(config string) error {
c
:=
rc
.
p
.
Get
()
c
:=
rc
.
p
.
Get
()
defer
c
.
Close
()
defer
c
.
Close
()
if
err
:=
c
.
Err
();
err
!=
nil
{
return
err
}
return
nil
return
c
.
Err
()
}
}
// connect to redis.
// connect to redis.
func
(
rc
*
RedisCache
)
connectInit
()
{
func
(
rc
*
RedisCache
)
connectInit
()
{
dialFunc
:=
func
()
(
c
redis
.
Conn
,
err
error
)
{
c
,
err
=
redis
.
Dial
(
"tcp"
,
rc
.
conninfo
)
return
}
// initialize a new pool
// initialize a new pool
rc
.
p
=
&
redis
.
Pool
{
rc
.
p
=
&
redis
.
Pool
{
MaxIdle
:
3
,
MaxIdle
:
3
,
IdleTimeout
:
180
*
time
.
Second
,
IdleTimeout
:
180
*
time
.
Second
,
Dial
:
func
()
(
redis
.
Conn
,
error
)
{
Dial
:
dialFunc
,
c
,
err
:=
redis
.
Dial
(
"tcp"
,
rc
.
conninfo
)
if
err
!=
nil
{
return
nil
,
err
}
return
c
,
nil
},
}
}
}
}
...
...
cache/redis/redis_test.go
View file @
77c40e6f
...
@@ -8,7 +8,7 @@
...
@@ -8,7 +8,7 @@
// @authors astaxie
// @authors astaxie
package
cache
package
redis
import
(
import
(
"testing"
"testing"
...
@@ -20,7 +20,7 @@ import (
...
@@ -20,7 +20,7 @@ import (
)
)
func
TestRedisCache
(
t
*
testing
.
T
)
{
func
TestRedisCache
(
t
*
testing
.
T
)
{
bm
,
err
:=
cache
.
NewCache
(
"redis"
,
`{"conn": "127.0.0.1:6379"}`
)
bm
,
err
:=
cache
.
NewCache
(
"redis"
,
`{"conn": "127.0.0.1:6379"}`
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Error
(
"init err"
)
t
.
Error
(
"init err"
)
}
}
...
@@ -48,7 +48,7 @@ func TestRedisCache(t *testing.T) {
...
@@ -48,7 +48,7 @@ func TestRedisCache(t *testing.T) {
t
.
Error
(
"Incr Error"
,
err
)
t
.
Error
(
"Incr Error"
,
err
)
}
}
if
v
,
_
:=
redis
.
Int
(
bm
.
Get
(
"astaxie"
),
err
);
v
!=
2
{
if
v
,
_
:=
redis
.
Int
(
bm
.
Get
(
"astaxie"
),
err
);
v
!=
2
{
t
.
Error
(
"get err"
)
t
.
Error
(
"get err"
)
}
}
...
@@ -74,8 +74,8 @@ func TestRedisCache(t *testing.T) {
...
@@ -74,8 +74,8 @@ func TestRedisCache(t *testing.T) {
if
v
,
_
:=
redis
.
String
(
bm
.
Get
(
"astaxie"
),
err
);
v
!=
"author"
{
if
v
,
_
:=
redis
.
String
(
bm
.
Get
(
"astaxie"
),
err
);
v
!=
"author"
{
t
.
Error
(
"get err"
)
t
.
Error
(
"get err"
)
}
}
// test clear all
// test clear all
if
err
=
bm
.
ClearAll
();
err
!=
nil
{
if
err
=
bm
.
ClearAll
();
err
!=
nil
{
t
.
Error
(
"clear all err"
)
t
.
Error
(
"clear all err"
)
}
}
}
}
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