Skip to content

Commit

Permalink
refactor: 更新 cache 版本
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Apr 26, 2024
1 parent aa15bb0 commit 6131aa7
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 71 deletions.
4 changes: 2 additions & 2 deletions cmd/web/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/issue9/source v0.9.1
github.com/issue9/term/v3 v3.2.8
github.com/issue9/version v1.0.8
github.com/issue9/web v0.93.1
github.com/issue9/web v0.93.2
golang.org/x/mod v0.17.0
golang.org/x/text v0.14.0
golang.org/x/tools v0.20.0
Expand All @@ -30,7 +30,7 @@ require (
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/swag v0.22.8 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/issue9/cache v0.14.0 // indirect
github.com/issue9/cache v0.15.0 // indirect
github.com/issue9/config v0.6.2 // indirect
github.com/issue9/conv v1.3.5 // indirect
github.com/issue9/errwrap v0.3.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions cmd/web/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=
github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
github.com/issue9/assert/v4 v4.3.0 h1:W3XDKmttsfzihYGxJ9rJoL2ViJgWERB9IxfHcxjv65U=
github.com/issue9/assert/v4 v4.3.0/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
github.com/issue9/cache v0.14.0 h1:aCMEeohToxp8vU7mrOskt7qhzrvF6WqZ4TjhIwuLnkk=
github.com/issue9/cache v0.14.0/go.mod h1:bLkT/324kSULoA/3/vAO0H6KILwDLwqGlOA/OvNwdlI=
github.com/issue9/cache v0.15.0 h1:WoBvmkNO7YMTzSGL1DnPGSu5XVNiUKTOoJC2KvALQIQ=
github.com/issue9/cache v0.15.0/go.mod h1:bLkT/324kSULoA/3/vAO0H6KILwDLwqGlOA/OvNwdlI=
github.com/issue9/cmdopt v0.13.1 h1:VA/Hgd92NBbZyHjZx1xcRCMhoc+XjI1LWhiuZkOZ0VU=
github.com/issue9/cmdopt v0.13.1/go.mod h1:7lnF45Ush0boi4/nDjeSDYV5lZfoOzp3jm+biJr0f4g=
github.com/issue9/config v0.6.2 h1:znXvsk6gh0wm+fTEn0zUjjramKuOLY8Jt0ZTxp4GIkc=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/andybalholm/brotli v1.1.0
github.com/fxamacker/cbor/v2 v2.6.0
github.com/issue9/assert/v4 v4.3.0
github.com/issue9/cache v0.14.0
github.com/issue9/cache v0.15.0
github.com/issue9/config v0.6.2
github.com/issue9/conv v1.3.5
github.com/issue9/errwrap v0.3.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ github.com/fxamacker/cbor/v2 v2.6.0 h1:sU6J2usfADwWlYDAFhZBQ6TnLFBHxgesMrQfQgk1t
github.com/fxamacker/cbor/v2 v2.6.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
github.com/issue9/assert/v4 v4.3.0 h1:W3XDKmttsfzihYGxJ9rJoL2ViJgWERB9IxfHcxjv65U=
github.com/issue9/assert/v4 v4.3.0/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
github.com/issue9/cache v0.14.0 h1:aCMEeohToxp8vU7mrOskt7qhzrvF6WqZ4TjhIwuLnkk=
github.com/issue9/cache v0.14.0/go.mod h1:bLkT/324kSULoA/3/vAO0H6KILwDLwqGlOA/OvNwdlI=
github.com/issue9/cache v0.15.0 h1:WoBvmkNO7YMTzSGL1DnPGSu5XVNiUKTOoJC2KvALQIQ=
github.com/issue9/cache v0.15.0/go.mod h1:bLkT/324kSULoA/3/vAO0H6KILwDLwqGlOA/OvNwdlI=
github.com/issue9/config v0.6.2 h1:znXvsk6gh0wm+fTEn0zUjjramKuOLY8Jt0ZTxp4GIkc=
github.com/issue9/config v0.6.2/go.mod h1:S97FVtZim4rPB+M49EoOuBajaqY4ULQbqltZmZ99+Dc=
github.com/issue9/conv v1.3.5 h1:UWeA+Zqp5vjNDLrmhLhaXPjS1hL8gh4quX6Shk5njKQ=
Expand Down
6 changes: 3 additions & 3 deletions server/config/CONFIG.html
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ <h2 id="cacheConfig">cacheConfig</h2>
</td></tr><tr><td>dsn</td><td>dsn</td><td>dsn</td><td>string</td><td><p>表示连接缓存服务器的参数
<p>不同类型其参数是不同的,以下是对应的格式说明:
<ul>
<li>memory,此值为 <a href="/time#Duration">time.Duration</a> 格式的参数,用于表示执行回收的间隔
<li>memcached则为服务器列表,多个服务器,以分号作为分隔;
<li>redis符合 <a href="https://www.iana.org/assignments/uri-schemes/prov/redis">Redis URI scheme</a> 的字符串;
<li>memory: 不需要参数
<li>memcached: 则为服务器列表,多个服务器,以分号作为分隔;
<li>redis: 符合 <a href="https://www.iana.org/assignments/uri-schemes/prov/redis">Redis URI scheme</a> 的字符串;
</ul>
</td></tr>
</tbody>
Expand Down
29 changes: 5 additions & 24 deletions server/config/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,14 @@
package config

import (
"time"

"github.com/issue9/cache"
"github.com/issue9/scheduled"

"github.com/issue9/web"
"github.com/issue9/web/locales"
"github.com/issue9/web/server"
)

// CacheBuilder 构建缓存客户端的方法
//
// drv 为缓存客户端对象;
// 如果是服务端客户端一体的,可通过 job 来指定服务端的定时回收任务;
type CacheBuilder = func(dsn string) (drv cache.Driver, job *Job, err error)

type Job struct {
Ticker time.Duration
Job scheduled.JobFunc
}
type CacheBuilder = func(dsn string) (cache.Driver, error)

type cacheConfig struct {
// 表示缓存的方式
Expand All @@ -38,9 +26,9 @@ type cacheConfig struct {
// 表示连接缓存服务器的参数
//
// 不同类型其参数是不同的,以下是对应的格式说明:
// - memory,此值为 [time.Duration] 格式的参数,用于表示执行回收的间隔
// - memcached则为服务器列表,多个服务器,以分号作为分隔;
// - redis符合 [Redis URI scheme] 的字符串;
// - memory: 不需要参数
// - memcached: 则为服务器列表,多个服务器,以分号作为分隔;
// - redis: 符合 [Redis URI scheme] 的字符串;
//
// [Redis URI scheme]: https://www.iana.org/assignments/uri-schemes/prov/redis
DSN string `yaml:"dsn" json:"dsn" xml:"dsn"`
Expand All @@ -56,18 +44,11 @@ func (conf *configOf[T]) buildCache() *web.FieldError {
return web.NewFieldError("type", locales.InvalidValue)
}

drv, job, err := b(conf.Cache.DSN)
drv, err := b(conf.Cache.DSN)
if err != nil {
return web.NewFieldError("dsn", err)
}
conf.cache = drv
if job != nil {
conf.init = append(conf.init, func(o *server.Options) {
o.Plugins = append(o.Plugins, web.PluginFunc(func(s web.Server) {
s.Services().AddTicker(locales.RecycleLocalCache, job.Job, job.Ticker, false, false)
}))
})
}

return nil
}
4 changes: 2 additions & 2 deletions server/config/micro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func TestConfigOf_buildMicro(t *testing.T) {
a := assert.New(t, false)
c, _ := memory.New()
c := memory.New()

conf := &configOf[empty]{}
a.NotError(conf.buildMicro(c))
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestConfigOf_buildMicro(t *testing.T) {

func TestRegistryConfig_build(t *testing.T) {
a := assert.New(t, false)
c, _ := memory.New()
c := memory.New()

conf := &registryConfig{
Type: "cache",
Expand Down
26 changes: 6 additions & 20 deletions server/config/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,30 +147,16 @@ func init() {

// RegisterCache

RegisterCache(func(dsn string) (cache.Driver, *Job, error) {
d, err := time.ParseDuration(dsn)
if err != nil {
return nil, nil, err
}

drv, job := memory.New()
j := func(now time.Time) error {
job(now)
return nil
}
return drv, &Job{Ticker: d, Job: j}, nil
RegisterCache(func(dsn string) (cache.Driver, error) {
return memory.New(), nil
}, "memory")

RegisterCache(func(dsn string) (cache.Driver, *Job, error) {
return memcache.New(strings.Split(dsn, ";")...), nil, nil
RegisterCache(func(dsn string) (cache.Driver, error) {
return memcache.New(strings.Split(dsn, ";")...), nil
}, "memcached", "memcache")

RegisterCache(func(dsn string) (cache.Driver, *Job, error) {
drv, err := redis.NewFromURL(dsn)
if err != nil {
return nil, nil, err
}
return drv, nil, nil
RegisterCache(func(dsn string) (cache.Driver, error) {
return redis.NewFromURL(dsn)
}, "redis")

// RegisterCompression
Expand Down
9 changes: 1 addition & 8 deletions server/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,7 @@ func sanitizeOptions(o *Options, t int) (*Options, *web.FieldError) {
}

if o.Cache == nil {
c, job := memory.New()
o.Cache = c
o.Plugins = append(o.Plugins, web.PluginFunc(func(s web.Server) {
s.Services().AddTicker(locales.RecycleLocalCache, func(now time.Time) error {
job(now)
return nil
}, time.Minute, false, false)
}))
o.Cache = memory.New()
}

if o.Language == language.Und {
Expand Down
4 changes: 2 additions & 2 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func TestNewService(t *testing.T) {
srv, err := NewService("app", "0.1.0", newOptions(nil))
a.Error(err).Nil(srv)

c, _ := memory.New()
c := memory.New()
reg := registry.NewCache(web.NewCache("reg:", c), registry.NewRandomStrategy(), time.Second)

srv = newService(a, "app", ":8080", reg, c)
Expand Down Expand Up @@ -365,7 +365,7 @@ func newService(a *assert.Assertion, name, addr string, reg registry.Registry, c

func TestNewGateway(t *testing.T) {
a := assert.New(t, false)
c, _ := memory.New() // 默认的缓存系统用的是内存类型的,保证引用同一个。
c := memory.New() // 默认的缓存系统用的是内存类型的,保证引用同一个。

reg := registry.NewCache(web.NewCache("reg:", c), registry.NewRandomStrategy(), time.Second)

Expand Down
3 changes: 1 addition & 2 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ func newTestServer(a *assert.Assertion) *testServer {

srv := &testServer{logBuf: logBuf}

cc, gc := memory.New()
cc := memory.New()
u := unique.NewNumber(100)
srv.InternalServer = InternalNewServer(srv, "test", "1.0.0", time.Local, log, u.String, l, cc, newCodec(a), header.XRequestID, "", nil)
srv.Services().Add(Phrase("unique"), u)
srv.Services().AddTicker(Phrase("gc memory"), func(t time.Time) error { gc(t); return nil }, time.Minute, false, false)

srv.Problems().Add(411, &LocaleProblem{ID: "41110", Title: Phrase("41110 title"), Detail: Phrase("41110 detail")})

Expand Down
4 changes: 2 additions & 2 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ func TestServer_scheduled(t *testing.T) {
a := assert.New(t, false)
s := newTestServer(a)
srv := s.Services()
a.Equal(1, len(srv.scheduled.Jobs())) // memory cache gc
a.Equal(0, len(srv.scheduled.Jobs()))

srv.AddAt(Phrase("lang"), func(t time.Time) error {
println("at:", t.Format(time.RFC3339))
return nil
}, time.Now(), false)
a.Equal(2, len(srv.scheduled.Jobs()))
a.Equal(1, len(srv.scheduled.Jobs()))

// 查找翻译项是否正确
var found bool
Expand Down
2 changes: 1 addition & 1 deletion web.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

// Version 当前框架的版本
const Version = "0.93.2"
const Version = "0.94.0"

type (
Logger = logs.Logger
Expand Down

0 comments on commit 6131aa7

Please sign in to comment.