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

View File

@ -1,12 +1,14 @@
package status package status
import "github.com/appleboy/gorush/storage" import (
"github.com/appleboy/gorush/core"
)
type StateStorage struct { type StateStorage struct {
store storage.Storage store core.Storage
} }
func NewStateStorage(store storage.Storage) *StateStorage { func NewStateStorage(store core.Storage) *StateStorage {
return &StateStorage{ return &StateStorage{
store: store, store: store,
} }
@ -22,81 +24,81 @@ func (s *StateStorage) Close() error {
// Reset Client storage. // Reset Client storage.
func (s *StateStorage) Reset() { func (s *StateStorage) Reset() {
s.store.Set(storage.TotalCountKey, 0) s.store.Set(core.TotalCountKey, 0)
s.store.Set(storage.IosSuccessKey, 0) s.store.Set(core.IosSuccessKey, 0)
s.store.Set(storage.IosErrorKey, 0) s.store.Set(core.IosErrorKey, 0)
s.store.Set(storage.AndroidSuccessKey, 0) s.store.Set(core.AndroidSuccessKey, 0)
s.store.Set(storage.AndroidErrorKey, 0) s.store.Set(core.AndroidErrorKey, 0)
s.store.Set(storage.HuaweiSuccessKey, 0) s.store.Set(core.HuaweiSuccessKey, 0)
s.store.Set(storage.HuaweiErrorKey, 0) s.store.Set(core.HuaweiErrorKey, 0)
} }
// AddTotalCount record push notification count. // AddTotalCount record push notification count.
func (s *StateStorage) AddTotalCount(count int64) { 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. // AddIosSuccess record counts of success iOS push notification.
func (s *StateStorage) AddIosSuccess(count int64) { 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. // AddIosError record counts of error iOS push notification.
func (s *StateStorage) AddIosError(count int64) { 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. // AddAndroidSuccess record counts of success Android push notification.
func (s *StateStorage) AddAndroidSuccess(count int64) { 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. // AddAndroidError record counts of error Android push notification.
func (s *StateStorage) AddAndroidError(count int64) { 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. // AddHuaweiSuccess record counts of success Huawei push notification.
func (s *StateStorage) AddHuaweiSuccess(count int64) { 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. // AddHuaweiError record counts of error Huawei push notification.
func (s *StateStorage) AddHuaweiError(count int64) { func (s *StateStorage) AddHuaweiError(count int64) {
s.store.Add(storage.HuaweiErrorKey, count) s.store.Add(core.HuaweiErrorKey, count)
} }
// GetTotalCount show counts of all notification. // GetTotalCount show counts of all notification.
func (s *StateStorage) GetTotalCount() int64 { func (s *StateStorage) GetTotalCount() int64 {
return s.store.Get(storage.TotalCountKey) return s.store.Get(core.TotalCountKey)
} }
// GetIosSuccess show success counts of iOS notification. // GetIosSuccess show success counts of iOS notification.
func (s *StateStorage) GetIosSuccess() int64 { func (s *StateStorage) GetIosSuccess() int64 {
return s.store.Get(storage.IosSuccessKey) return s.store.Get(core.IosSuccessKey)
} }
// GetIosError show error counts of iOS notification. // GetIosError show error counts of iOS notification.
func (s *StateStorage) GetIosError() int64 { func (s *StateStorage) GetIosError() int64 {
return s.store.Get(storage.IosErrorKey) return s.store.Get(core.IosErrorKey)
} }
// GetAndroidSuccess show success counts of Android notification. // GetAndroidSuccess show success counts of Android notification.
func (s *StateStorage) GetAndroidSuccess() int64 { func (s *StateStorage) GetAndroidSuccess() int64 {
return s.store.Get(storage.AndroidSuccessKey) return s.store.Get(core.AndroidSuccessKey)
} }
// GetAndroidError show error counts of Android notification. // GetAndroidError show error counts of Android notification.
func (s *StateStorage) GetAndroidError() int64 { func (s *StateStorage) GetAndroidError() int64 {
return s.store.Get(storage.AndroidErrorKey) return s.store.Get(core.AndroidErrorKey)
} }
// GetHuaweiSuccess show success counts of Huawei notification. // GetHuaweiSuccess show success counts of Huawei notification.
func (s *StateStorage) GetHuaweiSuccess() int64 { func (s *StateStorage) GetHuaweiSuccess() int64 {
return s.store.Get(storage.HuaweiSuccessKey) return s.store.Get(core.HuaweiSuccessKey)
} }
// GetHuaweiError show error counts of Huawei notification. // GetHuaweiError show error counts of Huawei notification.
func (s *StateStorage) GetHuaweiError() int64 { 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" "sync"
"testing" "testing"
"github.com/appleboy/gorush/storage"
"github.com/appleboy/gorush/config" "github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -19,15 +19,15 @@ func TestBadgerEngine(t *testing.T) {
err := badger.Init() err := badger.Init()
assert.Nil(t, err) assert.Nil(t, err)
badger.Add(storage.HuaweiSuccessKey, 10) badger.Add(core.HuaweiSuccessKey, 10)
val = badger.Get(storage.HuaweiSuccessKey) val = badger.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val) assert.Equal(t, int64(10), val)
badger.Add(storage.HuaweiSuccessKey, 10) badger.Add(core.HuaweiSuccessKey, 10)
val = badger.Get(storage.HuaweiSuccessKey) val = badger.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(20), val) assert.Equal(t, int64(20), val)
badger.Set(storage.HuaweiSuccessKey, 0) badger.Set(core.HuaweiSuccessKey, 0)
val = badger.Get(storage.HuaweiSuccessKey) val = badger.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(0), val) assert.Equal(t, int64(0), val)
// test concurrency issues // test concurrency issues
@ -35,12 +35,12 @@ func TestBadgerEngine(t *testing.T) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
wg.Add(1) wg.Add(1)
go func() { go func() {
badger.Add(storage.HuaweiSuccessKey, 1) badger.Add(core.HuaweiSuccessKey, 1)
wg.Done() wg.Done()
}() }()
} }
wg.Wait() wg.Wait()
val = badger.Get(storage.HuaweiSuccessKey) val = badger.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val) assert.Equal(t, int64(10), val)
assert.NoError(t, badger.Close()) assert.NoError(t, badger.Close())

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,33 +1 @@
package storage 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
}