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
15759f60
Commit
15759f60
authored
Jul 12, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #692 from fuxiaohei/develop
code style simplify
parents
a6379481
0e3e22ef
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
63 deletions
+41
-63
memcache.go
cache/memcache/memcache.go
+31
-49
memory.go
cache/memory.go
+10
-14
No files found.
cache/memcache/memcache.go
View file @
15759f60
...
...
@@ -7,7 +7,7 @@
// @license http://github.com/astaxie/beego/blob/master/LICENSE
//
// @authors astaxie
package
cache
package
mem
cache
import
(
"encoding/json"
...
...
@@ -20,7 +20,7 @@ import (
// Memcache adapter.
type
MemcacheCache
struct
{
c
*
memcache
.
Connection
c
onn
*
memcache
.
Connection
conninfo
string
}
...
...
@@ -31,32 +31,23 @@ func NewMemCache() *MemcacheCache {
// get value from memcache.
func
(
rc
*
MemcacheCache
)
Get
(
key
string
)
interface
{}
{
if
rc
.
c
==
nil
{
var
err
error
rc
.
c
,
err
=
rc
.
connectInit
()
if
err
!=
nil
{
if
rc
.
conn
==
nil
{
if
err
:=
rc
.
connectInit
();
err
!=
nil
{
return
err
}
}
v
,
err
:=
rc
.
c
.
Get
(
key
)
if
err
!=
nil
{
return
nil
}
var
contain
interface
{}
if
len
(
v
)
>
0
{
contain
=
string
(
v
[
0
]
.
Value
)
}
else
{
contain
=
nil
if
v
,
err
:=
rc
.
conn
.
Get
(
key
);
err
==
nil
{
if
len
(
v
)
>
0
{
return
string
(
v
[
0
]
.
Value
)
}
}
return
contain
return
nil
}
// put value to memcache. only support string.
func
(
rc
*
MemcacheCache
)
Put
(
key
string
,
val
interface
{},
timeout
int64
)
error
{
if
rc
.
c
==
nil
{
var
err
error
rc
.
c
,
err
=
rc
.
connectInit
()
if
err
!=
nil
{
if
rc
.
conn
==
nil
{
if
err
:=
rc
.
connectInit
();
err
!=
nil
{
return
err
}
}
...
...
@@ -64,7 +55,7 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
if
!
ok
{
return
errors
.
New
(
"val must string"
)
}
stored
,
err
:=
rc
.
c
.
Set
(
key
,
0
,
uint64
(
timeout
),
[]
byte
(
v
))
stored
,
err
:=
rc
.
c
onn
.
Set
(
key
,
0
,
uint64
(
timeout
),
[]
byte
(
v
))
if
err
==
nil
&&
stored
==
false
{
return
errors
.
New
(
"stored fail"
)
}
...
...
@@ -73,60 +64,52 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
// delete value in memcache.
func
(
rc
*
MemcacheCache
)
Delete
(
key
string
)
error
{
if
rc
.
c
==
nil
{
var
err
error
rc
.
c
,
err
=
rc
.
connectInit
()
if
err
!=
nil
{
if
rc
.
conn
==
nil
{
if
err
:=
rc
.
connectInit
();
err
!=
nil
{
return
err
}
}
_
,
err
:=
rc
.
c
.
Delete
(
key
)
_
,
err
:=
rc
.
c
onn
.
Delete
(
key
)
return
err
}
// [Not Support]
// increase counter.
func
(
rc
*
MemcacheCache
)
Incr
(
key
string
)
error
{
func
(
rc
*
MemcacheCache
)
Incr
(
_
string
)
error
{
return
errors
.
New
(
"not support in memcache"
)
}
// [Not Support]
// decrease counter.
func
(
rc
*
MemcacheCache
)
Decr
(
key
string
)
error
{
func
(
rc
*
MemcacheCache
)
Decr
(
_
string
)
error
{
return
errors
.
New
(
"not support in memcache"
)
}
// check value exists in memcache.
func
(
rc
*
MemcacheCache
)
IsExist
(
key
string
)
bool
{
if
rc
.
c
==
nil
{
var
err
error
rc
.
c
,
err
=
rc
.
connectInit
()
if
err
!=
nil
{
if
rc
.
conn
==
nil
{
if
err
:=
rc
.
connectInit
();
err
!=
nil
{
return
false
}
}
v
,
err
:=
rc
.
c
.
Get
(
key
)
v
,
err
:=
rc
.
c
onn
.
Get
(
key
)
if
err
!=
nil
{
return
false
}
if
len
(
v
)
==
0
{
return
false
}
else
{
return
true
}
return
true
}
// clear all cached in memcache.
func
(
rc
*
MemcacheCache
)
ClearAll
()
error
{
if
rc
.
c
==
nil
{
var
err
error
rc
.
c
,
err
=
rc
.
connectInit
()
if
err
!=
nil
{
if
rc
.
conn
==
nil
{
if
err
:=
rc
.
connectInit
();
err
!=
nil
{
return
err
}
}
err
:=
rc
.
c
.
FlushAll
()
return
err
return
rc
.
conn
.
FlushAll
()
}
// start memcache adapter.
...
...
@@ -139,23 +122,22 @@ func (rc *MemcacheCache) StartAndGC(config string) error {
return
errors
.
New
(
"config has no conn key"
)
}
rc
.
conninfo
=
cf
[
"conn"
]
var
err
error
if
rc
.
c
!=
nil
{
rc
.
c
,
err
=
rc
.
connectInit
()
if
err
!=
nil
{
return
errors
.
New
(
"dial tcp conn error"
)
if
rc
.
conn
==
nil
{
if
err
:=
rc
.
connectInit
();
err
!=
nil
{
return
err
}
}
return
nil
}
// connect to memcache and keep the connection.
func
(
rc
*
MemcacheCache
)
connectInit
()
(
*
memcache
.
Connection
,
error
)
{
func
(
rc
*
MemcacheCache
)
connectInit
()
error
{
c
,
err
:=
memcache
.
Connect
(
rc
.
conninfo
)
if
err
!=
nil
{
return
nil
,
err
return
err
}
return
c
,
nil
rc
.
conn
=
c
return
nil
}
func
init
()
{
...
...
cache/memory.go
View file @
15759f60
...
...
@@ -49,15 +49,14 @@ func NewMemoryCache() *MemoryCache {
func
(
bc
*
MemoryCache
)
Get
(
name
string
)
interface
{}
{
bc
.
lock
.
RLock
()
defer
bc
.
lock
.
RUnlock
()
itm
,
ok
:=
bc
.
items
[
name
]
if
!
ok
{
return
nil
}
if
(
time
.
Now
()
.
Unix
()
-
itm
.
Lastaccess
.
Unix
())
>
itm
.
expired
{
go
bc
.
Delete
(
name
)
return
nil
if
itm
,
ok
:=
bc
.
items
[
name
];
ok
{
if
(
time
.
Now
()
.
Unix
()
-
itm
.
Lastaccess
.
Unix
())
>
itm
.
expired
{
go
bc
.
Delete
(
name
)
return
nil
}
return
itm
.
val
}
return
itm
.
va
l
return
ni
l
}
// Put cache to memory.
...
...
@@ -65,12 +64,11 @@ func (bc *MemoryCache) Get(name string) interface{} {
func
(
bc
*
MemoryCache
)
Put
(
name
string
,
value
interface
{},
expired
int64
)
error
{
bc
.
lock
.
Lock
()
defer
bc
.
lock
.
Unlock
()
t
:=
MemoryItem
{
bc
.
items
[
name
]
=
&
MemoryItem
{
val
:
value
,
Lastaccess
:
time
.
Now
(),
expired
:
expired
,
}
bc
.
items
[
name
]
=
&
t
return
nil
}
...
...
@@ -82,8 +80,7 @@ func (bc *MemoryCache) Delete(name string) error {
return
errors
.
New
(
"key not exist"
)
}
delete
(
bc
.
items
,
name
)
_
,
valid
:=
bc
.
items
[
name
]
if
valid
{
if
_
,
ok
:=
bc
.
items
[
name
];
ok
{
return
errors
.
New
(
"delete key error"
)
}
return
nil
...
...
@@ -214,8 +211,7 @@ func (bc *MemoryCache) item_expired(name string) bool {
if
!
ok
{
return
true
}
sec
:=
time
.
Now
()
.
Unix
()
-
itm
.
Lastaccess
.
Unix
()
if
sec
>=
itm
.
expired
{
if
time
.
Now
()
.
Unix
()
-
itm
.
Lastaccess
.
Unix
()
>=
itm
.
expired
{
delete
(
bc
.
items
,
name
)
return
true
}
...
...
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