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
7f4ad7ff
authored
2013-07-16 19:05:44 +0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
fix #91
1 parent
60200689
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
111 additions
and
0 deletions
cache/cache.go
cache/cache_test.go
cache/memcache.go
cache/memory.go
cache/redis.go
cache/cache.go
View file @
7f4ad7f
...
...
@@ -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
View file @
7f4ad7f
...
...
@@ -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
View file @
7f4ad7f
...
...
@@ -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
View file @
7f4ad7f
...
...
@@ -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
View file @
7f4ad7f
...
...
@@ -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
()
...
...
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