refactor(storage): move interface to core package

This commit is contained in:
Bo-Yi Wu 2022-11-09 14:05:09 +08:00
parent 111a7cce49
commit 4be2c8036d
10 changed files with 120 additions and 117 deletions

33
core/storage.go Normal file
View File

@ -0,0 +1,33 @@
package core
const (
// TotalCountKey is key name for total count of storage
TotalCountKey = "gorush-total-count"
// IosSuccessKey is key name or ios success count of storage
IosSuccessKey = "gorush-ios-success-count"
// IosErrorKey is key name or ios success error of storage
IosErrorKey = "gorush-ios-error-count"
// AndroidSuccessKey is key name for android success count of storage
AndroidSuccessKey = "gorush-android-success-count"
// AndroidErrorKey is key name for android error count of storage
AndroidErrorKey = "gorush-android-error-count"
// HuaweiSuccessKey is key name for huawei success count of storage
HuaweiSuccessKey = "gorush-huawei-success-count"
// HuaweiErrorKey is key name for huawei error count of storage
HuaweiErrorKey = "gorush-huawei-error-count"
)
// Storage interface
type Storage interface {
Init() error
Add(key string, count int64)
Set(key string, count int64)
Get(key string) int64
Close() error
}

View File

@ -4,8 +4,8 @@ import (
"errors"
"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"
"github.com/appleboy/gorush/logx"
"github.com/appleboy/gorush/storage"
"github.com/appleboy/gorush/storage/badger"
"github.com/appleboy/gorush/storage/boltdb"
"github.com/appleboy/gorush/storage/buntdb"
@ -57,7 +57,7 @@ type HuaweiStatus struct {
func InitAppStatus(conf *config.ConfYaml) error {
logx.LogAccess.Info("Init App Status Engine as ", conf.Stat.Engine)
var store storage.Storage
var store core.Storage
switch conf.Stat.Engine {
case "memory":
store = memory.New()

View File

@ -1,12 +1,14 @@
package status
import "github.com/appleboy/gorush/storage"
import (
"github.com/appleboy/gorush/core"
)
type StateStorage struct {
store storage.Storage
store core.Storage
}
func NewStateStorage(store storage.Storage) *StateStorage {
func NewStateStorage(store core.Storage) *StateStorage {
return &StateStorage{
store: store,
}
@ -22,81 +24,81 @@ func (s *StateStorage) Close() error {
// Reset Client storage.
func (s *StateStorage) Reset() {
s.store.Set(storage.TotalCountKey, 0)
s.store.Set(storage.IosSuccessKey, 0)
s.store.Set(storage.IosErrorKey, 0)
s.store.Set(storage.AndroidSuccessKey, 0)
s.store.Set(storage.AndroidErrorKey, 0)
s.store.Set(storage.HuaweiSuccessKey, 0)
s.store.Set(storage.HuaweiErrorKey, 0)
s.store.Set(core.TotalCountKey, 0)
s.store.Set(core.IosSuccessKey, 0)
s.store.Set(core.IosErrorKey, 0)
s.store.Set(core.AndroidSuccessKey, 0)
s.store.Set(core.AndroidErrorKey, 0)
s.store.Set(core.HuaweiSuccessKey, 0)
s.store.Set(core.HuaweiErrorKey, 0)
}
// AddTotalCount record push notification count.
func (s *StateStorage) AddTotalCount(count int64) {
s.store.Add(storage.TotalCountKey, count)
s.store.Add(core.TotalCountKey, count)
}
// AddIosSuccess record counts of success iOS push notification.
func (s *StateStorage) AddIosSuccess(count int64) {
s.store.Add(storage.IosSuccessKey, count)
s.store.Add(core.IosSuccessKey, count)
}
// AddIosError record counts of error iOS push notification.
func (s *StateStorage) AddIosError(count int64) {
s.store.Add(storage.IosErrorKey, count)
s.store.Add(core.IosErrorKey, count)
}
// AddAndroidSuccess record counts of success Android push notification.
func (s *StateStorage) AddAndroidSuccess(count int64) {
s.store.Add(storage.AndroidSuccessKey, count)
s.store.Add(core.AndroidSuccessKey, count)
}
// AddAndroidError record counts of error Android push notification.
func (s *StateStorage) AddAndroidError(count int64) {
s.store.Add(storage.AndroidErrorKey, count)
s.store.Add(core.AndroidErrorKey, count)
}
// AddHuaweiSuccess record counts of success Huawei push notification.
func (s *StateStorage) AddHuaweiSuccess(count int64) {
s.store.Add(storage.HuaweiSuccessKey, count)
s.store.Add(core.HuaweiSuccessKey, count)
}
// AddHuaweiError record counts of error Huawei push notification.
func (s *StateStorage) AddHuaweiError(count int64) {
s.store.Add(storage.HuaweiErrorKey, count)
s.store.Add(core.HuaweiErrorKey, count)
}
// GetTotalCount show counts of all notification.
func (s *StateStorage) GetTotalCount() int64 {
return s.store.Get(storage.TotalCountKey)
return s.store.Get(core.TotalCountKey)
}
// GetIosSuccess show success counts of iOS notification.
func (s *StateStorage) GetIosSuccess() int64 {
return s.store.Get(storage.IosSuccessKey)
return s.store.Get(core.IosSuccessKey)
}
// GetIosError show error counts of iOS notification.
func (s *StateStorage) GetIosError() int64 {
return s.store.Get(storage.IosErrorKey)
return s.store.Get(core.IosErrorKey)
}
// GetAndroidSuccess show success counts of Android notification.
func (s *StateStorage) GetAndroidSuccess() int64 {
return s.store.Get(storage.AndroidSuccessKey)
return s.store.Get(core.AndroidSuccessKey)
}
// GetAndroidError show error counts of Android notification.
func (s *StateStorage) GetAndroidError() int64 {
return s.store.Get(storage.AndroidErrorKey)
return s.store.Get(core.AndroidErrorKey)
}
// GetHuaweiSuccess show success counts of Huawei notification.
func (s *StateStorage) GetHuaweiSuccess() int64 {
return s.store.Get(storage.HuaweiSuccessKey)
return s.store.Get(core.HuaweiSuccessKey)
}
// GetHuaweiError show error counts of Huawei notification.
func (s *StateStorage) GetHuaweiError() int64 {
return s.store.Get(storage.HuaweiErrorKey)
return s.store.Get(core.HuaweiErrorKey)
}

View File

@ -4,9 +4,9 @@ import (
"sync"
"testing"
"github.com/appleboy/gorush/storage"
"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"
"github.com/stretchr/testify/assert"
)
@ -19,15 +19,15 @@ func TestBadgerEngine(t *testing.T) {
err := badger.Init()
assert.Nil(t, err)
badger.Add(storage.HuaweiSuccessKey, 10)
val = badger.Get(storage.HuaweiSuccessKey)
badger.Add(core.HuaweiSuccessKey, 10)
val = badger.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
badger.Add(storage.HuaweiSuccessKey, 10)
val = badger.Get(storage.HuaweiSuccessKey)
badger.Add(core.HuaweiSuccessKey, 10)
val = badger.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(20), val)
badger.Set(storage.HuaweiSuccessKey, 0)
val = badger.Get(storage.HuaweiSuccessKey)
badger.Set(core.HuaweiSuccessKey, 0)
val = badger.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(0), val)
// test concurrency issues
@ -35,12 +35,12 @@ func TestBadgerEngine(t *testing.T) {
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
badger.Add(storage.HuaweiSuccessKey, 1)
badger.Add(core.HuaweiSuccessKey, 1)
wg.Done()
}()
}
wg.Wait()
val = badger.Get(storage.HuaweiSuccessKey)
val = badger.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
assert.NoError(t, badger.Close())

View File

@ -4,9 +4,9 @@ import (
"sync"
"testing"
"github.com/appleboy/gorush/storage"
"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"
"github.com/stretchr/testify/assert"
)
@ -19,15 +19,15 @@ func TestBoltDBEngine(t *testing.T) {
err := boltDB.Init()
assert.Nil(t, err)
boltDB.Add(storage.HuaweiSuccessKey, 10)
val = boltDB.Get(storage.HuaweiSuccessKey)
boltDB.Add(core.HuaweiSuccessKey, 10)
val = boltDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
boltDB.Add(storage.HuaweiSuccessKey, 10)
val = boltDB.Get(storage.HuaweiSuccessKey)
boltDB.Add(core.HuaweiSuccessKey, 10)
val = boltDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(20), val)
boltDB.Set(storage.HuaweiSuccessKey, 0)
val = boltDB.Get(storage.HuaweiSuccessKey)
boltDB.Set(core.HuaweiSuccessKey, 0)
val = boltDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(0), val)
// test concurrency issues
@ -35,12 +35,12 @@ func TestBoltDBEngine(t *testing.T) {
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
boltDB.Add(storage.HuaweiSuccessKey, 1)
boltDB.Add(core.HuaweiSuccessKey, 1)
wg.Done()
}()
}
wg.Wait()
val = boltDB.Get(storage.HuaweiSuccessKey)
val = boltDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
assert.NoError(t, boltDB.Close())

View File

@ -5,9 +5,9 @@ import (
"sync"
"testing"
"github.com/appleboy/gorush/storage"
"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"
"github.com/stretchr/testify/assert"
)
@ -25,15 +25,15 @@ func TestBuntDBEngine(t *testing.T) {
err := buntDB.Init()
assert.Nil(t, err)
buntDB.Add(storage.HuaweiSuccessKey, 10)
val = buntDB.Get(storage.HuaweiSuccessKey)
buntDB.Add(core.HuaweiSuccessKey, 10)
val = buntDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
buntDB.Add(storage.HuaweiSuccessKey, 10)
val = buntDB.Get(storage.HuaweiSuccessKey)
buntDB.Add(core.HuaweiSuccessKey, 10)
val = buntDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(20), val)
buntDB.Set(storage.HuaweiSuccessKey, 0)
val = buntDB.Get(storage.HuaweiSuccessKey)
buntDB.Set(core.HuaweiSuccessKey, 0)
val = buntDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(0), val)
// test concurrency issues
@ -41,12 +41,12 @@ func TestBuntDBEngine(t *testing.T) {
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
buntDB.Add(storage.HuaweiSuccessKey, 1)
buntDB.Add(core.HuaweiSuccessKey, 1)
wg.Done()
}()
}
wg.Wait()
val = buntDB.Get(storage.HuaweiSuccessKey)
val = buntDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
assert.NoError(t, buntDB.Close())

View File

@ -5,9 +5,9 @@ import (
"sync"
"testing"
"github.com/appleboy/gorush/storage"
"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"
"github.com/stretchr/testify/assert"
)
@ -25,15 +25,15 @@ func TestLevelDBEngine(t *testing.T) {
err := levelDB.Init()
assert.Nil(t, err)
levelDB.Add(storage.HuaweiSuccessKey, 10)
val = levelDB.Get(storage.HuaweiSuccessKey)
levelDB.Add(core.HuaweiSuccessKey, 10)
val = levelDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
levelDB.Add(storage.HuaweiSuccessKey, 10)
val = levelDB.Get(storage.HuaweiSuccessKey)
levelDB.Add(core.HuaweiSuccessKey, 10)
val = levelDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(20), val)
levelDB.Set(storage.HuaweiSuccessKey, 0)
val = levelDB.Get(storage.HuaweiSuccessKey)
levelDB.Set(core.HuaweiSuccessKey, 0)
val = levelDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(0), val)
// test concurrency issues
@ -41,12 +41,12 @@ func TestLevelDBEngine(t *testing.T) {
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
levelDB.Add(storage.HuaweiSuccessKey, 1)
levelDB.Add(core.HuaweiSuccessKey, 1)
wg.Done()
}()
}
wg.Wait()
val = levelDB.Get(storage.HuaweiSuccessKey)
val = levelDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
assert.NoError(t, levelDB.Close())

