Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
f657509a
authored
2014-07-12 19:33:32 +0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Plain Diff
Merge pull request #693 from fuxiaohei/develop
code style simplify
2 parents
15759f60
77c40e6f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
32 deletions
cache/redis/redis.go
cache/redis/redis_test.go
cache/redis/redis.go
View file @
f657509
...
...
@@ -7,7 +7,7 @@
// @license http://github.com/astaxie/beego/blob/master/LICENSE
//
// @authors astaxie
package
cache
package
redis
import
(
"encoding/json"
...
...
@@ -46,23 +46,21 @@ func (rc *RedisCache) do(commandName string, args ...interface{}) (reply interfa
// Get cache from redis.
func
(
rc
*
RedisCache
)
Get
(
key
string
)
interface
{}
{
v
,
err
:=
rc
.
do
(
"GET"
,
key
)
if
err
!=
nil
{
return
nil
}
if
v
,
err
:=
rc
.
do
(
"GET"
,
key
);
err
==
nil
{
return
v
}
return
nil
}
// put cache to redis.
func
(
rc
*
RedisCache
)
Put
(
key
string
,
val
interface
{},
timeout
int64
)
error
{
_
,
err
:=
rc
.
do
(
"SET"
,
key
,
val
)
if
err
!=
nil
{
return
nil
var
err
error
if
_
,
err
=
rc
.
do
(
"SET"
,
key
,
val
);
err
!=
nil
{
return
err
}
_
,
err
=
rc
.
do
(
"HSET"
,
rc
.
key
,
key
,
true
)
if
err
!=
nil
{
return
nil
if
_
,
err
=
rc
.
do
(
"HSET"
,
rc
.
key
,
key
,
true
);
err
!=
nil
{
return
err
}
_
,
err
=
rc
.
do
(
"EXPIRE"
,
key
,
timeout
)
return
err
...
...
@@ -70,9 +68,9 @@ func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
// delete cache in redis.
func
(
rc
*
RedisCache
)
Delete
(
key
string
)
error
{
_
,
err
:=
rc
.
do
(
"DEL"
,
key
)
if
err
!=
nil
{
return
nil
var
err
error
if
_
,
err
=
rc
.
do
(
"DEL"
,
key
);
err
!=
nil
{
return
err
}
_
,
err
=
rc
.
do
(
"HDEL"
,
rc
.
key
,
key
)
return
err
...
...
@@ -85,8 +83,7 @@ func (rc *RedisCache) IsExist(key string) bool {
return
false
}
if
v
==
false
{
_
,
err
:=
rc
.
do
(
"HDEL"
,
rc
.
key
,
key
)
if
err
!=
nil
{
if
_
,
err
=
rc
.
do
(
"HDEL"
,
rc
.
key
,
key
);
err
!=
nil
{
return
false
}
}
...
...
@@ -108,10 +105,12 @@ func (rc *RedisCache) Decr(key string) error {
// clean all cache in redis. delete this redis collection.
func
(
rc
*
RedisCache
)
ClearAll
()
error
{
cachedKeys
,
err
:=
redis
.
Strings
(
rc
.
do
(
"HKEYS"
,
rc
.
key
))
for
_
,
str
:=
range
cachedKeys
{
_
,
err
:=
rc
.
do
(
"DEL"
,
str
)
if
err
!=
nil
{
return
nil
return
err
}
for
_
,
str
:=
range
cachedKeys
{
if
_
,
err
=
rc
.
do
(
"DEL"
,
str
);
err
!=
nil
{
return
err
}
}
_
,
err
=
rc
.
do
(
"DEL"
,
rc
.
key
)
...
...
@@ -140,26 +139,21 @@ func (rc *RedisCache) StartAndGC(config string) error {
c
:=
rc
.
p
.
Get
()
defer
c
.
Close
()
if
err
:=
c
.
Err
();
err
!=
nil
{
return
err
}
return
nil
return
c
.
Err
()
}
// connect to redis.
func
(
rc
*
RedisCache
)
connectInit
()
{
dialFunc
:=
func
()
(
c
redis
.
Conn
,
err
error
)
{
c
,
err
=
redis
.
Dial
(
"tcp"
,
rc
.
conninfo
)
return
}
// initialize a new pool
rc
.
p
=
&
redis
.
Pool
{
MaxIdle
:
3
,
IdleTimeout
:
180
*
time
.
Second
,
Dial
:
func
()
(
redis
.
Conn
,
error
)
{
c
,
err
:=
redis
.
Dial
(
"tcp"
,
rc
.
conninfo
)
if
err
!=
nil
{
return
nil
,
err
}
return
c
,
nil
},
Dial
:
dialFunc
,
}
}
...
...
cache/redis/redis_test.go
View file @
f657509
...
...
@@ -8,7 +8,7 @@
// @authors astaxie
package
cache
package
redis
import
(
"testing"
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
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 post a comment