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
174298b4
Commit
174298b4
authored
Jul 04, 2013
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix cache's bug expird is not changed by get method
parent
9b392a06
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
23 additions
and
25 deletions
+23
-25
cache.go
cache/cache.go
+2
-2
cache_test.go
cache/cache_test.go
+2
-2
memcache.go
cache/memcache.go
+1
-1
memory.go
cache/memory.go
+17
-19
redis.go
cache/redis.go
+1
-1
No files found.
cache/cache.go
View file @
174298b4
...
...
@@ -6,7 +6,7 @@ import (
type
Cache
interface
{
Get
(
key
string
)
interface
{}
Put
(
key
string
,
val
interface
{},
timeout
int
)
error
Put
(
key
string
,
val
interface
{},
timeout
int
64
)
error
Delete
(
key
string
)
error
IsExist
(
key
string
)
bool
ClearAll
()
error
...
...
@@ -28,7 +28,7 @@ func Register(name string, adapter Cache) {
adapters
[
name
]
=
adapter
}
// config need to be correct JSON as string: {"interval":360}
// config need to be correct JSON as string: {"interval":360}
func
NewCache
(
adapterName
,
config
string
)
(
Cache
,
error
)
{
adapter
,
ok
:=
adapters
[
adapterName
]
if
!
ok
{
...
...
cache/cache_test.go
View file @
174298b4
...
...
@@ -6,7 +6,7 @@ import (
)
func
Test_cache
(
t
*
testing
.
T
)
{
bm
,
err
:=
NewCache
(
"memory"
,
`{"interval":
6
0}`
)
bm
,
err
:=
NewCache
(
"memory"
,
`{"interval":
2
0}`
)
if
err
!=
nil
{
t
.
Error
(
"init err"
)
}
...
...
@@ -21,7 +21,7 @@ func Test_cache(t *testing.T) {
t
.
Error
(
"get err"
)
}
time
.
Sleep
(
7
0
*
time
.
Second
)
time
.
Sleep
(
3
0
*
time
.
Second
)
if
bm
.
IsExist
(
"astaxie"
)
{
t
.
Error
(
"check err"
)
...
...
cache/memcache.go
View file @
174298b4
...
...
@@ -28,7 +28,7 @@ func (rc *MemcacheCache) Get(key string) interface{} {
return
contain
}
func
(
rc
*
MemcacheCache
)
Put
(
key
string
,
val
interface
{},
timeout
int
)
error
{
func
(
rc
*
MemcacheCache
)
Put
(
key
string
,
val
interface
{},
timeout
int
64
)
error
{
if
rc
.
c
==
nil
{
rc
.
c
=
rc
.
connectInit
()
}
...
...
cache/memory.go
View file @
174298b4
...
...
@@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"strconv"
"sync"
"time"
)
...
...
@@ -16,12 +15,7 @@ var (
type
MemoryItem
struct
{
val
interface
{}
Lastaccess
time
.
Time
expired
int
}
func
(
itm
*
MemoryItem
)
Access
()
interface
{}
{
itm
.
Lastaccess
=
time
.
Now
()
return
itm
.
val
expired
int64
}
type
MemoryCache
struct
{
...
...
@@ -44,13 +38,21 @@ func (bc *MemoryCache) Get(name string) interface{} {
if
!
ok
{
return
nil
}
return
itm
.
Access
()
if
(
time
.
Now
()
.
Unix
()
-
itm
.
Lastaccess
.
Unix
())
>
itm
.
expired
{
go
bc
.
Delete
(
name
)
return
nil
}
return
itm
.
val
}
func
(
bc
*
MemoryCache
)
Put
(
name
string
,
value
interface
{},
expired
int
)
error
{
func
(
bc
*
MemoryCache
)
Put
(
name
string
,
value
interface
{},
expired
int
64
)
error
{
bc
.
lock
.
Lock
()
defer
bc
.
lock
.
Unlock
()
t
:=
MemoryItem
{
val
:
value
,
Lastaccess
:
time
.
Now
(),
expired
:
expired
}
t
:=
MemoryItem
{
val
:
value
,
Lastaccess
:
time
.
Now
(),
expired
:
expired
,
}
if
_
,
ok
:=
bc
.
items
[
name
];
ok
{
return
errors
.
New
(
"the key is exist"
)
}
else
{
...
...
@@ -87,11 +89,11 @@ func (bc *MemoryCache) ClearAll() error {
return
nil
}
// Start activates the file cache; it will
// Start activates the file cache; it will
func
(
bc
*
MemoryCache
)
StartAndGC
(
config
string
)
error
{
var
cf
map
[
string
]
int
json
.
Unmarshal
([]
byte
(
config
),
&
cf
)
if
_
,
ok
:=
cf
[
"
every
"
];
!
ok
{
if
_
,
ok
:=
cf
[
"
interval
"
];
!
ok
{
cf
=
make
(
map
[
string
]
int
)
cf
[
"interval"
]
=
DefaultEvery
}
...
...
@@ -110,7 +112,7 @@ func (bc *MemoryCache) vaccuum() {
return
}
for
{
<-
time
.
After
(
time
.
Duration
(
bc
.
dur
)
*
time
.
Second
)
<-
time
.
After
(
bc
.
dur
)
if
bc
.
items
==
nil
{
return
}
...
...
@@ -128,12 +130,8 @@ func (bc *MemoryCache) item_expired(name string) bool {
if
!
ok
{
return
true
}
dur
:=
time
.
Now
()
.
Sub
(
itm
.
Lastaccess
)
sec
,
err
:=
strconv
.
Atoi
(
fmt
.
Sprintf
(
"%0.0f"
,
dur
.
Seconds
()))
if
err
!=
nil
{
delete
(
bc
.
items
,
name
)
return
true
}
else
if
sec
>=
itm
.
expired
{
sec
:=
time
.
Now
()
.
Unix
()
-
itm
.
Lastaccess
.
Unix
()
if
sec
>=
itm
.
expired
{
delete
(
bc
.
items
,
name
)
return
true
}
...
...
cache/redis.go
View file @
174298b4
...
...
@@ -31,7 +31,7 @@ func (rc *RedisCache) Get(key string) interface{} {
return
v
}
func
(
rc
*
RedisCache
)
Put
(
key
string
,
val
interface
{},
timeout
int
)
error
{
func
(
rc
*
RedisCache
)
Put
(
key
string
,
val
interface
{},
timeout
int
64
)
error
{
if
rc
.
c
==
nil
{
rc
.
c
=
rc
.
connectInit
()
}
...
...
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