mirror of
https://github.com/go-micro/go-micro.git
synced 2025-05-31 21:59:42 +02:00
rename redis
This commit is contained in:
parent
60482ad364
commit
0944f9917e
@ -1,16 +1,16 @@
|
||||
package rediscache
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
rclient "github.com/go-redis/redis/v8"
|
||||
"go-micro.dev/v5/cache"
|
||||
)
|
||||
|
||||
type redisOptionsContextKey struct{}
|
||||
|
||||
// WithRedisOptions sets advanced options for redis.
|
||||
func WithRedisOptions(options redis.UniversalOptions) cache.Option {
|
||||
func WithRedisOptions(options rclient.UniversalOptions) cache.Option {
|
||||
return func(o *cache.Options) {
|
||||
if o.Context == nil {
|
||||
o.Context = context.Background()
|
||||
@ -20,29 +20,29 @@ func WithRedisOptions(options redis.UniversalOptions) cache.Option {
|
||||
}
|
||||
}
|
||||
|
||||
func newUniversalClient(options cache.Options) redis.UniversalClient {
|
||||
func newUniversalClient(options cache.Options) rclient.UniversalClient {
|
||||
if options.Context == nil {
|
||||
options.Context = context.Background()
|
||||
}
|
||||
|
||||
opts, ok := options.Context.Value(redisOptionsContextKey{}).(redis.UniversalOptions)
|
||||
opts, ok := options.Context.Value(redisOptionsContextKey{}).(rclient.UniversalOptions)
|
||||
if !ok {
|
||||
addr := "redis://127.0.0.1:6379"
|
||||
if len(options.Address) > 0 {
|
||||
addr = options.Address
|
||||
}
|
||||
|
||||
redisOptions, err := redis.ParseURL(addr)
|
||||
redisOptions, err := rclient.ParseURL(addr)
|
||||
if err != nil {
|
||||
redisOptions = &redis.Options{Addr: addr}
|
||||
redisOptions = &rclient.Options{Addr: addr}
|
||||
}
|
||||
|
||||
return redis.NewClient(redisOptions)
|
||||
return rclient.NewClient(redisOptions)
|
||||
}
|
||||
|
||||
if len(opts.Addrs) == 0 && len(options.Address) > 0 {
|
||||
opts.Addrs = []string{options.Address}
|
||||
}
|
||||
|
||||
return redis.NewUniversalClient(&opts)
|
||||
return rclient.NewUniversalClient(&opts)
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
package rediscache
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
rclient "github.com/go-redis/redis/v8"
|
||||
"go-micro.dev/v5/cache"
|
||||
)
|
||||
|
||||
@ -60,7 +60,7 @@ func Test_newUniversalClient(t *testing.T) {
|
||||
options: cache.Options{
|
||||
Context: context.WithValue(
|
||||
context.TODO(), redisOptionsContextKey{},
|
||||
redis.UniversalOptions{MasterName: "master-name"}),
|
||||
rclient.UniversalOptions{MasterName: "master-name"}),
|
||||
}},
|
||||
want: wantValues{
|
||||
username: "",
|
||||
@ -71,7 +71,7 @@ func Test_newUniversalClient(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
univClient := newUniversalClient(tt.fields.options)
|
||||
client, ok := univClient.(*redis.Client)
|
||||
client, ok := univClient.(*rclient.Client)
|
||||
if !ok {
|
||||
t.Errorf("newUniversalClient() expect a *redis.Client")
|
||||
return
|
||||
@ -109,7 +109,7 @@ func Test_newUniversalClientCluster(t *testing.T) {
|
||||
Address: "127.0.0.1:6379", // <- ignored
|
||||
Context: context.WithValue(
|
||||
context.TODO(), redisOptionsContextKey{},
|
||||
redis.UniversalOptions{Addrs: []string{"127.0.0.1:6381", "127.0.0.1:6382"}}),
|
||||
rclient.UniversalOptions{Addrs: []string{"127.0.0.1:6381", "127.0.0.1:6382"}}),
|
||||
}},
|
||||
want: wantValues{
|
||||
username: "",
|
||||
@ -120,7 +120,7 @@ func Test_newUniversalClientCluster(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
univClient := newUniversalClient(tt.fields.options)
|
||||
client, ok := univClient.(*redis.ClusterClient)
|
||||
client, ok := univClient.(*rclient.ClusterClient)
|
||||
if !ok {
|
||||
t.Errorf("newUniversalClient() expect a *redis.ClusterClient")
|
||||
return
|
@ -1,10 +1,10 @@
|
||||
package rediscache
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
rclient "github.com/go-redis/redis/v8"
|
||||
"go-micro.dev/v5/cache"
|
||||
)
|
||||
|
||||
@ -19,12 +19,12 @@ func NewRedisCache(opts ...cache.Option) cache.Cache {
|
||||
|
||||
type redisCache struct {
|
||||
opts cache.Options
|
||||
client redis.UniversalClient
|
||||
client rclient.UniversalClient
|
||||
}
|
||||
|
||||
func (c *redisCache) Get(ctx context.Context, key string) (interface{}, time.Time, error) {
|
||||
val, err := c.client.Get(ctx, key).Bytes()
|
||||
if err != nil && err == redis.Nil {
|
||||
if err != nil && err == rclient.Nil {
|
||||
return nil, time.Time{}, cache.ErrKeyNotFound
|
||||
} else if err != nil {
|
||||
return nil, time.Time{}, err
|
@ -1,4 +1,4 @@
|
||||
package rediscache
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
@ -14,7 +14,7 @@ import (
|
||||
|
||||
"go-micro.dev/v5/broker"
|
||||
"go-micro.dev/v5/cache"
|
||||
rediscache "go-micro.dev/v5/cache/rediscache"
|
||||
"go-micro.dev/v5/cache/redis"
|
||||
"go-micro.dev/v5/client"
|
||||
"go-micro.dev/v5/config"
|
||||
"go-micro.dev/v5/debug/profile"
|
||||
@ -276,7 +276,7 @@ var (
|
||||
DefaultConfigs = map[string]func(...config.Option) (config.Config, error){}
|
||||
|
||||
DefaultCaches = map[string]func(...cache.Option) cache.Cache{
|
||||
"redis": rediscache.NewRedisCache,
|
||||
"redis": redis.NewRedisCache,
|
||||
}
|
||||
)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user