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