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
480aa521
Commit
480aa521
authored
Jan 01, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix #430
parent
d57557dc
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
6 deletions
+54
-6
cache_test.go
cache/cache_test.go
+49
-1
file.go
cache/file.go
+5
-5
No files found.
cache/cache_test.go
View file @
480aa521
...
...
@@ -5,7 +5,7 @@ import (
"time"
)
func
Test
_c
ache
(
t
*
testing
.
T
)
{
func
Test
C
ache
(
t
*
testing
.
T
)
{
bm
,
err
:=
NewCache
(
"memory"
,
`{"interval":20}`
)
if
err
!=
nil
{
t
.
Error
(
"init err"
)
...
...
@@ -51,3 +51,51 @@ func Test_cache(t *testing.T) {
t
.
Error
(
"delete err"
)
}
}
func
TestFileCache
(
t
*
testing
.
T
)
{
bm
,
err
:=
NewCache
(
"file"
,
`{"CachePath":"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}`
)
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"
)
}
if
v
:=
bm
.
Get
(
"astaxie"
);
v
.
(
int
)
!=
1
{
t
.
Error
(
"get err"
)
}
if
err
=
bm
.
Incr
(
"astaxie"
);
err
!=
nil
{
t
.
Error
(
"Incr Error"
,
err
)
}
if
v
:=
bm
.
Get
(
"astaxie"
);
v
.
(
int
)
!=
2
{
t
.
Error
(
"get err"
)
}
if
err
=
bm
.
Decr
(
"astaxie"
);
err
!=
nil
{
t
.
Error
(
"Incr Error"
,
err
)
}
if
v
:=
bm
.
Get
(
"astaxie"
);
v
.
(
int
)
!=
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
:=
bm
.
Get
(
"astaxie"
);
v
.
(
string
)
!=
"author"
{
t
.
Error
(
"get err"
)
}
}
cache/file.go
View file @
480aa521
...
...
@@ -61,6 +61,7 @@ func (this *FileCache) StartAndGC(config string) error {
var
cfg
map
[
string
]
string
json
.
Unmarshal
([]
byte
(
config
),
&
cfg
)
//fmt.Println(cfg)
//fmt.Println(config)
if
_
,
ok
:=
cfg
[
"CachePath"
];
!
ok
{
cfg
[
"CachePath"
]
=
FileCachePath
}
...
...
@@ -135,7 +136,7 @@ func (this *FileCache) Get(key string) interface{} {
return
""
}
var
to
FileCacheItem
Gob_decode
(
[]
byte
(
filedata
)
,
&
to
)
Gob_decode
(
filedata
,
&
to
)
if
to
.
Expired
<
time
.
Now
()
.
Unix
()
{
return
""
}
...
...
@@ -177,7 +178,7 @@ func (this *FileCache) Delete(key string) error {
func
(
this
*
FileCache
)
Incr
(
key
string
)
error
{
data
:=
this
.
Get
(
key
)
var
incr
int
fmt
.
Println
(
reflect
.
TypeOf
(
data
)
.
Name
())
//
fmt.Println(reflect.TypeOf(data).Name())
if
reflect
.
TypeOf
(
data
)
.
Name
()
!=
"int"
{
incr
=
0
}
else
{
...
...
@@ -210,8 +211,7 @@ func (this *FileCache) IsExist(key string) bool {
// Clean cached files.
// not implemented.
func
(
this
*
FileCache
)
ClearAll
()
error
{
//this.CachePath .递归删除
//this.CachePath
return
nil
}
...
...
@@ -271,7 +271,7 @@ func Gob_encode(data interface{}) ([]byte, error) {
}
// Gob decodes file cache item.
func
Gob_decode
(
data
[]
byte
,
to
interface
{}
)
error
{
func
Gob_decode
(
data
[]
byte
,
to
*
FileCacheItem
)
error
{
buf
:=
bytes
.
NewBuffer
(
data
)
dec
:=
gob
.
NewDecoder
(
buf
)
return
dec
.
Decode
(
&
to
)
...
...
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