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
512d1156
Commit
512d1156
authored
Mar 14, 2013
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update cache add mutex
parent
3b890713
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
23 deletions
+19
-23
cache.go
cache.go
+19
-23
No files found.
cache.go
View file @
512d1156
...
...
@@ -4,27 +4,14 @@ import (
"errors"
"fmt"
"strconv"
"sync"
"time"
)
const
(
Kb
=
1024
Mb
=
1024
*
1024
Gb
=
1024
*
1024
*
1024
)
var
(
DefaultEvery
int
=
60
// 1 minute
)
var
(
InvalidCacheItem
=
errors
.
New
(
"invalid cache item"
)
ItemIsDirectory
=
errors
.
New
(
"can't cache a directory"
)
ItemNotInCache
=
errors
.
New
(
"item not in cache"
)
ItemTooLarge
=
errors
.
New
(
"item too large for cache"
)
WriteIncomplete
=
errors
.
New
(
"incomplete write of cache item"
)
)
type
BeeItem
struct
{
val
interface
{}
Lastaccess
time
.
Time
...
...
@@ -37,6 +24,7 @@ func (itm *BeeItem) Access() interface{} {
}
type
BeeCache
struct
{
lock
sync
.
RWMutex
dur
time
.
Duration
items
map
[
string
]
*
BeeItem
Every
int
// Run an expiration check Every seconds
...
...
@@ -44,13 +32,14 @@ type BeeCache struct {
// NewDefaultCache returns a new FileCache with sane defaults.
func
NewBeeCache
()
*
BeeCache
{
cache
:=
BeeCache
{
time
.
Since
(
time
.
Now
()),
nil
,
DefaultEvery
}
cache
:=
BeeCache
{
dur
:
time
.
Since
(
time
.
Now
()),
Every
:
DefaultEvery
}
return
&
cache
}
func
(
bc
*
BeeCache
)
Get
(
name
string
)
interface
{}
{
bc
.
lock
.
RLock
()
defer
bc
.
lock
.
RUnlock
()
itm
,
ok
:=
bc
.
items
[
name
]
if
!
ok
{
return
nil
...
...
@@ -59,8 +48,10 @@ func (bc *BeeCache) Get(name string) interface{} {
}
func
(
bc
*
BeeCache
)
Put
(
name
string
,
value
interface
{},
expired
int
)
error
{
bc
.
lock
.
Lock
()
defer
bc
.
lock
.
Unlock
()
t
:=
BeeItem
{
val
:
value
,
Lastaccess
:
time
.
Now
(),
expired
:
expired
}
if
bc
.
IsExist
(
name
)
{
if
_
,
ok
:=
bc
.
items
[
name
];
!
ok
{
return
errors
.
New
(
"the key is exist"
)
}
else
{
bc
.
items
[
name
]
=
&
t
...
...
@@ -69,8 +60,9 @@ func (bc *BeeCache) Put(name string, value interface{}, expired int) error {
}
func
(
bc
*
BeeCache
)
Delete
(
name
string
)
(
ok
bool
,
err
error
)
{
_
,
ok
=
bc
.
items
[
name
]
if
!
ok
{
bc
.
lock
.
Lock
()
defer
bc
.
lock
.
Unlock
()
if
_
,
ok
=
bc
.
items
[
name
];
!
ok
{
return
}
delete
(
bc
.
items
,
name
)
...
...
@@ -82,6 +74,8 @@ func (bc *BeeCache) Delete(name string) (ok bool, err error) {
}
func
(
bc
*
BeeCache
)
IsExist
(
name
string
)
bool
{
bc
.
lock
.
RLock
()
defer
bc
.
lock
.
RUnlock
()
_
,
ok
:=
bc
.
items
[
name
]
return
ok
}
...
...
@@ -108,15 +102,15 @@ func (bc *BeeCache) vaccuum() {
return
}
for
name
,
_
:=
range
bc
.
items
{
if
bc
.
item_expired
(
name
)
{
delete
(
bc
.
items
,
name
)
}
bc
.
item_expired
(
name
)
}
}
}
// item_expired returns true if an item is expired.
func
(
bc
*
BeeCache
)
item_expired
(
name
string
)
bool
{
bc
.
lock
.
Lock
()
defer
bc
.
lock
.
Unlock
()
itm
,
ok
:=
bc
.
items
[
name
]
if
!
ok
{
return
true
...
...
@@ -124,8 +118,10 @@ func (bc *BeeCache) item_expired(name string) bool {
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
{
delete
(
bc
.
items
,
name
)
return
true
}
return
false
...
...
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