2016-05-02 03:04:19 +00:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
2022-02-03 09:03:37 +00:00
|
|
|
"sync"
|
2016-05-02 09:03:08 +00:00
|
|
|
|
2022-02-03 09:03:37 +00:00
|
|
|
"go.uber.org/atomic"
|
|
|
|
)
|
2020-09-04 03:01:21 +00:00
|
|
|
|
2016-07-31 13:10:59 +00:00
|
|
|
// New func implements the storage interface for gorush (https://github.com/appleboy/gorush)
|
2016-05-02 09:03:08 +00:00
|
|
|
func New() *Storage {
|
2022-02-03 09:03:37 +00:00
|
|
|
return &Storage{}
|
2016-05-02 03:04:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 02:57:00 +00:00
|
|
|
// Storage is interface structure
|
2016-05-02 03:04:19 +00:00
|
|
|
type Storage struct {
|
2022-02-03 09:03:37 +00:00
|
|
|
mem sync.Map
|
2016-05-02 03:04:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 09:03:37 +00:00
|
|
|
func (s *Storage) getValueBtKey(key string) *atomic.Int64 {
|
|
|
|
if val, ok := s.mem.Load(key); ok {
|
|
|
|
return val.(*atomic.Int64)
|
|
|
|
}
|
|
|
|
val := atomic.NewInt64(0)
|
|
|
|
s.mem.Store(key, val)
|
|
|
|
return val
|
2016-05-02 03:04:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 09:03:37 +00:00
|
|
|
func (s *Storage) Add(key string, count int64) {
|
|
|
|
s.getValueBtKey(key).Add(count)
|
2016-05-02 03:04:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 09:03:37 +00:00
|
|
|
func (s *Storage) Set(key string, count int64) {
|
|
|
|
s.getValueBtKey(key).Store(count)
|
2016-05-02 03:04:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 09:03:37 +00:00
|
|
|
func (s *Storage) Get(key string) int64 {
|
|
|
|
return s.getValueBtKey(key).Load()
|
2016-05-02 03:04:19 +00:00
|
|
|
}
|
2020-09-04 03:01:21 +00:00
|
|
|
|
2022-02-03 09:03:37 +00:00
|
|
|
// Init client storage.
|
|
|
|
func (*Storage) Init() error {
|
|
|
|
return nil
|
2020-09-04 03:01:21 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 09:03:37 +00:00
|
|
|
// Close the storage connection
|
|
|
|
func (*Storage) Close() error {
|
|
|
|
return nil
|
2020-09-04 03:01:21 +00:00
|
|
|
}
|