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
970f0b46
authored
2015-06-07 21:33:01 +0800
by
weizili.build17
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Add GetMulti method for Cache interface
1 parent
b9852df5
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
258 additions
and
1 deletions
cache/cache.go
cache/cache_test.go
cache/file.go
cache/memcache/memcache.go
cache/memcache/memcache_test.go
cache/memory.go
cache/redis/redis.go
cache/redis/redis_test.go
cache/cache.go
View file @
970f0b4
...
...
@@ -47,6 +47,8 @@ import (
type
Cache
interface
{
// get cached value by key.
Get
(
key
string
)
interface
{}
// GetMulti is a batch version of Get.
GetMulti
(
keys
[]
string
)
[]
interface
{}
// set cached value with key and expire time.
Put
(
key
string
,
val
interface
{},
timeout
int64
)
error
// delete cached value by key.
...
...
cache/cache_test.go
View file @
970f0b4
...
...
@@ -65,6 +65,35 @@ func TestCache(t *testing.T) {
if
bm
.
IsExist
(
"astaxie"
)
{
t
.
Error
(
"delete err"
)
}
//test GetMulti
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"
)
}
if
err
=
bm
.
Put
(
"astaxie1"
,
"author1"
,
10
);
err
!=
nil
{
t
.
Error
(
"set Error"
,
err
)
}
if
!
bm
.
IsExist
(
"astaxie1"
)
{
t
.
Error
(
"check err"
)
}
vv
:=
bm
.
GetMulti
([]
string
{
"astaxie"
,
"astaxie1"
})
if
len
(
vv
)
!=
2
{
t
.
Error
(
"GetMulti ERROR"
)
}
if
vv
[
0
]
.
(
string
)
!=
"author"
{
t
.
Error
(
"GetMulti ERROR"
)
}
if
vv
[
1
]
.
(
string
)
!=
"author1"
{
t
.
Error
(
"GetMulti ERROR"
)
}
}
func
TestFileCache
(
t
*
testing
.
T
)
{
...
...
@@ -102,6 +131,7 @@ func TestFileCache(t *testing.T) {
if
bm
.
IsExist
(
"astaxie"
)
{
t
.
Error
(
"delete err"
)
}
//test string
if
err
=
bm
.
Put
(
"astaxie"
,
"author"
,
10
);
err
!=
nil
{
t
.
Error
(
"set Error"
,
err
)
...
...
@@ -109,9 +139,28 @@ func TestFileCache(t *testing.T) {
if
!
bm
.
IsExist
(
"astaxie"
)
{
t
.
Error
(
"check err"
)
}
if
v
:=
bm
.
Get
(
"astaxie"
);
v
.
(
string
)
!=
"author"
{
t
.
Error
(
"get err"
)
}
//test GetMulti
if
err
=
bm
.
Put
(
"astaxie1"
,
"author1"
,
10
);
err
!=
nil
{
t
.
Error
(
"set Error"
,
err
)
}
if
!
bm
.
IsExist
(
"astaxie1"
)
{
t
.
Error
(
"check err"
)
}
vv
:=
bm
.
GetMulti
([]
string
{
"astaxie"
,
"astaxie1"
})
if
len
(
vv
)
!=
2
{
t
.
Error
(
"GetMulti ERROR"
)
}
if
vv
[
0
]
.
(
string
)
!=
"author"
{
t
.
Error
(
"GetMulti ERROR"
)
}
if
vv
[
1
]
.
(
string
)
!=
"author1"
{
t
.
Error
(
"GetMulti ERROR"
)
}
os
.
RemoveAll
(
"cache"
)
}
...
...
cache/file.go
View file @
970f0b4
...
...
@@ -132,6 +132,16 @@ func (fc *FileCache) Get(key string) interface{} {
return
to
.
Data
}
// GetMulti gets values from file cache.
// if non-exist or expired, return empty string.
func
(
fc
*
FileCache
)
GetMulti
(
keys
[]
string
)
[]
interface
{}
{
var
rc
[]
interface
{}
for
_
,
key
:=
range
keys
{
rc
=
append
(
rc
,
fc
.
Get
(
key
))
}
return
rc
}
// Put value into file cache.
// timeout means how long to keep this file, unit of ms.
// if timeout equals FileCacheEmbedExpiry(default is 0), cache this item forever.
...
...
cache/memcache/memcache.go
View file @
970f0b4
...
...
@@ -63,6 +63,32 @@ func (rc *MemcacheCache) Get(key string) interface{} {
return
nil
}
// get value from memcache.
func
(
rc
*
MemcacheCache
)
GetMulti
(
keys
[]
string
)
[]
interface
{}
{
size
:=
len
(
keys
)
var
rv
[]
interface
{}
if
rc
.
conn
==
nil
{
if
err
:=
rc
.
connectInit
();
err
!=
nil
{
for
i
:=
0
;
i
<
size
;
i
++
{
rv
=
append
(
rv
,
err
)
}
return
rv
}
}
mv
,
err
:=
rc
.
conn
.
GetMulti
(
keys
)
if
err
==
nil
{
for
_
,
v
:=
range
mv
{
rv
=
append
(
rv
,
string
(
v
.
Value
))
}
return
rv
}
else
{
for
i
:=
0
;
i
<
size
;
i
++
{
rv
=
append
(
rv
,
err
)
}
return
rv
}
}
// put value to memcache. only support string.
func
(
rc
*
MemcacheCache
)
Put
(
key
string
,
val
interface
{},
timeout
int64
)
error
{
if
rc
.
conn
==
nil
{
...
...
cache/memcache/memcache_test.go
0 → 100644
View file @
970f0b4
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package
memcache
import
(
_
"github.com/bradfitz/gomemcache/memcache"
"github.com/astaxie/beego/cache"
"strconv"
"testing"
"time"
)
func
TestRedisCache
(
t
*
testing
.
T
)
{
bm
,
err
:=
cache
.
NewCache
(
"memcache"
,
`{"conn": "127.0.0.1:11211"}`
)
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
,
err
:=
strconv
.
Atoi
(
bm
.
Get
(
"astaxie"
)
.
(
string
));
err
!=
nil
||
v
!=
1
{
t
.
Error
(
"get err"
)
}
if
err
=
bm
.
Incr
(
"astaxie"
);
err
!=
nil
{
t
.
Error
(
"Incr Error"
,
err
)
}
if
v
,
err
:=
strconv
.
Atoi
(
bm
.
Get
(
"astaxie"
)
.
(
string
));
err
!=
nil
||
v
!=
2
{
t
.
Error
(
"get err"
)
}
if
err
=
bm
.
Decr
(
"astaxie"
);
err
!=
nil
{
t
.
Error
(
"Decr Error"
,
err
)
}
if
v
,
err
:=
strconv
.
Atoi
(
bm
.
Get
(
"astaxie"
)
.
(
string
));
err
!=
nil
||
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
:=
bm
.
Get
(
"astaxie"
)
.
(
string
);
v
!=
"author"
{
t
.
Error
(
"get err"
)
}
//test GetMulti
if
err
=
bm
.
Put
(
"astaxie1"
,
"author1"
,
10
);
err
!=
nil
{
t
.
Error
(
"set Error"
,
err
)
}
if
!
bm
.
IsExist
(
"astaxie1"
)
{
t
.
Error
(
"check err"
)
}
vv
:=
bm
.
GetMulti
([]
string
{
"astaxie"
,
"astaxie1"
})
if
len
(
vv
)
!=
2
{
t
.
Error
(
"GetMulti ERROR"
)
}
if
vv
[
0
]
.
(
string
)
!=
"author"
{
t
.
Error
(
"GetMulti ERROR"
)
}
if
vv
[
1
]
.
(
string
)
!=
"author1"
{
t
.
Error
(
"GetMulti ERROR"
)
}
// test clear all
if
err
=
bm
.
ClearAll
();
err
!=
nil
{
t
.
Error
(
"clear all err"
)
}
}
cache/memory.go
View file @
970f0b4
...
...
@@ -64,6 +64,16 @@ func (bc *MemoryCache) Get(name string) interface{} {
return
nil
}
// GetMulti gets caches from memory.
// if non-existed or expired, return nil.
func
(
bc
*
MemoryCache
)
GetMulti
(
names
[]
string
)
[]
interface
{}
{
var
rc
[]
interface
{}
for
_
,
name
:=
range
names
{
rc
=
append
(
rc
,
bc
.
Get
(
name
))
}
return
rc
}
// Put cache to memory.
// if expired is 0, it will be cleaned by next gc operation ( default gc clock is 1 minute).
func
(
bc
*
MemoryCache
)
Put
(
name
string
,
value
interface
{},
expired
int64
)
error
{
...
...
cache/redis/redis.go
View file @
970f0b4
...
...
@@ -74,6 +74,39 @@ func (rc *RedisCache) Get(key string) interface{} {
return
nil
}
// GetMulti get cache from redis.
func
(
rc
*
RedisCache
)
GetMulti
(
keys
[]
string
)
[]
interface
{}
{
size
:=
len
(
keys
)
var
rv
[]
interface
{}
c
:=
rc
.
p
.
Get
()
defer
c
.
Close
()
var
err
error
for
_
,
key
:=
range
keys
{
err
=
c
.
Send
(
"GET"
,
key
)
if
err
!=
nil
{
goto
ERROR
}
}
if
err
=
c
.
Flush
();
err
!=
nil
{
goto
ERROR
}
for
i
:=
0
;
i
<
size
;
i
++
{
if
v
,
err
:=
c
.
Receive
();
err
==
nil
{
rv
=
append
(
rv
,
v
.
([]
byte
))
}
else
{
rv
=
append
(
rv
,
err
)
}
}
return
rv
ERROR
:
rv
=
rv
[
0
:
0
]
for
i
:=
0
;
i
<
size
;
i
++
{
rv
=
append
(
rv
,
nil
)
}
return
rv
}
// put cache to redis.
func
(
rc
*
RedisCache
)
Put
(
key
string
,
val
interface
{},
timeout
int64
)
error
{
var
err
error
...
...
cache/redis/redis_test.go
View file @
970f0b4
...
...
@@ -67,6 +67,7 @@ func TestRedisCache(t *testing.T) {
if
bm
.
IsExist
(
"astaxie"
)
{
t
.
Error
(
"delete err"
)
}
//test string
if
err
=
bm
.
Put
(
"astaxie"
,
"author"
,
10
);
err
!=
nil
{
t
.
Error
(
"set Error"
,
err
)
...
...
@@ -78,6 +79,26 @@ func TestRedisCache(t *testing.T) {
if
v
,
_
:=
redis
.
String
(
bm
.
Get
(
"astaxie"
),
err
);
v
!=
"author"
{
t
.
Error
(
"get err"
)
}
//test GetMulti
if
err
=
bm
.
Put
(
"astaxie1"
,
"author1"
,
10
);
err
!=
nil
{
t
.
Error
(
"set Error"
,
err
)
}
if
!
bm
.
IsExist
(
"astaxie1"
)
{
t
.
Error
(
"check err"
)
}
vv
:=
bm
.
GetMulti
([]
string
{
"astaxie"
,
"astaxie1"
})
if
len
(
vv
)
!=
2
{
t
.
Error
(
"GetMulti ERROR"
)
}
if
v
,
_
:=
redis
.
String
(
vv
[
0
],
nil
);
v
!=
"author"
{
t
.
Error
(
"GetMulti ERROR"
)
}
if
v
,
_
:=
redis
.
String
(
vv
[
1
],
nil
);
v
!=
"author1"
{
t
.
Error
(
"GetMulti ERROR"
)
}
// test clear all
if
err
=
bm
.
ClearAll
();
err
!=
nil
{
t
.
Error
(
"clear all err"
)
...
...
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