2016-05-02 04:06:08 +00:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
2017-01-19 09:08:12 +00:00
|
|
|
|
|
|
|
"github.com/appleboy/gorush/config"
|
2017-06-24 16:48:48 +00:00
|
|
|
"github.com/appleboy/gorush/storage"
|
2016-05-02 04:06:08 +00:00
|
|
|
|
2020-04-23 08:23:29 +00:00
|
|
|
"github.com/go-redis/redis/v7"
|
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{
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:39:24 +00:00
|
|
|
func (s *Storage) getInt64(key string, count *int64) {
|
|
|
|
val, _ := s.client.Get(key).Result()
|
2016-05-02 04:06:08 +00:00
|
|
|
*count, _ = strconv.ParseInt(val, 10, 64)
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// Storage is interface structure
|
2016-05-02 04:06:08 +00:00
|
|
|
type Storage struct {
|
2021-08-02 06:07:30 +00:00
|
|
|
config *config.ConfYaml
|
2020-04-23 07:39:24 +00:00
|
|
|
client *redis.Client
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// Init client storage.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) Init() error {
|
2020-04-23 07:39:24 +00:00
|
|
|
s.client = redis.NewClient(&redis.Options{
|
2016-05-02 04:06:08 +00:00
|
|
|
Addr: s.config.Stat.Redis.Addr,
|
|
|
|
Password: s.config.Stat.Redis.Password,
|
|
|
|
DB: s.config.Stat.Redis.DB,
|
|
|
|
})
|
2020-04-23 07:39:24 +00:00
|
|
|
_, err := s.client.Ping().Result()
|
2016-05-02 04:06:08 +00:00
|
|
|
|
2020-04-23 07:39:24 +00:00
|
|
|
return err
|
|
|
|
}
|
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 {
|
|
|
|
if s.client == nil {
|
|
|
|
return nil
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 07:39:24 +00:00
|
|
|
return s.client.Close()
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// Reset Client storage.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) Reset() {
|
2020-04-23 07:39:24 +00:00
|
|
|
s.client.Set(storage.TotalCountKey, int64(0), 0)
|
|
|
|
s.client.Set(storage.IosSuccessKey, int64(0), 0)
|
|
|
|
s.client.Set(storage.IosErrorKey, int64(0), 0)
|
|
|
|
s.client.Set(storage.AndroidSuccessKey, int64(0), 0)
|
|
|
|
s.client.Set(storage.AndroidErrorKey, int64(0), 0)
|
2020-09-04 03:01:21 +00:00
|
|
|
s.client.Set(storage.HuaweiSuccessKey, int64(0), 0)
|
|
|
|
s.client.Set(storage.HuaweiErrorKey, int64(0), 0)
|
2016-05-02 04:34:35 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// AddTotalCount record push notification count.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) AddTotalCount(count int64) {
|
2020-04-23 07:39:24 +00:00
|
|
|
s.client.IncrBy(storage.TotalCountKey, count)
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// AddIosSuccess record counts of success iOS push notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) AddIosSuccess(count int64) {
|
2020-04-23 07:39:24 +00:00
|
|
|
s.client.IncrBy(storage.IosSuccessKey, count)
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// AddIosError record counts of error iOS push notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) AddIosError(count int64) {
|
2020-04-23 07:39:24 +00:00
|
|
|
s.client.IncrBy(storage.IosErrorKey, count)
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// AddAndroidSuccess record counts of success Android push notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) AddAndroidSuccess(count int64) {
|
2020-04-23 07:39:24 +00:00
|
|
|
s.client.IncrBy(storage.AndroidSuccessKey, count)
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// AddAndroidError record counts of error Android push notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) AddAndroidError(count int64) {
|
2020-04-23 07:39:24 +00:00
|
|
|
s.client.IncrBy(storage.AndroidErrorKey, count)
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 03:01:21 +00:00
|
|
|
// AddHuaweiSuccess record counts of success Android push notification.
|
|
|
|
func (s *Storage) AddHuaweiSuccess(count int64) {
|
|
|
|
s.client.IncrBy(storage.HuaweiSuccessKey, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddHuaweiError record counts of error Android push notification.
|
|
|
|
func (s *Storage) AddHuaweiError(count int64) {
|
|
|
|
s.client.IncrBy(storage.HuaweiErrorKey, count)
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// GetTotalCount show counts of all notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) GetTotalCount() int64 {
|
2016-05-02 04:06:08 +00:00
|
|
|
var count int64
|
2020-04-23 07:39:24 +00:00
|
|
|
s.getInt64(storage.TotalCountKey, &count)
|
2016-05-02 04:06:08 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// GetIosSuccess show success counts of iOS notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) GetIosSuccess() int64 {
|
2016-05-02 04:06:08 +00:00
|
|
|
var count int64
|
2020-04-23 07:39:24 +00:00
|
|
|
s.getInt64(storage.IosSuccessKey, &count)
|
2016-05-02 04:06:08 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// GetIosError show error counts of iOS notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) GetIosError() int64 {
|
2016-05-02 04:06:08 +00:00
|
|
|
var count int64
|
2020-04-23 07:39:24 +00:00
|
|
|
s.getInt64(storage.IosErrorKey, &count)
|
2016-05-02 04:06:08 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// GetAndroidSuccess show success counts of Android notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) GetAndroidSuccess() int64 {
|
2016-05-02 04:06:08 +00:00
|
|
|
var count int64
|
2020-04-23 07:39:24 +00:00
|
|
|
s.getInt64(storage.AndroidSuccessKey, &count)
|
2016-05-02 04:06:08 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2016-08-01 02:39:33 +00:00
|
|
|
// GetAndroidError show error counts of Android notification.
|
2016-05-02 11:42:21 +00:00
|
|
|
func (s *Storage) GetAndroidError() int64 {
|
2016-05-02 04:06:08 +00:00
|
|
|
var count int64
|
2020-04-23 07:39:24 +00:00
|
|
|
s.getInt64(storage.AndroidErrorKey, &count)
|
2016-05-02 04:06:08 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
2020-09-04 03:01:21 +00:00
|
|
|
|
|
|
|
// GetHuaweiSuccess show success counts of Huawei notification.
|
|
|
|
func (s *Storage) GetHuaweiSuccess() int64 {
|
|
|
|
var count int64
|
|
|
|
s.getInt64(storage.HuaweiSuccessKey, &count)
|
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHuaweiError show error counts of Huawei notification.
|
|
|
|
func (s *Storage) GetHuaweiError() int64 {
|
|
|
|
var count int64
|
|
|
|
s.getInt64(storage.HuaweiErrorKey, &count)
|
|
|
|
|
|
|
|
return count
|
|
|
|
}
|