Separate storage logic from actual business logic (#649)
This commit is contained in:
@@ -8,8 +8,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/appleboy/gorush/config"
|
||||
"github.com/appleboy/gorush/storage"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
@@ -21,11 +19,6 @@ func New(config *config.ConfYaml) *Storage {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Storage) getInt64(key string, count *int64) {
|
||||
val, _ := s.client.Get(s.ctx, key).Result()
|
||||
*count, _ = strconv.ParseInt(val, 10, 64)
|
||||
}
|
||||
|
||||
// Storage is interface structure
|
||||
type Storage struct {
|
||||
ctx context.Context
|
||||
@@ -33,6 +26,20 @@ type Storage struct {
|
||||
client redis.Cmdable
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Init client storage.
|
||||
func (s *Storage) Init() error {
|
||||
if s.config.Stat.Redis.Cluster {
|
||||
@@ -69,105 +76,3 @@ func (s *Storage) Close() error {
|
||||
panic(fmt.Sprintf("invalid redis client: %v", reflect.TypeOf(v)))
|
||||
}
|
||||
}
|
||||
|
||||
// Reset Client storage.
|
||||
func (s *Storage) Reset() {
|
||||
s.client.Set(s.ctx, storage.TotalCountKey, int64(0), 0)
|
||||
s.client.Set(s.ctx, storage.IosSuccessKey, int64(0), 0)
|
||||
s.client.Set(s.ctx, storage.IosErrorKey, int64(0), 0)
|
||||
s.client.Set(s.ctx, storage.AndroidSuccessKey, int64(0), 0)
|
||||
s.client.Set(s.ctx, storage.AndroidErrorKey, int64(0), 0)
|
||||
s.client.Set(s.ctx, storage.HuaweiSuccessKey, int64(0), 0)
|
||||
s.client.Set(s.ctx, storage.HuaweiErrorKey, int64(0), 0)
|
||||
}
|
||||
|
||||
// AddTotalCount record push notification count.
|
||||
func (s *Storage) AddTotalCount(count int64) {
|
||||
s.client.IncrBy(s.ctx, storage.TotalCountKey, count)
|
||||
}
|
||||
|
||||
// AddIosSuccess record counts of success iOS push notification.
|
||||
func (s *Storage) AddIosSuccess(count int64) {
|
||||
s.client.IncrBy(s.ctx, storage.IosSuccessKey, count)
|
||||
}
|
||||
|
||||
// AddIosError record counts of error iOS push notification.
|
||||
func (s *Storage) AddIosError(count int64) {
|
||||
s.client.IncrBy(s.ctx, storage.IosErrorKey, count)
|
||||
}
|
||||
|
||||
// AddAndroidSuccess record counts of success Android push notification.
|
||||
func (s *Storage) AddAndroidSuccess(count int64) {
|
||||
s.client.IncrBy(s.ctx, storage.AndroidSuccessKey, count)
|
||||
}
|
||||
|
||||
// AddAndroidError record counts of error Android push notification.
|
||||
func (s *Storage) AddAndroidError(count int64) {
|
||||
s.client.IncrBy(s.ctx, storage.AndroidErrorKey, count)
|
||||
}
|
||||
|
||||
// AddHuaweiSuccess record counts of success Android push notification.
|
||||
func (s *Storage) AddHuaweiSuccess(count int64) {
|
||||
s.client.IncrBy(s.ctx, storage.HuaweiSuccessKey, count)
|
||||
}
|
||||
|
||||
// AddHuaweiError record counts of error Android push notification.
|
||||
func (s *Storage) AddHuaweiError(count int64) {
|
||||
s.client.IncrBy(s.ctx, storage.HuaweiErrorKey, count)
|
||||
}
|
||||
|
||||
// GetTotalCount show counts of all notification.
|
||||
func (s *Storage) GetTotalCount() int64 {
|
||||
var count int64
|
||||
s.getInt64(storage.TotalCountKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetIosSuccess show success counts of iOS notification.
|
||||
func (s *Storage) GetIosSuccess() int64 {
|
||||
var count int64
|
||||
s.getInt64(storage.IosSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetIosError show error counts of iOS notification.
|
||||
func (s *Storage) GetIosError() int64 {
|
||||
var count int64
|
||||
s.getInt64(storage.IosErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetAndroidSuccess show success counts of Android notification.
|
||||
func (s *Storage) GetAndroidSuccess() int64 {
|
||||
var count int64
|
||||
s.getInt64(storage.AndroidSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetAndroidError show error counts of Android notification.
|
||||
func (s *Storage) GetAndroidError() int64 {
|
||||
var count int64
|
||||
s.getInt64(storage.AndroidErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/appleboy/gorush/storage"
|
||||
|
||||
"github.com/appleboy/gorush/config"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -27,34 +29,16 @@ func TestRedisEngine(t *testing.T) {
|
||||
redis := New(cfg)
|
||||
err := redis.Init()
|
||||
assert.Nil(t, err)
|
||||
redis.Reset()
|
||||
|
||||
redis.AddTotalCount(10)
|
||||
val = redis.GetTotalCount()
|
||||
redis.Add(storage.HuaweiSuccessKey, 10)
|
||||
val = redis.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(10), val)
|
||||
redis.AddTotalCount(10)
|
||||
val = redis.GetTotalCount()
|
||||
redis.Add(storage.HuaweiSuccessKey, 10)
|
||||
val = redis.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(20), val)
|
||||
|
||||
redis.AddIosSuccess(20)
|
||||
val = redis.GetIosSuccess()
|
||||
assert.Equal(t, int64(20), val)
|
||||
|
||||
redis.AddIosError(30)
|
||||
val = redis.GetIosError()
|
||||
assert.Equal(t, int64(30), val)
|
||||
|
||||
redis.AddAndroidSuccess(40)
|
||||
val = redis.GetAndroidSuccess()
|
||||
assert.Equal(t, int64(40), val)
|
||||
|
||||
redis.AddAndroidError(50)
|
||||
val = redis.GetAndroidError()
|
||||
assert.Equal(t, int64(50), val)
|
||||
|
||||
// test reset db
|
||||
redis.Reset()
|
||||
val = redis.GetAndroidError()
|
||||
redis.Set(storage.HuaweiSuccessKey, 0)
|
||||
val = redis.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(0), val)
|
||||
|
||||
// test concurrency issues
|
||||
@@ -62,12 +46,12 @@ func TestRedisEngine(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
redis.AddTotalCount(1)
|
||||
redis.Add(storage.HuaweiSuccessKey, 1)
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
val = redis.GetTotalCount()
|
||||
val = redis.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(10), val)
|
||||
|
||||
assert.NoError(t, redis.Close())
|
||||
|
||||
Reference in New Issue
Block a user