Skip to content

Commit

Permalink
move cache to extension
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Apr 18, 2020
1 parent 2e3c39f commit 8fe9aaa
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 51 deletions.
18 changes: 0 additions & 18 deletions agcache/cache.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package agcache

var (
gobalCacheFactory CacheFactory
)

func init() {
gobalCacheFactory = &DefaultCacheFactory{}
}

//CacheInterface 自定义缓存组件接口
type CacheInterface interface {
Set(key string, value []byte, expireSeconds int) (err error)
Expand All @@ -28,13 +20,3 @@ type CacheFactory interface {
//Create 创建缓存组件
Create() CacheInterface
}

//GetCacheFactory 获取CacheFactory
func GetCacheFactory() CacheFactory {
return gobalCacheFactory
}

//UseCacheFactory 替换CacheFactory
func UseCacheFactory(cacheFactory CacheFactory) {
gobalCacheFactory = cacheFactory
}
22 changes: 0 additions & 22 deletions agcache/cache_test.go

This file was deleted.

11 changes: 9 additions & 2 deletions agcache/default.go → agcache/memory/memory.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package agcache
package memory

import (
"errors"
"sync"

"github.com/zouyx/agollo/v3/agcache"
"github.com/zouyx/agollo/v3/extension"
)

func init() {
extension.SetCacheFactory(&DefaultCacheFactory{})
}

//DefaultCache 默认缓存
type DefaultCache struct {
defaultCache sync.Map
Expand Down Expand Up @@ -56,6 +63,6 @@ type DefaultCacheFactory struct {
}

//Create 创建默认缓存组件
func (d *DefaultCacheFactory) Create() CacheInterface {
func (d *DefaultCacheFactory) Create() agcache.CacheInterface {
return &DefaultCache{}
}
5 changes: 3 additions & 2 deletions agcache/default_test.go → agcache/memory/memory_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package agcache
package memory

import (
"testing"

. "github.com/tevid/gohamcrest"
"github.com/zouyx/agollo/v3/agcache"
)

var testDefaultCache CacheInterface
var testDefaultCache agcache.CacheInterface

func init() {
factory := &DefaultCacheFactory{}
Expand Down
1 change: 1 addition & 0 deletions component/notify/change_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

. "github.com/tevid/gohamcrest"
_ "github.com/zouyx/agollo/v3/agcache/memory"
"github.com/zouyx/agollo/v3/env"
jsonFile "github.com/zouyx/agollo/v3/env/file/json"
"github.com/zouyx/agollo/v3/extension"
Expand Down
17 changes: 17 additions & 0 deletions extension/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package extension

import "github.com/zouyx/agollo/v3/agcache"

var (
gobalCacheFactory agcache.CacheFactory
)

//GetCacheFactory 获取CacheFactory
func GetCacheFactory() agcache.CacheFactory {
return gobalCacheFactory
}

//SetCacheFactory 替换CacheFactory
func SetCacheFactory(cacheFactory agcache.CacheFactory) {
gobalCacheFactory = cacheFactory
}
80 changes: 80 additions & 0 deletions extension/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package extension

import (
"errors"
"sync"
"testing"

. "github.com/tevid/gohamcrest"
"github.com/zouyx/agollo/v3/agcache"
)

type TestCacheFactory struct {
}

func (d *TestCacheFactory) Create() agcache.CacheInterface {
return &defaultCache{}
}

//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() agcache.CacheInterface {
return &defaultCache{}
}

func TestUseCacheFactory(t *testing.T) {
SetCacheFactory(&TestCacheFactory{})

factory := GetCacheFactory()
cacheFactory := factory.(*TestCacheFactory)
Assert(t, cacheFactory, NotNilVal())
}
3 changes: 2 additions & 1 deletion start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package agollo

import (
"github.com/zouyx/agollo/v3/agcache"
_ "github.com/zouyx/agollo/v3/agcache/memory"
"github.com/zouyx/agollo/v3/cluster"
_ "github.com/zouyx/agollo/v3/cluster/roundrobin"
"github.com/zouyx/agollo/v3/component"
Expand Down Expand Up @@ -54,7 +55,7 @@ func SetLogger(loggerInterface log.LoggerInterface) {
//SetCache 设置自定义cache组件
func SetCache(cacheFactory agcache.CacheFactory) {
if cacheFactory != nil {
agcache.UseCacheFactory(cacheFactory)
extension.SetCacheFactory(cacheFactory)
storage.InitConfigCache()
}
}
Expand Down
6 changes: 3 additions & 3 deletions start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

. "github.com/tevid/gohamcrest"
"github.com/zouyx/agollo/v3/agcache"
"github.com/zouyx/agollo/v3/agcache/memory"
"github.com/zouyx/agollo/v3/component/log"
"github.com/zouyx/agollo/v3/component/notify"
"github.com/zouyx/agollo/v3/env"
Expand Down Expand Up @@ -177,9 +177,9 @@ func TestSetLogger(t *testing.T) {
}

func TestSetCache(t *testing.T) {
defaultCacheFactory := &agcache.DefaultCacheFactory{}
defaultCacheFactory := &memory.DefaultCacheFactory{}
SetCache(defaultCacheFactory)
Assert(t, agcache.GetCacheFactory(), Equal(defaultCacheFactory))
Assert(t, extension.GetCacheFactory(), Equal(defaultCacheFactory))
}

func TestSetLoadBalance(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions storage/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func CreateNamespaceConfig(namespace string) {
if _, ok := apolloConfigCache.Load(namespace); ok {
return
}
c := initConfig(namespace, agcache.GetCacheFactory())
c := initConfig(namespace, extension.GetCacheFactory())
apolloConfigCache.Store(namespace, c)
})
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func UpdateApolloConfig(apolloConfig *env.ApolloConfig, isBackupConfig bool) {
func UpdateApolloConfigCache(configurations map[string]string, expireTime int, namespace string) map[string]*ConfigChange {
config := GetConfig(namespace)
if config == nil {
config = initConfig(namespace, agcache.GetCacheFactory())
config = initConfig(namespace, extension.GetCacheFactory())
apolloConfigCache.Store(namespace, config)
}

Expand Down
1 change: 1 addition & 0 deletions storage/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

. "github.com/tevid/gohamcrest"
_ "github.com/zouyx/agollo/v3/agcache/memory"
"github.com/zouyx/agollo/v3/env"
jsonFile "github.com/zouyx/agollo/v3/env/file/json"
"github.com/zouyx/agollo/v3/extension"
Expand Down
59 changes: 58 additions & 1 deletion utils/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package utils

import (
"errors"
"strings"
"sync"
"testing"

. "github.com/tevid/gohamcrest"
Expand All @@ -14,8 +16,63 @@ var (
propertiesParser ContentParser
)

//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() agcache.CacheInterface {
return &DefaultCache{}
}

func init() {
factory := &agcache.DefaultCacheFactory{}
factory := &DefaultCacheFactory{}
testDefaultCache = factory.Create()

defaultParser = &DefaultParser{}
Expand Down

0 comments on commit 8fe9aaa

Please sign in to comment.