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
e635e274
authored
2014-07-09 08:43:37 +0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Plain Diff
Merge branch 'master' into develop
2 parents
46cde6e5
cec151fd
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
119 additions
and
15 deletions
cache/cache.go
cache/redis/redis.go
cache/redis/redis_test.go
orm/db_alias.go
cache/cache.go
View file @
e635e27
...
...
@@ -34,11 +34,11 @@ type Cache interface {
Incr
(
key
string
)
error
// decrease cached int value by key, as a counter.
Decr
(
key
string
)
error
// check
cached value is existed
or not.
// check
if cached value exists
or not.
IsExist
(
key
string
)
bool
// clear all cache.
ClearAll
()
error
// start gc routine
via config string setting
.
// start gc routine
based on config string settings
.
StartAndGC
(
config
string
)
error
}
...
...
@@ -57,13 +57,13 @@ func Register(name string, adapter Cache) {
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}.
// it will start gc automatically.
func
NewCache
(
adapterName
,
config
string
)
(
Cache
,
error
)
{
adapter
,
ok
:=
adapters
[
adapterName
]
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
)
if
err
!=
nil
{
...
...
cache/redis/redis.go
View file @
e635e27
...
...
@@ -46,7 +46,7 @@ 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
(
"
HGET"
,
rc
.
key
,
key
)
v
,
err
:=
rc
.
do
(
"
GET"
,
key
)
if
err
!=
nil
{
return
nil
}
...
...
@@ -55,43 +55,66 @@ func (rc *RedisCache) Get(key string) interface{} {
}
// put cache to redis.
// timeout is ignored.
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
}
// delete cache in redis.
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
}
// check cache
exist
in redis.
// check cache
's existence
in redis.
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
{
return
false
}
if
v
==
false
{
_
,
err
:=
rc
.
do
(
"HDEL"
,
rc
.
key
,
key
)
if
err
!=
nil
{
return
false
}
}
return
v
}
// increase counter in redis.
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
}
// decrease counter in redis.
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
}
// clean all cache in redis. delete this redis collection.
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
}
...
...
cache/redis/redis_test.go
0 → 100644
View file @
e635e27
// 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 @
e635e27
...
...
@@ -139,7 +139,7 @@ func detectTZ(al *alias) {
if
engine
!=
""
{
al
.
Engine
=
engine
}
else
{
e
ngine
=
"INNODB"
al
.
E
ngine
=
"INNODB"
}
case
DR_Sqlite
:
...
...
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