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
e635e274
Commit
e635e274
authored
Jul 09, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' into develop
parents
46cde6e5
cec151fd
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
119 additions
and
15 deletions
+119
-15
cache.go
cache/cache.go
+4
-4
redis.go
cache/redis/redis.go
+33
-10
redis_test.go
cache/redis/redis_test.go
+81
-0
db_alias.go
orm/db_alias.go
+1
-1
No files found.
cache/cache.go
View file @
e635e274
...
@@ -34,11 +34,11 @@ type Cache interface {
...
@@ -34,11 +34,11 @@ type Cache interface {
Incr
(
key
string
)
error
Incr
(
key
string
)
error
// decrease cached int value by key, as a counter.
// decrease cached int value by key, as a counter.
Decr
(
key
string
)
error
Decr
(
key
string
)
error
// check
cached value is existed
or not.
// check
if cached value exists
or not.
IsExist
(
key
string
)
bool
IsExist
(
key
string
)
bool
// clear all cache.
// clear all cache.
ClearAll
()
error
ClearAll
()
error
// start gc routine
via config string setting
.
// start gc routine
based on config string settings
.
StartAndGC
(
config
string
)
error
StartAndGC
(
config
string
)
error
}
}
...
@@ -57,13 +57,13 @@ func Register(name string, adapter Cache) {
...
@@ -57,13 +57,13 @@ func Register(name string, adapter Cache) {
adapters
[
name
]
=
adapter
adapters
[
name
]
=
adapter
}
}
// Create a new cache driver by adapter and config string.
// 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
)
(
Cache
,
error
)
{
func
NewCache
(
adapterName
,
config
string
)
(
Cache
,
error
)
{
adapter
,
ok
:=
adapters
[
adapterName
]
adapter
,
ok
:=
adapters
[
adapterName
]
if
!
ok
{
if
!
ok
{
return
nil
,
fmt
.
Errorf
(
"cache: unknown adapter
name %q (forgotten
import?)"
,
adapterName
)
return
nil
,
fmt
.
Errorf
(
"cache: unknown adapter
name %q (forgot to
import?)"
,
adapterName
)
}
}
err
:=
adapter
.
StartAndGC
(
config
)
err
:=
adapter
.
StartAndGC
(
config
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
cache/redis/redis.go
View file @
e635e274
...
@@ -46,7 +46,7 @@ func (rc *RedisCache) do(commandName string, args ...interface{}) (reply interfa
...
@@ -46,7 +46,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
*
RedisCache
)
Get
(
key
string
)
interface
{}
{
v
,
err
:=
rc
.
do
(
"
HGET"
,
rc
.
key
,
key
)
v
,
err
:=
rc
.
do
(
"
GET"
,
key
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
return
nil
}
}
...
@@ -55,43 +55,66 @@ func (rc *RedisCache) Get(key string) interface{} {
...
@@ -55,43 +55,66 @@ func (rc *RedisCache) Get(key string) interface{} {
}
}
// put cache to redis.
// put cache to redis.
// timeout is ignored.
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
(
"HSET"
,
rc
.
key
,
key
,
val
)
_
,
err
:=
rc
.
do
(
"SET"
,
key
,
val
)
if
err
!=
nil
{
return
nil
}
_
,
err
=
rc
.
do
(
"HSET"
,
rc
.
key
,
key
,
true
)
if
err
!=
nil
{
return
nil
}
_
,
err
=
rc
.
do
(
"EXPIRE"
,
key
,
timeout
)
return
err
return
err
}
}
// 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
(
"HDEL"
,
rc
.
key
,
key
)
_
,
err
:=
rc
.
do
(
"DEL"
,
key
)
if
err
!=
nil
{
return
nil
}
_
,
err
=
rc
.
do
(
"HDEL"
,
rc
.
key
,
key
)
return
err
return
err
}
}
// check cache
exist
in redis.
// check cache
's existence
in redis.
func
(
rc
*
RedisCache
)
IsExist
(
key
string
)
bool
{
func
(
rc
*
RedisCache
)
IsExist
(
key
string
)
bool
{
v
,
err
:=
redis
.
Bool
(
rc
.
do
(
"
HEXISTS"
,
rc
.
key
,
key
))
v
,
err
:=
redis
.
Bool
(
rc
.
do
(
"
EXISTS"
,
key
))
if
err
!=
nil
{
if
err
!=
nil
{
return
false
return
false
}
}
if
v
==
false
{
_
,
err
:=
rc
.
do
(
"HDEL"
,
rc
.
key
,
key
)
if
err
!=
nil
{
return
false
}
}
return
v
return
v
}
}
// increase counter in redis.
// increase counter in redis.
func
(
rc
*
RedisCache
)
Incr
(
key
string
)
error
{
func
(
rc
*
RedisCache
)
Incr
(
key
string
)
error
{
_
,
err
:=
redis
.
Bool
(
rc
.
do
(
"
HINCRBY"
,
rc
.
key
,
key
,
1
))
_
,
err
:=
redis
.
Bool
(
rc
.
do
(
"
INCRBY"
,
key
,
1
))
return
err
return
err
}
}
// decrease counter in redis.
// decrease counter in redis.
func
(
rc
*
RedisCache
)
Decr
(
key
string
)
error
{
func
(
rc
*
RedisCache
)
Decr
(
key
string
)
error
{
_
,
err
:=
redis
.
Bool
(
rc
.
do
(
"
HINCRBY"
,
rc
.
key
,
key
,
-
1
))
_
,
err
:=
redis
.
Bool
(
rc
.
do
(
"
INCRBY"
,
key
,
-
1
))
return
err
return
err
}
}
// 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
{
_
,
err
:=
rc
.
do
(
"DEL"
,
rc
.
key
)
cachedKeys
,
err
:=
redis
.
Strings
(
rc
.
do
(
"HKEYS"
,
rc
.
key
))
for
_
,
str
:=
range
cachedKeys
{
_
,
err
:=
rc
.
do
(
"DEL"
,
str
)
if
err
!=
nil
{
return
nil
}
}
_
,
err
=
rc
.
do
(
"DEL"
,
rc
.
key
)
return
err
return
err
}
}
...
...
cache/redis/redis_test.go
0 → 100644
View file @
e635e274
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors astaxie
package
cache
import
(
"testing"
"time"
"github.com/beego/redigo/redis"
"github.com/astaxie/beego/cache"
)
func
TestRedisCache
(
t
*
testing
.
T
)
{
bm
,
err
:=
cache
.
NewCache
(
"redis"
,
`{"conn": "127.0.0.1:6379"}`
)
if
err
!=
nil
{
t
.
Error
(
"init err"
)
}
if
err
=
bm
.
Put
(
"astaxie"
,
1
,
10
);
err
!=
nil
{
t
.
Error
(
"set Error"
,
err
)
}
if
!
bm
.
IsExist
(
"astaxie"
)
{
t
.
Error
(
"check err"
)
}
time
.
Sleep
(
10
*
time
.
Second
)
if
bm
.
IsExist
(
"astaxie"
)
{
t
.
Error
(
"check err"
)
}
if
err
=
bm
.
Put
(
"astaxie"
,
1
,
10
);
err
!=
nil
{
t
.
Error
(
"set Error"
,
err
)
}
if
v
,
_
:=
redis
.
Int
(
bm
.
Get
(
"astaxie"
),
err
);
v
!=
1
{
t
.
Error
(
"get err"
)
}
if
err
=
bm
.
Incr
(
"astaxie"
);
err
!=
nil
{
t
.
Error
(
"Incr Error"
,
err
)
}
if
v
,
_
:=
redis
.
Int
(
bm
.
Get
(
"astaxie"
),
err
);
v
!=
2
{
t
.
Error
(
"get err"
)
}
if
err
=
bm
.
Decr
(
"astaxie"
);
err
!=
nil
{
t
.
Error
(
"Decr Error"
,
err
)
}
if
v
,
_
:=
redis
.
Int
(
bm
.
Get
(
"astaxie"
),
err
);
v
!=
1
{
t
.
Error
(
"get err"
)
}
bm
.
Delete
(
"astaxie"
)
if
bm
.
IsExist
(
"astaxie"
)
{
t
.
Error
(
"delete err"
)
}
//test string
if
err
=
bm
.
Put
(
"astaxie"
,
"author"
,
10
);
err
!=
nil
{
t
.
Error
(
"set Error"
,
err
)
}
if
!
bm
.
IsExist
(
"astaxie"
)
{
t
.
Error
(
"check err"
)
}
if
v
,
_
:=
redis
.
String
(
bm
.
Get
(
"astaxie"
),
err
);
v
!=
"author"
{
t
.
Error
(
"get err"
)
}
// test clear all
if
err
=
bm
.
ClearAll
();
err
!=
nil
{
t
.
Error
(
"clear all err"
)
}
}
orm/db_alias.go
View file @
e635e274
...
@@ -139,7 +139,7 @@ func detectTZ(al *alias) {
...
@@ -139,7 +139,7 @@ func detectTZ(al *alias) {
if
engine
!=
""
{
if
engine
!=
""
{
al
.
Engine
=
engine
al
.
Engine
=
engine
}
else
{
}
else
{
e
ngine
=
"INNODB"
al
.
E
ngine
=
"INNODB"
}
}
case
DR_Sqlite
:
case
DR_Sqlite
:
...
...
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