Skip to content

Commit

Permalink
update demo
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Sep 14, 2020
1 parent db6cc8a commit 29d9768
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 67 deletions.
66 changes: 46 additions & 20 deletions custom/cache/check.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"errors"
"fmt"
"github.com/zouyx/agollo/v4"
"github.com/zouyx/agollo/v4/agcache"
"github.com/zouyx/agollo/v4/env/config"
"sync"
)

func main() {
Expand All @@ -15,13 +18,14 @@ func main() {
IsBackupConfig: false,
Secret: "6ce3ff7e96a24335a9634fe9abca6d51",
}
agollo.SetLogger(&DefaultLogger{})

client,error:=agollo.StartWithConfig(func() (*config.AppConfig, error) {
agollo.SetCache(&DefaultCacheFactory{})

client,err:=agollo.StartWithConfig(func() (*config.AppConfig, error) {
return c, nil
})

fmt.Println("err:", error)
fmt.Println("err:", err)

writeConfig(c.NamespaceName,client)
}
Expand All @@ -34,36 +38,58 @@ func writeConfig(namespace string,client *agollo.Client) {
})
}

type DefaultLogger struct {
//DefaultCache 默认缓存
type DefaultCache struct {
defaultCache sync.Map
}

func (this *DefaultLogger) Debugf(format string, params ...interface{}) {
this.Debug(format, params)
//Set 获取缓存
func (d *DefaultCache)Set(key string, value interface{}, expireSeconds int) (err error) {
d.defaultCache.Store(key,value)
return nil
}

func (this *DefaultLogger) Infof(format string, params ...interface{}) {
this.Debug(format, params)
//EntryCount 获取实体数量
func (d *DefaultCache)EntryCount() (entryCount int64){
count:=int64(0)
d.defaultCache.Range(func(key, value interface{}) bool {
count++
return true
})
return count
}

func (this *DefaultLogger) Warnf(format string, params ...interface{}) {
this.Debug(format, params)
//Get 获取缓存
func (d *DefaultCache)Get(key string) (value interface{}, err error){
v, ok := d.defaultCache.Load(key)
if !ok{
return nil,errors.New("load default cache fail")
}
return v.([]byte),nil
}

func (this *DefaultLogger) Errorf(format string, params ...interface{}) {
this.Debug(format, params)
//Range 遍历缓存
func (d *DefaultCache)Range(f func(key, value interface{}) bool){
d.defaultCache.Range(f)
}

func (this *DefaultLogger) Debug(v ...interface{}) {
fmt.Println(v)
//Del 删除缓存
func (d *DefaultCache)Del(key string) (affected bool) {
d.defaultCache.Delete(key)
return true
}
func (this *DefaultLogger) Info(v ...interface{}) {
this.Debug(v)

//Clear 清除所有缓存
func (d *DefaultCache)Clear() {
d.defaultCache=sync.Map{}
}

func (this *DefaultLogger) Warn(v ...interface{}) {
this.Debug(v)
//DefaultCacheFactory 构造默认缓存组件工厂类
type DefaultCacheFactory struct {

}

func (this *DefaultLogger) Error(v ...interface{}) {
this.Debug(v)
//Create 创建默认缓存组件
func (d *DefaultCacheFactory) Create()agcache.CacheInterface {
return &DefaultCache{}
}
67 changes: 21 additions & 46 deletions custom/log/check.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package main

import (
"errors"
"fmt"
"github.com/zouyx/agollo/v4"
"github.com/zouyx/agollo/v4/agcache"
"github.com/zouyx/agollo/v4/env/config"
"sync"
)


func main() {
c := &config.AppConfig{
AppID: "testApplication_yang",
Expand All @@ -18,14 +16,13 @@ func main() {
IsBackupConfig: false,
Secret: "6ce3ff7e96a24335a9634fe9abca6d51",
}
agollo.SetLogger(&DefaultLogger{})

agollo.SetCache(&DefaultCacheFactory{})

client,err:=agollo.StartWithConfig(func() (*config.AppConfig, error) {
client,error:=agollo.StartWithConfig(func() (*config.AppConfig, error) {
return c, nil
})

fmt.Println("err:", err)
fmt.Println("err:", error)

writeConfig(c.NamespaceName,client)
}
Expand All @@ -38,58 +35,36 @@ func writeConfig(namespace string,client *agollo.Client) {
})
}

//DefaultCache 默认缓存
type DefaultCache struct {
defaultCache sync.Map
type DefaultLogger struct {
}

//Set 获取缓存
func (d *DefaultCache)Set(key string, value interface{}, expireSeconds int) (err error) {
d.defaultCache.Store(key,value)
return nil
func (this *DefaultLogger) Debugf(format string, params ...interface{}) {
this.Debug(format, params)
}

//EntryCount 获取实体数量
func (d *DefaultCache)EntryCount() (entryCount int64){
count:=int64(0)
d.defaultCache.Range(func(key, value interface{}) bool {
count++
return true
})
return count
func (this *DefaultLogger) Infof(format string, params ...interface{}) {
this.Debug(format, params)
}

//Get 获取缓存
func (d *DefaultCache)Get(key string) (value interface{}, err error){
v, ok := d.defaultCache.Load(key)
if !ok{
return nil,errors.New("load default cache fail")
}
return v.([]byte),nil
func (this *DefaultLogger) Warnf(format string, params ...interface{}) {
this.Debug(format, params)
}

//Range 遍历缓存
func (d *DefaultCache)Range(f func(key, value interface{}) bool){
d.defaultCache.Range(f)
func (this *DefaultLogger) Errorf(format string, params ...interface{}) {
this.Debug(format, params)
}

//Del 删除缓存
func (d *DefaultCache)Del(key string) (affected bool) {
d.defaultCache.Delete(key)
return true
func (this *DefaultLogger) Debug(v ...interface{}) {
fmt.Println(v)
}

//Clear 清除所有缓存
func (d *DefaultCache)Clear() {
d.defaultCache=sync.Map{}
func (this *DefaultLogger) Info(v ...interface{}) {
this.Debug(v)
}

//DefaultCacheFactory 构造默认缓存组件工厂类
type DefaultCacheFactory struct {

func (this *DefaultLogger) Warn(v ...interface{}) {
this.Debug(v)
}

//Create 创建默认缓存组件
func (d *DefaultCacheFactory) Create()agcache.CacheInterface {
return &DefaultCache{}
func (this *DefaultLogger) Error(v ...interface{}) {
this.Debug(v)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/zouyx/agollo_demo

require github.com/zouyx/agollo/v4 v4.0.0-20200913150511-a6a7f5445933
require github.com/zouyx/agollo/v4 v4.0.0-20200914102923-3ec4472360b3

go 1.13
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/zouyx/agollo/v4 v4.0.0-20200913150511-a6a7f5445933 h1:+Lcg+8K5Xncc/70A3cd/igwVodIeQ9wvwUCd5NUPpyw=
github.com/zouyx/agollo/v4 v4.0.0-20200913150511-a6a7f5445933/go.mod h1:unhojnZiGLlT4gLpWz3Oa7sGcChZWv/1DJBkV6s8uAE=
github.com/zouyx/agollo/v4 v4.0.0-20200914102923-3ec4472360b3 h1:AlsLpXoosCGkfHNFnpg70ncrfF4d/lVSgL8s0iAyi0I=
github.com/zouyx/agollo/v4 v4.0.0-20200914102923-3ec4472360b3/go.mod h1:unhojnZiGLlT4gLpWz3Oa7sGcChZWv/1DJBkV6s8uAE=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
Expand Down

0 comments on commit 29d9768

Please sign in to comment.