2016-05-02 04:06:08 +00:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2021-09-20 11:53:42 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2016-05-02 04:06:08 +00:00
|
|
|
"strconv"
|
2021-09-20 11:53:42 +00:00
|
|
|
"strings"
|
2017-01-19 09:08:12 +00:00
|
|
|
|
|
|
|
"github.com/appleboy/gorush/config"
|
2022-12-11 03:12:58 +00:00
|
|
|
|
|
|
|
"github.com/go-redis/redis/v9"
|
2016-05-02 11:42:21 +00:00
|
|
|
)
|
|
|
|
|
2016-07-31 13:10:59 +00:00
|
|
|
// New func implements the storage interface for gorush (https://github.com/appleboy/gorush)
|
2021-08-02 06:07:30 +00:00
|
|
|
func New(config *config.ConfYaml) *Storage {
|
2016-05-02 04:06:08 +00:00
|
|
|
return &Storage{
|
2021-09-20 11:53:42 +00:00
|
|
|
ctx: context.Background(),
|
2016-05-02 04:06:08 +00:00
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// Storage is interface structure
|
2016-05-02 04:06:08 +00:00
|
|
|
type Storage struct {
|
2021-09-20 11:53:42 +00:00
|
|
|
ctx context.Context
|
2021-08-02 06:07:30 +00:00
|
|
|
config *config.ConfYaml
|
2021-09-20 11:53:42 +00:00
|
|
|
client redis.Cmdable
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 09:03:37 +00:00
|
|
|
func (s *Storage) Add(key string, count int64) {
|
|
|
|
s.client.IncrBy(s.ctx, key, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) Set(key string, count int64) {
|
|
|
|
s.client.Set(s.ctx, key, count, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) Get(key string) int64 {
|
|
|
|
val, _ := s.client.Get(s.ctx, key).Result()
|
|
|
|
count, _ := strconv.ParseInt(val, 10, 64)
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// Init client storage.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) Init() error {
|
2021-09-20 11:53:42 +00:00
|
|
|
if s.config.Stat.Redis.Cluster {
|
|
|
|
s.client = redis.NewClusterClient(&redis.ClusterOptions{
|
|
|
|
Addrs: strings.Split(s.config.Stat.Redis.Addr, ","),
|
|
|
|
Password: s.config.Stat.Redis.Password,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
s.client = redis.NewClient(&redis.Options{
|
|
|
|
Addr: s.config.Stat.Redis.Addr,
|
|
|
|
Password: s.config.Stat.Redis.Password,
|
|
|
|
DB: s.config.Stat.Redis.DB,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.client.Ping(s.ctx).Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-04-23 07:39:24 +00:00
|
|
|
}
|
2016-05-02 04:06:08 +00:00
|
|
|
|
2020-04-23 07:39:24 +00:00
|
|
|
// Close the storage connection
|
|
|
|
func (s *Storage) Close() error {
|
2021-09-20 11:53:42 +00:00
|
|
|
switch v := s.client.(type) {
|
|
|
|
case *redis.Client:
|
|
|
|
return v.Close()
|
|
|
|
case *redis.ClusterClient:
|
|
|
|
return v.Close()
|
|
|
|
case nil:
|
2020-04-23 07:39:24 +00:00
|
|
|
return nil
|
2021-09-20 11:53:42 +00:00
|
|
|
default:
|
|
|
|
// this will not happen anyway, unless we mishandle it on `Init`
|
|
|
|
panic(fmt.Sprintf("invalid redis client: %v", reflect.TypeOf(v)))
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
}
|