View File

@ -4,7 +4,7 @@ import (
"sync"
"testing"
"github.com/appleboy/gorush/storage"
"github.com/appleboy/gorush/core"
"github.com/stretchr/testify/assert"
)
@ -16,15 +16,15 @@ func TestMemoryEngine(t *testing.T) {
err := memory.Init()
assert.Nil(t, err)
memory.Add(storage.HuaweiSuccessKey, 10)
val = memory.Get(storage.HuaweiSuccessKey)
memory.Add(core.HuaweiSuccessKey, 10)
val = memory.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
memory.Add(storage.HuaweiSuccessKey, 10)
val = memory.Get(storage.HuaweiSuccessKey)
memory.Add(core.HuaweiSuccessKey, 10)
val = memory.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(20), val)
memory.Set(storage.HuaweiSuccessKey, 0)
val = memory.Get(storage.HuaweiSuccessKey)
memory.Set(core.HuaweiSuccessKey, 0)
val = memory.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(0), val)
// test concurrency issues
@ -32,12 +32,12 @@ func TestMemoryEngine(t *testing.T) {
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
memory.Add(storage.HuaweiSuccessKey, 1)
memory.Add(core.HuaweiSuccessKey, 1)
wg.Done()
}()
}
wg.Wait()
val = memory.Get(storage.HuaweiSuccessKey)
val = memory.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
assert.NoError(t, memory.Close())

