-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # .github/auto-comment.yml
- Loading branch information
Showing
7 changed files
with
88 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,22 @@ | ||
package agcache | ||
|
||
import "github.com/coocood/freecache" | ||
|
||
//CacheInterface 自定义缓存组件接口 | ||
type CacheInterface interface { | ||
Set(key, value []byte, expireSeconds int) (err error) | ||
Set(key string, value []byte, expireSeconds int) (err error) | ||
|
||
EntryCount() (entryCount int64) | ||
|
||
Get(key []byte) (value []byte, err error) | ||
|
||
Del(key []byte) (affected bool) | ||
Get(key string) (value []byte, err error) | ||
|
||
NewIterator() *freecache.Iterator | ||
Del(key string) (affected bool) | ||
|
||
TTL(key []byte) (timeLeft uint32, err error) | ||
Range(f func(key, value interface{}) bool) | ||
|
||
Clear() | ||
} | ||
|
||
const ( | ||
//50m | ||
apolloConfigCacheSize = 50 * 1024 * 1024 | ||
|
||
//1 minute | ||
configCacheExpireTime = 120 | ||
) | ||
|
||
//CacheFactory 缓存组件工厂接口 | ||
type CacheFactory interface { | ||
//Create 创建缓存组件 | ||
Create() CacheInterface | ||
} | ||
|
||
//DefaultCacheFactory 构造默认缓存组件工厂类 | ||
type DefaultCacheFactory struct { | ||
|
||
} | ||
|
||
//Create 创建默认缓存组件 | ||
func (d *DefaultCacheFactory) Create()CacheInterface { | ||
return freecache.NewCache(apolloConfigCacheSize) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package agcache | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
) | ||
|
||
//DefaultCache 默认缓存 | ||
type DefaultCache struct { | ||
defaultCache sync.Map | ||
} | ||
|
||
//Set 获取缓存 | ||
func (d *DefaultCache)Set(key string, value []byte, expireSeconds int) (err error) { | ||
d.defaultCache.Store(key,value) | ||
return nil | ||
} | ||
|
||
//EntryCount 获取实体数量 | ||
func (d *DefaultCache)EntryCount() (entryCount int64){ | ||
count:=int64(0) | ||
d.defaultCache.Range(func(key, value interface{}) bool { | ||
count++ | ||
return true | ||
}) | ||
return count | ||
} | ||
|
||
//Get 获取缓存 | ||
func (d *DefaultCache)Get(key string) (value []byte, err error){ | ||
v, ok := d.defaultCache.Load(key) | ||
if !ok{ | ||
return nil,errors.New("load default cache fail") | ||
} | ||
return v.([]byte),nil | ||
} | ||
|
||
//Range 遍历缓存 | ||
func (d *DefaultCache)Range(f func(key, value interface{}) bool){ | ||
d.defaultCache.Range(f) | ||
} | ||
|
||
//Del 删除缓存 | ||
func (d *DefaultCache)Del(key string) (affected bool) { | ||
d.defaultCache.Delete(key) | ||
return true | ||
} | ||
|
||
//Clear 清除所有缓存 | ||
func (d *DefaultCache)Clear() { | ||
d.defaultCache=sync.Map{} | ||
} | ||
|
||
//DefaultCacheFactory 构造默认缓存组件工厂类 | ||
type DefaultCacheFactory struct { | ||
|
||
} | ||
|
||
//Create 创建默认缓存组件 | ||
func (d *DefaultCacheFactory) Create()CacheInterface { | ||
return &DefaultCache{} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
module github.com/zouyx/agollo | ||
|
||
require ( | ||
github.com/cespare/xxhash v1.1.0 | ||
github.com/coocood/freecache v1.1.0 | ||
github.com/tevid/gohamcrest v1.1.1 | ||
) | ||
require github.com/tevid/gohamcrest v1.1.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters