123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package cache
- import (
- "context"
- "eta_mini_ht_api/common/component/config"
- logger "eta_mini_ht_api/common/component/log"
- "github.com/go-redis/redis/v8"
- "log"
- "strings"
- "sync"
- "time"
- )
- var (
- redisCache *RedisCache
- once sync.Once
- )
- func GetInstance() *RedisCache {
- once.Do(func() {
- redisConf, ok := config.GetConfig("redis").(*config.RedisConfig)
-
- if !ok {
- logger.Info("加载redis配置失败")
- return
- }
- opts, ok := redisConf.GetConfig().(config.RedisOpts)
-
- if ok && opts.Host != "" {
- logger.Info("初始化redis")
-
- redisCache = newRedis(opts)
- } else {
- logger.Info("Redis configuration is incomplete: Host is missing")
- return
- }
- })
- return redisCache
- }
- func newRedis(opts config.RedisOpts) *RedisCache {
- if &opts == nil || opts.Host == "" {
- log.Fatalf("redis 连接失败")
- }
- redisTemplate := redis.NewUniversalClient(&redis.UniversalOptions{
- Addrs: strings.Split(opts.Host, ";"),
- DB: opts.Database,
- Password: opts.Password,
- IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
- MinIdleConns: opts.MaxIdle,
- })
- return &RedisCache{redisTemplate: redisTemplate}
- }
- type RedisCache struct {
- redisTemplate redis.UniversalClient
- }
- func (r *RedisCache) GetString(key string) string {
- return r.GetStringWithContext(context.Background(), key)
- }
- func (r *RedisCache) GetStringWithContext(ctx context.Context, key string) (value string) {
- value, _ = r.redisTemplate.Get(ctx, key).Result()
- return
- }
- func (r *RedisCache) SetString(key string, val string, timeout time.Duration) error {
- return r.redisTemplate.SetEX(context.Background(), key, val, timeout).Err()
- }
- func (r *RedisCache) SetStringWithContext(ctx context.Context, key string, val string, timeout time.Duration) error {
- return r.redisTemplate.SetEX(ctx, key, val, timeout).Err()
- }
- func (r *RedisCache) IsExist(key string) bool {
- return r.IsExistWithContext(context.Background(), key)
- }
- func (r *RedisCache) IsExistWithContext(ctx context.Context, key string) bool {
- result, _ := r.redisTemplate.Exists(ctx, key).Result()
- return result > 0
- }
- func (r *RedisCache) Delete(key string) error {
- return r.DeleteWithContext(context.Background(), key)
- }
- func (r *RedisCache) DeleteWithContext(ctx context.Context, key string) error {
- return r.redisTemplate.Del(ctx, key).Err()
- }
- func (r *RedisCache) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
- return r.DoWithContext(context.Background(), commandName, args...)
- }
- func (r *RedisCache) DoWithContext(ctx context.Context, commandName string, args ...interface{}) (reply interface{}, err error) {
- newArgs := []interface{}{commandName}
- newArgs = append(newArgs, args...)
- return r.redisTemplate.Do(ctx, newArgs...).Result()
- }
|