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
a03fa0fb
Commit
a03fa0fb
authored
Jan 04, 2016
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve cache modules. support mulit instances
parent
92d15773
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
18 additions
and
15 deletions
+18
-15
cache.go
cache/cache.go
+7
-4
file.go
cache/file.go
+5
-5
memcache.go
cache/memcache/memcache.go
+2
-2
memory.go
cache/memory.go
+2
-2
redis.go
cache/redis/redis.go
+2
-2
No files found.
cache/cache.go
View file @
a03fa0fb
...
...
@@ -37,7 +37,7 @@ import (
// Cache interface contains all behaviors for cache adapter.
// usage:
// cache.Register("file",cache.NewFileCache
()
) // this operation is run in init method of file.go.
// cache.Register("file",cache.NewFileCache) // this operation is run in init method of file.go.
// c,err := cache.NewCache("file","{....}")
// c.Put("key",value,3600)
// v := c.Get("key")
...
...
@@ -66,12 +66,14 @@ type Cache interface {
StartAndGC
(
config
string
)
error
}
var
adapters
=
make
(
map
[
string
]
Cache
)
type
CacheInstance
func
()
Cache
var
adapters
=
make
(
map
[
string
]
CacheInstance
)
// Register makes a cache adapter available by the adapter name.
// If Register is called twice with the same name or if driver is nil,
// it panics.
func
Register
(
name
string
,
adapter
Cache
)
{
func
Register
(
name
string
,
adapter
Cache
Instance
)
{
if
adapter
==
nil
{
panic
(
"cache: Register adapter is nil"
)
}
...
...
@@ -85,11 +87,12 @@ func Register(name string, adapter Cache) {
// config need to be correct JSON as string: {"interval":360}.
// it will start gc automatically.
func
NewCache
(
adapterName
,
config
string
)
(
adapter
Cache
,
err
error
)
{
adapter
,
ok
:=
adapters
[
adapterName
]
instanceFunc
,
ok
:=
adapters
[
adapterName
]
if
!
ok
{
err
=
fmt
.
Errorf
(
"cache: unknown adapter name %q (forgot to import?)"
,
adapterName
)
return
}
adapter
=
instanceFunc
()
err
=
adapter
.
StartAndGC
(
config
)
if
err
!=
nil
{
adapter
=
nil
...
...
cache/file.go
View file @
a03fa0fb
...
...
@@ -29,10 +29,6 @@ import (
"time"
)
func
init
()
{
Register
(
"file"
,
NewFileCache
())
}
// FileCacheItem is basic unit of file cache adapter.
// it contains data and expire time.
type
FileCacheItem
struct
{
...
...
@@ -59,7 +55,7 @@ type FileCache struct {
// NewFileCache Create new file cache with no config.
// the level and expiry need set in method StartAndGC as config string.
func
NewFileCache
()
*
File
Cache
{
func
NewFileCache
()
Cache
{
// return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix}
return
&
FileCache
{}
}
...
...
@@ -272,3 +268,7 @@ func GobDecode(data []byte, to *FileCacheItem) error {
dec
:=
gob
.
NewDecoder
(
buf
)
return
dec
.
Decode
(
&
to
)
}
func
init
()
{
Register
(
"file"
,
NewFileCache
)
}
cache/memcache/memcache.go
View file @
a03fa0fb
...
...
@@ -46,7 +46,7 @@ type Cache struct {
}
// NewMemCache create new memcache adapter.
func
NewMemCache
()
*
Cache
{
func
NewMemCache
()
cache
.
Cache
{
return
&
Cache
{}
}
...
...
@@ -184,5 +184,5 @@ func (rc *Cache) connectInit() error {
}
func
init
()
{
cache
.
Register
(
"memcache"
,
NewMemCache
()
)
cache
.
Register
(
"memcache"
,
NewMemCache
)
}
cache/memory.go
View file @
a03fa0fb
...
...
@@ -44,7 +44,7 @@ type MemoryCache struct {
}
// NewMemoryCache returns a new MemoryCache.
func
NewMemoryCache
()
*
Memory
Cache
{
func
NewMemoryCache
()
Cache
{
cache
:=
MemoryCache
{
items
:
make
(
map
[
string
]
*
MemoryItem
)}
return
&
cache
}
...
...
@@ -234,5 +234,5 @@ func (bc *MemoryCache) itemExpired(name string) bool {
}
func
init
()
{
Register
(
"memory"
,
NewMemoryCache
()
)
Register
(
"memory"
,
NewMemoryCache
)
}
cache/redis/redis.go
View file @
a03fa0fb
...
...
@@ -55,7 +55,7 @@ type Cache struct {
}
// NewRedisCache create new redis cache with default collection name.
func
NewRedisCache
()
*
Cache
{
func
NewRedisCache
()
cache
.
Cache
{
return
&
Cache
{
key
:
DefaultKey
}
}
...
...
@@ -236,5 +236,5 @@ func (rc *Cache) connectInit() {
}
func
init
()
{
cache
.
Register
(
"redis"
,
NewRedisCache
()
)
cache
.
Register
(
"redis"
,
NewRedisCache
)
}
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