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
d7090689
Commit
d7090689
authored
Jul 24, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cache: change the memcache &redis driver
change the memcache to the newest
parent
52d153da
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
30 additions
and
32 deletions
+30
-32
memcache.go
cache/memcache/memcache.go
+28
-30
redis.go
cache/redis/redis.go
+1
-1
redis_test.go
cache/redis/redis_test.go
+1
-1
No files found.
cache/memcache/memcache.go
View file @
d7090689
...
@@ -12,16 +12,17 @@ package memcache
...
@@ -12,16 +12,17 @@ package memcache
import
(
import
(
"encoding/json"
"encoding/json"
"errors"
"errors"
"strings"
"github.com/b
eego
/memcache"
"github.com/b
radfitz/gomemcache
/memcache"
"github.com/astaxie/beego/cache"
"github.com/astaxie/beego/cache"
)
)
// Memcache adapter.
// Memcache adapter.
type
MemcacheCache
struct
{
type
MemcacheCache
struct
{
conn
*
memcache
.
C
onnection
conn
*
memcache
.
C
lient
conninfo
string
conninfo
[]
string
}
}
// create new memcache adapter.
// create new memcache adapter.
...
@@ -36,10 +37,8 @@ func (rc *MemcacheCache) Get(key string) interface{} {
...
@@ -36,10 +37,8 @@ func (rc *MemcacheCache) Get(key string) interface{} {
return
err
return
err
}
}
}
}
if
v
,
err
:=
rc
.
conn
.
Get
(
key
);
err
==
nil
{
if
item
,
err
:=
rc
.
conn
.
Get
(
key
);
err
==
nil
{
if
len
(
v
)
>
0
{
return
string
(
item
.
Value
)
return
string
(
v
[
0
]
.
Value
)
}
}
}
return
nil
return
nil
}
}
...
@@ -55,11 +54,8 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
...
@@ -55,11 +54,8 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
if
!
ok
{
if
!
ok
{
return
errors
.
New
(
"val must string"
)
return
errors
.
New
(
"val must string"
)
}
}
stored
,
err
:=
rc
.
conn
.
Set
(
key
,
0
,
uint64
(
timeout
),
[]
byte
(
v
))
item
:=
memcache
.
Item
{
Key
:
key
,
Value
:
[]
byte
(
v
),
Expiration
:
int32
(
timeout
)}
if
err
==
nil
&&
stored
==
false
{
return
rc
.
conn
.
Set
(
&
item
)
return
errors
.
New
(
"stored fail"
)
}
return
err
}
}
// delete value in memcache.
// delete value in memcache.
...
@@ -69,20 +65,29 @@ func (rc *MemcacheCache) Delete(key string) error {
...
@@ -69,20 +65,29 @@ func (rc *MemcacheCache) Delete(key string) error {
return
err
return
err
}
}
}
}
_
,
err
:=
rc
.
conn
.
Delete
(
key
)
return
rc
.
conn
.
Delete
(
key
)
return
err
}
}
// [Not Support]
// increase counter.
// increase counter.
func
(
rc
*
MemcacheCache
)
Incr
(
_
string
)
error
{
func
(
rc
*
MemcacheCache
)
Incr
(
key
string
)
error
{
return
errors
.
New
(
"not support in memcache"
)
if
rc
.
conn
==
nil
{
if
err
:=
rc
.
connectInit
();
err
!=
nil
{
return
err
}
}
_
,
err
:=
rc
.
conn
.
Increment
(
key
,
1
)
return
err
}
}
// [Not Support]
// decrease counter.
// decrease counter.
func
(
rc
*
MemcacheCache
)
Decr
(
_
string
)
error
{
func
(
rc
*
MemcacheCache
)
Decr
(
key
string
)
error
{
return
errors
.
New
(
"not support in memcache"
)
if
rc
.
conn
==
nil
{
if
err
:=
rc
.
connectInit
();
err
!=
nil
{
return
err
}
}
_
,
err
:=
rc
.
conn
.
Decrement
(
key
,
1
)
return
err
}
}
// check value exists in memcache.
// check value exists in memcache.
...
@@ -92,13 +97,10 @@ func (rc *MemcacheCache) IsExist(key string) bool {
...
@@ -92,13 +97,10 @@ func (rc *MemcacheCache) IsExist(key string) bool {
return
false
return
false
}
}
}
}
v
,
err
:=
rc
.
conn
.
Get
(
key
)
_
,
err
:=
rc
.
conn
.
Get
(
key
)
if
err
!=
nil
{
if
err
!=
nil
{
return
false
return
false
}
}
if
len
(
v
)
==
0
{
return
false
}
return
true
return
true
}
}
...
@@ -121,7 +123,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error {
...
@@ -121,7 +123,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error {
if
_
,
ok
:=
cf
[
"conn"
];
!
ok
{
if
_
,
ok
:=
cf
[
"conn"
];
!
ok
{
return
errors
.
New
(
"config has no conn key"
)
return
errors
.
New
(
"config has no conn key"
)
}
}
rc
.
conninfo
=
cf
[
"conn"
]
rc
.
conninfo
=
strings
.
Split
(
cf
[
"conn"
],
";"
)
if
rc
.
conn
==
nil
{
if
rc
.
conn
==
nil
{
if
err
:=
rc
.
connectInit
();
err
!=
nil
{
if
err
:=
rc
.
connectInit
();
err
!=
nil
{
return
err
return
err
...
@@ -132,11 +134,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error {
...
@@ -132,11 +134,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error {
// connect to memcache and keep the connection.
// connect to memcache and keep the connection.
func
(
rc
*
MemcacheCache
)
connectInit
()
error
{
func
(
rc
*
MemcacheCache
)
connectInit
()
error
{
c
,
err
:=
memcache
.
Connect
(
rc
.
conninfo
)
rc
.
conn
=
memcache
.
New
(
rc
.
conninfo
...
)
if
err
!=
nil
{
return
err
}
rc
.
conn
=
c
return
nil
return
nil
}
}
...
...
cache/redis/redis.go
View file @
d7090689
...
@@ -14,7 +14,7 @@ import (
...
@@ -14,7 +14,7 @@ import (
"errors"
"errors"
"time"
"time"
"github.com/
beego
/redigo/redis"
"github.com/
garyburd
/redigo/redis"
"github.com/astaxie/beego/cache"
"github.com/astaxie/beego/cache"
)
)
...
...
cache/redis/redis_test.go
View file @
d7090689
...
@@ -14,7 +14,7 @@ import (
...
@@ -14,7 +14,7 @@ import (
"testing"
"testing"
"time"
"time"
"github.com/
beego
/redigo/redis"
"github.com/
garyburd
/redigo/redis"
"github.com/astaxie/beego/cache"
"github.com/astaxie/beego/cache"
)
)
...
...
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