Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
7f4ad7ff
B
beego
项目概览
二次元的罪恶王冠
/
beego
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
beego
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7f4ad7ff
编写于
7月 16, 2013
作者:
A
astaxie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix #91
上级
60200689
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
111 addition
and
0 deletion
+111
-0
cache/cache.go
cache/cache.go
+2
-0
cache/cache_test.go
cache/cache_test.go
+15
-0
cache/memcache.go
cache/memcache.go
+8
-0
cache/memory.go
cache/memory.go
+64
-0
cache/redis.go
cache/redis.go
+22
-0
未找到文件。
cache/cache.go
浏览文件 @
7f4ad7ff
...
...
@@ -8,6 +8,8 @@ type Cache interface {
Get
(
key
string
)
interface
{}
Put
(
key
string
,
val
interface
{},
timeout
int64
)
error
Delete
(
key
string
)
error
Incr
(
key
string
)
error
Decr
(
key
string
)
error
IsExist
(
key
string
)
bool
ClearAll
()
error
StartAndGC
(
config
string
)
error
...
...
cache/cache_test.go
浏览文件 @
7f4ad7ff
...
...
@@ -31,6 +31,21 @@ func Test_cache(t *testing.T) {
t
.
Error
(
"set Error"
,
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"
)
...
...
cache/memcache.go
浏览文件 @
7f4ad7ff
...
...
@@ -51,6 +51,14 @@ func (rc *MemcacheCache) Delete(key string) error {
return
err
}
func
(
rc
*
MemcacheCache
)
Incr
(
key
string
)
error
{
return
errors
.
New
(
"not support in memcache"
)
}
func
(
rc
*
MemcacheCache
)
Decr
(
key
string
)
error
{
return
errors
.
New
(
"not support in memcache"
)
}
func
(
rc
*
MemcacheCache
)
IsExist
(
key
string
)
bool
{
if
rc
.
c
==
nil
{
rc
.
c
=
rc
.
connectInit
()
...
...
cache/memory.go
浏览文件 @
7f4ad7ff
...
...
@@ -75,6 +75,70 @@ func (bc *MemoryCache) Delete(name string) error {
return
nil
}
func
(
bc
*
MemoryCache
)
Incr
(
key
string
)
error
{
bc
.
lock
.
RLock
()
defer
bc
.
lock
.
RUnlock
()
itm
,
ok
:=
bc
.
items
[
key
]
if
!
ok
{
return
errors
.
New
(
"key not exist"
)
}
switch
itm
.
val
.
(
type
)
{
case
int
:
itm
.
val
=
itm
.
val
.
(
int
)
+
1
case
int64
:
itm
.
val
=
itm
.
val
.
(
int64
)
+
1
case
int32
:
itm
.
val
=
itm
.
val
.
(
int32
)
+
1
case
uint
:
itm
.
val
=
itm
.
val
.
(
uint
)
+
1
case
uint32
:
itm
.
val
=
itm
.
val
.
(
uint32
)
+
1
case
uint64
:
itm
.
val
=
itm
.
val
.
(
uint64
)
+
1
default
:
return
errors
.
New
(
"item val is not int int64 int32"
)
}
return
nil
}
func
(
bc
*
MemoryCache
)
Decr
(
key
string
)
error
{
bc
.
lock
.
RLock
()
defer
bc
.
lock
.
RUnlock
()
itm
,
ok
:=
bc
.
items
[
key
]
if
!
ok
{
return
errors
.
New
(
"key not exist"
)
}
switch
itm
.
val
.
(
type
)
{
case
int
:
itm
.
val
=
itm
.
val
.
(
int
)
-
1
case
int64
:
itm
.
val
=
itm
.
val
.
(
int64
)
-
1
case
int32
:
itm
.
val
=
itm
.
val
.
(
int32
)
-
1
case
uint
:
if
itm
.
val
.
(
uint
)
>
0
{
itm
.
val
=
itm
.
val
.
(
uint
)
-
1
}
else
{
return
errors
.
New
(
"item val is less than 0"
)
}
case
uint32
:
if
itm
.
val
.
(
uint32
)
>
0
{
itm
.
val
=
itm
.
val
.
(
uint32
)
-
1
}
else
{
return
errors
.
New
(
"item val is less than 0"
)
}
case
uint64
:
if
itm
.
val
.
(
uint64
)
>
0
{
itm
.
val
=
itm
.
val
.
(
uint64
)
-
1
}
else
{
return
errors
.
New
(
"item val is less than 0"
)
}
default
:
return
errors
.
New
(
"item val is not int int64 int32"
)
}
return
nil
}
func
(
bc
*
MemoryCache
)
IsExist
(
name
string
)
bool
{
bc
.
lock
.
RLock
()
defer
bc
.
lock
.
RUnlock
()
...
...
cache/redis.go
浏览文件 @
7f4ad7ff
...
...
@@ -58,6 +58,28 @@ func (rc *RedisCache) IsExist(key string) bool {
return
v
}
func
(
rc
*
RedisCache
)
Incr
(
key
string
)
error
{
if
rc
.
c
==
nil
{
rc
.
c
=
rc
.
connectInit
()
}
_
,
err
:=
redis
.
Bool
(
rc
.
c
.
Do
(
"HINCRBY"
,
rc
.
key
,
key
,
1
))
if
err
!=
nil
{
return
err
}
return
nil
}
func
(
rc
*
RedisCache
)
Decr
(
key
string
)
error
{
if
rc
.
c
==
nil
{
rc
.
c
=
rc
.
connectInit
()
}
_
,
err
:=
redis
.
Bool
(
rc
.
c
.
Do
(
"HINCRBY"
,
rc
.
key
,
key
,
-
1
))
if
err
!=
nil
{
return
err
}
return
nil
}
func
(
rc
*
RedisCache
)
ClearAll
()
error
{
if
rc
.
c
==
nil
{
rc
.
c
=
rc
.
connectInit
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录