View File

@ -4,9 +4,9 @@ import (
"sync"
"testing"
"github.com/appleboy/gorush/storage"
"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"
"github.com/stretchr/testify/assert"
)
@ -30,15 +30,15 @@ func TestRedisEngine(t *testing.T) {
err := redis.Init()
assert.Nil(t, err)
redis.Add(storage.HuaweiSuccessKey, 10)
val = redis.Get(storage.HuaweiSuccessKey)
redis.Add(core.HuaweiSuccessKey, 10)
val = redis.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
redis.Add(storage.HuaweiSuccessKey, 10)
val = redis.Get(storage.HuaweiSuccessKey)
redis.Add(core.HuaweiSuccessKey, 10)
val = redis.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(20), val)
redis.Set(storage.HuaweiSuccessKey, 0)
val = redis.Get(storage.HuaweiSuccessKey)
redis.Set(core.HuaweiSuccessKey, 0)
val = redis.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(0), val)
// test concurrency issues
@ -46,12 +46,12 @@ func TestRedisEngine(t *testing.T) {
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
redis.Add(storage.HuaweiSuccessKey, 1)
redis.Add(core.HuaweiSuccessKey, 1)
wg.Done()
}()
}
wg.Wait()
val = redis.Get(storage.HuaweiSuccessKey)
val = redis.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
assert.NoError(t, redis.Close())

View File

@ -1,33 +1 @@
package storage
const (
// TotalCountKey is key name for total count of storage
TotalCountKey = "gorush-total-count"
// IosSuccessKey is key name or ios success count of storage
IosSuccessKey = "gorush-ios-success-count"
// IosErrorKey is key name or ios success error of storage
IosErrorKey = "gorush-ios-error-count"
// AndroidSuccessKey is key name for android success count of storage
AndroidSuccessKey = "gorush-android-success-count"
// AndroidErrorKey is key name for android error count of storage
AndroidErrorKey = "gorush-android-error-count"
// HuaweiSuccessKey is key name for huawei success count of storage
HuaweiSuccessKey = "gorush-huawei-success-count"
// HuaweiErrorKey is key name for huawei error count of storage
HuaweiErrorKey = "gorush-huawei-error-count"
)
// Storage interface
type Storage interface {
Init() error
Add(key string, count int64)
Set(key string, count int64)
Get(key string) int64
Close() error
}