export cache package, add client interceptor customization

master
kevin 4 years ago
parent dbca20e3df
commit d1b303fe7e

@ -1,4 +1,4 @@
package internal
package cache
import (
"fmt"

@ -1,4 +1,4 @@
package internal
package cache
import (
"encoding/json"

@ -1,5 +1,3 @@
package cache
import "github.com/tal-tech/go-zero/core/stores/internal"
type CacheConf = internal.ClusterConf
type CacheConf = ClusterConf

@ -1,4 +1,4 @@
package internal
package cache
import (
"encoding/json"

@ -1,4 +1,4 @@
package internal
package cache
import (
"errors"

@ -1,21 +1,45 @@
package cache
import (
"time"
import "time"
"github.com/tal-tech/go-zero/core/stores/internal"
const (
defaultExpiry = time.Hour * 24 * 7
defaultNotFoundExpiry = time.Minute
)
type Option = internal.Option
type (
Options struct {
Expiry time.Duration
NotFoundExpiry time.Duration
}
Option func(o *Options)
)
func newOptions(opts ...Option) Options {
var o Options
for _, opt := range opts {
opt(&o)
}
if o.Expiry <= 0 {
o.Expiry = defaultExpiry
}
if o.NotFoundExpiry <= 0 {
o.NotFoundExpiry = defaultNotFoundExpiry
}
return o
}
func WithExpiry(expiry time.Duration) Option {
return func(o *internal.Options) {
return func(o *Options) {
o.Expiry = expiry
}
}
func WithNotFoundExpiry(expiry time.Duration) Option {
return func(o *internal.Options) {
return func(o *Options) {
o.NotFoundExpiry = expiry
}
}

@ -1,4 +1,4 @@
package internal
package cache
import (
"sync/atomic"

@ -1,4 +1,4 @@
package internal
package cache
import (
"fmt"

@ -1,4 +1,4 @@
package internal
package cache
import "github.com/tal-tech/go-zero/core/stores/redis"

@ -1,4 +1,4 @@
package internal
package cache
import "strings"

@ -1,33 +0,0 @@
package internal
import "time"
const (
defaultExpiry = time.Hour * 24 * 7
defaultNotFoundExpiry = time.Minute
)
type (
Options struct {
Expiry time.Duration
NotFoundExpiry time.Duration
}
Option func(o *Options)
)
func newOptions(opts ...Option) Options {
var o Options
for _, opt := range opts {
opt(&o)
}
if o.Expiry <= 0 {
o.Expiry = defaultExpiry
}
if o.NotFoundExpiry <= 0 {
o.NotFoundExpiry = defaultNotFoundExpiry
}
return o
}

@ -1,5 +1,7 @@
package kv
import "github.com/tal-tech/go-zero/core/stores/internal"
import (
"github.com/tal-tech/go-zero/core/stores/cache"
)
type KvConf = internal.ClusterConf
type KvConf = cache.ClusterConf

@ -6,7 +6,7 @@ import (
"github.com/tal-tech/go-zero/core/errorx"
"github.com/tal-tech/go-zero/core/hash"
"github.com/tal-tech/go-zero/core/stores/internal"
"github.com/tal-tech/go-zero/core/stores/cache"
"github.com/tal-tech/go-zero/core/stores/redis"
)
@ -81,7 +81,7 @@ type (
)
func NewStore(c KvConf) Store {
if len(c) == 0 || internal.TotalWeights(c) <= 0 {
if len(c) == 0 || cache.TotalWeights(c) <= 0 {
log.Fatal("no cache nodes")
}

@ -6,7 +6,7 @@ import (
"github.com/alicebob/miniredis"
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/core/stores/internal"
"github.com/tal-tech/go-zero/core/stores/cache"
"github.com/tal-tech/go-zero/core/stores/redis"
"github.com/tal-tech/go-zero/core/stringx"
)
@ -478,7 +478,7 @@ func runOnCluster(t *testing.T, fn func(cluster Store)) {
s1.FlushAll()
s2.FlushAll()
store := NewStore([]internal.NodeConf{
store := NewStore([]cache.NodeConf{
{
RedisConf: redis.RedisConf{
Host: s1.Addr(),

@ -2,7 +2,7 @@ package mongoc
import (
"github.com/globalsign/mgo"
"github.com/tal-tech/go-zero/core/stores/internal"
"github.com/tal-tech/go-zero/core/stores/cache"
"github.com/tal-tech/go-zero/core/stores/mongo"
"github.com/tal-tech/go-zero/core/syncx"
)
@ -12,7 +12,7 @@ var (
// can't use one SharedCalls per conn, because multiple conns may share the same cache key.
sharedCalls = syncx.NewSharedCalls()
stats = internal.NewCacheStat("mongoc")
stats = cache.NewCacheStat("mongoc")
)
type (
@ -20,11 +20,11 @@ type (
cachedCollection struct {
collection mongo.Collection
cache internal.Cache
cache cache.Cache
}
)
func newCollection(collection mongo.Collection, c internal.Cache) *cachedCollection {
func newCollection(collection mongo.Collection, c cache.Cache) *cachedCollection {
return &cachedCollection{
collection: collection,
cache: c,

@ -16,7 +16,7 @@ import (
"github.com/globalsign/mgo/bson"
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/core/stat"
"github.com/tal-tech/go-zero/core/stores/internal"
"github.com/tal-tech/go-zero/core/stores/cache"
"github.com/tal-tech/go-zero/core/stores/mongo"
"github.com/tal-tech/go-zero/core/stores/redis"
)
@ -33,7 +33,7 @@ func TestStat(t *testing.T) {
}
r := redis.NewRedis(s.Addr(), redis.NodeType)
cach := internal.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound)
cach := cache.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound)
c := newCollection(dummyConn{}, cach)
for i := 0; i < 10; i++ {
@ -56,7 +56,7 @@ func TestStatCacheFails(t *testing.T) {
defer log.SetOutput(os.Stdout)
r := redis.NewRedis("localhost:59999", redis.NodeType)
cach := internal.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound)
cach := cache.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound)
c := newCollection(dummyConn{}, cach)
for i := 0; i < 20; i++ {
@ -79,7 +79,7 @@ func TestStatDbFails(t *testing.T) {
}
r := redis.NewRedis(s.Addr(), redis.NodeType)
cach := internal.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound)
cach := cache.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound)
c := newCollection(dummyConn{}, cach)
for i := 0; i < 20; i++ {
@ -103,7 +103,7 @@ func TestStatFromMemory(t *testing.T) {
}
r := redis.NewRedis(s.Addr(), redis.NodeType)
cach := internal.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound)
cach := cache.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound)
c := newCollection(dummyConn{}, cach)
var all sync.WaitGroup

@ -5,14 +5,13 @@ import (
"github.com/globalsign/mgo"
"github.com/tal-tech/go-zero/core/stores/cache"
"github.com/tal-tech/go-zero/core/stores/internal"
"github.com/tal-tech/go-zero/core/stores/mongo"
"github.com/tal-tech/go-zero/core/stores/redis"
)
type Model struct {
*mongo.Model
cache internal.Cache
cache cache.Cache
generateCollection func(*mgo.Session) *cachedCollection
}
@ -35,14 +34,14 @@ func MustNewModel(url, collection string, c cache.CacheConf, opts ...cache.Optio
}
func NewNodeModel(url, collection string, rds *redis.Redis, opts ...cache.Option) (*Model, error) {
c := internal.NewCacheNode(rds, sharedCalls, stats, mgo.ErrNotFound, opts...)
c := cache.NewCacheNode(rds, sharedCalls, stats, mgo.ErrNotFound, opts...)
return createModel(url, collection, c, func(collection mongo.Collection) *cachedCollection {
return newCollection(collection, c)
})
}
func NewModel(url, collection string, conf cache.CacheConf, opts ...cache.Option) (*Model, error) {
c := internal.NewCache(conf, sharedCalls, stats, mgo.ErrNotFound, opts...)
c := cache.NewCache(conf, sharedCalls, stats, mgo.ErrNotFound, opts...)
return createModel(url, collection, c, func(collection mongo.Collection) *cachedCollection {
return newCollection(collection, c)
})
@ -224,7 +223,7 @@ func (mm *Model) pipe(fn func(c *cachedCollection) mongo.Pipe) (mongo.Pipe, erro
return fn(mm.GetCollection(session)), nil
}
func createModel(url, collection string, c internal.Cache,
func createModel(url, collection string, c cache.Cache,
create func(mongo.Collection) *cachedCollection) (*Model, error) {
model, err := mongo.NewModel(url, collection)
if err != nil {

@ -5,7 +5,6 @@ import (
"time"
"github.com/tal-tech/go-zero/core/stores/cache"
"github.com/tal-tech/go-zero/core/stores/internal"
"github.com/tal-tech/go-zero/core/stores/redis"
"github.com/tal-tech/go-zero/core/stores/sqlx"
"github.com/tal-tech/go-zero/core/syncx"
@ -19,7 +18,7 @@ var (
// can't use one SharedCalls per conn, because multiple conns may share the same cache key.
exclusiveCalls = syncx.NewSharedCalls()
stats = internal.NewCacheStat("sqlc")
stats = cache.NewCacheStat("sqlc")
)
type (
@ -30,21 +29,21 @@ type (
CachedConn struct {
db sqlx.SqlConn
cache internal.Cache
cache cache.Cache
}
)
func NewNodeConn(db sqlx.SqlConn, rds *redis.Redis, opts ...cache.Option) CachedConn {
return CachedConn{
db: db,
cache: internal.NewCacheNode(rds, exclusiveCalls, stats, sql.ErrNoRows, opts...),
cache: cache.NewCacheNode(rds, exclusiveCalls, stats, sql.ErrNoRows, opts...),
}
}
func NewConn(db sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) CachedConn {
return CachedConn{
db: db,
cache: internal.NewCache(c, exclusiveCalls, stats, sql.ErrNoRows, opts...),
cache: cache.NewCache(c, exclusiveCalls, stats, sql.ErrNoRows, opts...),
}
}

@ -16,6 +16,8 @@ var (
)
type (
ClientOption = internal.ClientOption
Client interface {
AddInterceptor(interceptor grpc.UnaryClientInterceptor)
Conn() *grpc.ClientConn
@ -26,7 +28,7 @@ type (
}
)
func MustNewClient(c RpcClientConf, options ...internal.ClientOption) Client {
func MustNewClient(c RpcClientConf, options ...ClientOption) Client {
cli, err := NewClient(c, options...)
if err != nil {
log.Fatal(err)
@ -35,8 +37,8 @@ func MustNewClient(c RpcClientConf, options ...internal.ClientOption) Client {
return cli
}
func NewClient(c RpcClientConf, options ...internal.ClientOption) (Client, error) {
var opts []internal.ClientOption
func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
var opts []ClientOption
if c.HasCredential() {
opts = append(opts, WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
App: c.App,
@ -75,7 +77,7 @@ func NewClientNoAuth(c discov.EtcdConf) (Client, error) {
}, nil
}
func NewClientWithTarget(target string, opts ...internal.ClientOption) (Client, error) {
func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
return internal.NewClient(target, opts...)
}

Loading…
Cancel
Save