|
@@ -1,24 +1,30 @@
|
|
package local
|
|
package local
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "github.com/pkg/errors"
|
|
"strings"
|
|
"strings"
|
|
"sync"
|
|
"sync"
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+type valueItem struct {
|
|
|
|
+ value string
|
|
|
|
+ expireStartTime time.Time
|
|
|
|
+ expireSec int64
|
|
|
|
+ expireDoneChan chan any
|
|
|
|
+}
|
|
|
|
+
|
|
type Cache struct {
|
|
type Cache struct {
|
|
- namespace string
|
|
|
|
- syncMap *sync.Map
|
|
|
|
- expireRoutineDoneChannelsMutex *sync.Mutex
|
|
|
|
- expireRoutineDoneChannels []chan any
|
|
|
|
|
|
+ *sync.RWMutex
|
|
|
|
+ namespace string
|
|
|
|
+ cacheMap map[string]*valueItem
|
|
}
|
|
}
|
|
|
|
|
|
func New(namespace string) *Cache {
|
|
func New(namespace string) *Cache {
|
|
return &Cache{
|
|
return &Cache{
|
|
- namespace: namespace,
|
|
|
|
- syncMap: new(sync.Map),
|
|
|
|
- expireRoutineDoneChannelsMutex: new(sync.Mutex),
|
|
|
|
- expireRoutineDoneChannels: make([]chan any, 0),
|
|
|
|
|
|
+ RWMutex: new(sync.RWMutex),
|
|
|
|
+ namespace: namespace,
|
|
|
|
+ cacheMap: make(map[string]*valueItem),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -27,124 +33,194 @@ func Destroy(cache *Cache) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- cache.expireRoutineDoneChannelsMutex.Lock()
|
|
|
|
|
|
+ cache.Lock()
|
|
|
|
+ defer cache.Unlock()
|
|
|
|
|
|
- for _, doneChan := range cache.expireRoutineDoneChannels {
|
|
|
|
- doneChan <- nil
|
|
|
|
- close(doneChan)
|
|
|
|
- doneChan = nil
|
|
|
|
|
|
+ for _, item := range cache.cacheMap {
|
|
|
|
+ if item.expireDoneChan != nil {
|
|
|
|
+ item.expireDoneChan <- nil
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- cache.expireRoutineDoneChannels = nil
|
|
|
|
-
|
|
|
|
- cache.expireRoutineDoneChannelsMutex.Unlock()
|
|
|
|
-
|
|
|
|
- cache.syncMap = nil
|
|
|
|
|
|
+ cache.cacheMap = nil
|
|
}
|
|
}
|
|
|
|
|
|
func (cache *Cache) Set(key string, value string, expireSec int64) error {
|
|
func (cache *Cache) Set(key string, value string, expireSec int64) error {
|
|
- cache.syncMap.Store(cache.key2CacheKey(key), value)
|
|
|
|
|
|
+ cache.Lock()
|
|
|
|
+ defer cache.Unlock()
|
|
|
|
+
|
|
|
|
+ oldItem, ok := cache.cacheMap[cache.key2CacheKey(key)]
|
|
|
|
+ if ok {
|
|
|
|
+ cache.stopExpireRoutine(oldItem)
|
|
|
|
|
|
- if expireSec != 0 {
|
|
|
|
- doneChan := make(chan any)
|
|
|
|
|
|
+ newItem := &valueItem{
|
|
|
|
+ value: value,
|
|
|
|
+ expireStartTime: time.Now(),
|
|
|
|
+ expireSec: expireSec,
|
|
|
|
+ }
|
|
|
|
|
|
- cache.expireRoutineDoneChannelsMutex.Lock()
|
|
|
|
|
|
+ cache.cacheMap[cache.key2CacheKey(key)] = newItem
|
|
|
|
|
|
- if cache.expireRoutineDoneChannels != nil {
|
|
|
|
- cache.expireRoutineDoneChannels = append(cache.expireRoutineDoneChannels, doneChan)
|
|
|
|
|
|
+ if expireSec == 0 {
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
|
|
|
|
- cache.expireRoutineDoneChannelsMutex.Unlock()
|
|
|
|
|
|
+ cache.startExpireRoutine(key, newItem)
|
|
|
|
|
|
- go func() {
|
|
|
|
- timer := time.NewTimer(time.Second * time.Duration(expireSec))
|
|
|
|
|
|
+ return nil
|
|
|
|
+ }
|
|
|
|
|
|
- defer func() {
|
|
|
|
- timer.Stop()
|
|
|
|
|
|
+ item := &valueItem{
|
|
|
|
+ value: value,
|
|
|
|
+ expireStartTime: time.Now(),
|
|
|
|
+ expireSec: expireSec,
|
|
|
|
+ }
|
|
|
|
|
|
- cache.expireRoutineDoneChannelsMutex.Lock()
|
|
|
|
-
|
|
|
|
- if cache.expireRoutineDoneChannels != nil {
|
|
|
|
- findIndex := -1
|
|
|
|
-
|
|
|
|
- for i, savedDoneChan := range cache.expireRoutineDoneChannels {
|
|
|
|
- if savedDoneChan == doneChan {
|
|
|
|
- findIndex = i
|
|
|
|
- close(savedDoneChan)
|
|
|
|
- savedDoneChan = nil
|
|
|
|
- break
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if findIndex != -1 {
|
|
|
|
- cache.expireRoutineDoneChannels =
|
|
|
|
- append(cache.expireRoutineDoneChannels[:findIndex], cache.expireRoutineDoneChannels[findIndex+1:]...)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- cache.expireRoutineDoneChannelsMutex.Unlock()
|
|
|
|
- }()
|
|
|
|
-
|
|
|
|
- for {
|
|
|
|
- select {
|
|
|
|
- case <-timer.C:
|
|
|
|
- cache.syncMap.Delete(cache.key2CacheKey(key))
|
|
|
|
- return
|
|
|
|
- case <-doneChan:
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }()
|
|
|
|
|
|
+ cache.cacheMap[cache.key2CacheKey(key)] = item
|
|
|
|
+
|
|
|
|
+ if item.expireSec != 0 {
|
|
|
|
+ cache.startExpireRoutine(key, item)
|
|
}
|
|
}
|
|
|
|
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
func (cache *Cache) Get(key string) (string, error) {
|
|
func (cache *Cache) Get(key string) (string, error) {
|
|
- value, loaded := cache.syncMap.Load(cache.key2CacheKey(key))
|
|
|
|
- if !loaded {
|
|
|
|
|
|
+ cache.RLock()
|
|
|
|
+ defer cache.RUnlock()
|
|
|
|
+
|
|
|
|
+ item, ok := cache.cacheMap[cache.key2CacheKey(key)]
|
|
|
|
+ if !ok {
|
|
return "", nil
|
|
return "", nil
|
|
}
|
|
}
|
|
|
|
|
|
- return value.(string), nil
|
|
|
|
|
|
+ return item.value, nil
|
|
}
|
|
}
|
|
|
|
|
|
func (cache *Cache) GetMulti(keys []string) (map[string]string, error) {
|
|
func (cache *Cache) GetMulti(keys []string) (map[string]string, error) {
|
|
|
|
+ cache.RLock()
|
|
|
|
+ defer cache.RUnlock()
|
|
|
|
+
|
|
result := make(map[string]string)
|
|
result := make(map[string]string)
|
|
|
|
|
|
for _, key := range keys {
|
|
for _, key := range keys {
|
|
- value, loaded := cache.syncMap.Load(cache.key2CacheKey(key))
|
|
|
|
- if !loaded {
|
|
|
|
|
|
+ item, ok := cache.cacheMap[cache.key2CacheKey(key)]
|
|
|
|
+ if !ok {
|
|
result[key] = ""
|
|
result[key] = ""
|
|
}
|
|
}
|
|
|
|
|
|
- result[key] = value.(string)
|
|
|
|
|
|
+ result[key] = item.value
|
|
}
|
|
}
|
|
|
|
|
|
return result, nil
|
|
return result, nil
|
|
}
|
|
}
|
|
|
|
|
|
func (cache *Cache) GetAll() (map[string]string, error) {
|
|
func (cache *Cache) GetAll() (map[string]string, error) {
|
|
|
|
+ cache.RLock()
|
|
|
|
+ defer cache.RUnlock()
|
|
|
|
+
|
|
result := make(map[string]string)
|
|
result := make(map[string]string)
|
|
|
|
|
|
- cache.syncMap.Range(func(key any, value any) bool {
|
|
|
|
- result[cache.cacheKey2Key(key.(string))] = value.(string)
|
|
|
|
- return true
|
|
|
|
- })
|
|
|
|
|
|
+ for key, item := range cache.cacheMap {
|
|
|
|
+ result[cache.cacheKey2Key(key)] = item.value
|
|
|
|
+ }
|
|
|
|
|
|
return result, nil
|
|
return result, nil
|
|
}
|
|
}
|
|
|
|
|
|
func (cache *Cache) Delete(key string) error {
|
|
func (cache *Cache) Delete(key string) error {
|
|
- cache.syncMap.Delete(cache.key2CacheKey(key))
|
|
|
|
|
|
+ cache.Lock()
|
|
|
|
+ defer cache.Unlock()
|
|
|
|
+
|
|
|
|
+ delete(cache.cacheMap, cache.key2CacheKey(key))
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
func (cache *Cache) Clear() error {
|
|
func (cache *Cache) Clear() error {
|
|
- cache.syncMap = new(sync.Map)
|
|
|
|
|
|
+ cache.Lock()
|
|
|
|
+ defer cache.Unlock()
|
|
|
|
+
|
|
|
|
+ cache.cacheMap = make(map[string]*valueItem)
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (cache *Cache) Expire(key string, expireSec int64) error {
|
|
|
|
+ cache.Lock()
|
|
|
|
+ defer cache.Unlock()
|
|
|
|
+
|
|
|
|
+ oldItem, ok := cache.cacheMap[cache.key2CacheKey(key)]
|
|
|
|
+ if !ok {
|
|
|
|
+ return errors.New("对应的键不存在: " + key)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ cache.stopExpireRoutine(oldItem)
|
|
|
|
+
|
|
|
|
+ newItem := &valueItem{
|
|
|
|
+ value: oldItem.value,
|
|
|
|
+ expireStartTime: time.Now(),
|
|
|
|
+ expireSec: expireSec,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ cache.cacheMap[cache.key2CacheKey(key)] = newItem
|
|
|
|
+
|
|
|
|
+ if expireSec == 0 {
|
|
|
|
+ return nil
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ cache.startExpireRoutine(key, newItem)
|
|
|
|
+
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (cache *Cache) TTL(key string) (int64, error) {
|
|
|
|
+ cache.RLock()
|
|
|
|
+ defer cache.RUnlock()
|
|
|
|
+
|
|
|
|
+ item, ok := cache.cacheMap[cache.key2CacheKey(key)]
|
|
|
|
+ if !ok {
|
|
|
|
+ return 0, nil
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ remainSec := int64(item.expireStartTime.Add(time.Duration(item.expireSec)*time.Second).Sub(time.Now()).Seconds() + 0.5)
|
|
|
|
+ if remainSec <= 0 {
|
|
|
|
+ return 0, nil
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return remainSec, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (cache *Cache) startExpireRoutine(key string, item *valueItem) {
|
|
|
|
+ item.expireDoneChan = make(chan any)
|
|
|
|
+
|
|
|
|
+ go func() {
|
|
|
|
+ timer := time.NewTimer(time.Second * time.Duration(item.expireSec))
|
|
|
|
+
|
|
|
|
+ defer func() {
|
|
|
|
+ if item.expireDoneChan != nil {
|
|
|
|
+ close(item.expireDoneChan)
|
|
|
|
+ item.expireDoneChan = nil
|
|
|
|
+ }
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ for {
|
|
|
|
+ select {
|
|
|
|
+ case <-timer.C:
|
|
|
|
+ delete(cache.cacheMap, cache.key2CacheKey(key))
|
|
|
|
+ return
|
|
|
|
+ case <-item.expireDoneChan:
|
|
|
|
+ timer.Stop()
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (cache *Cache) stopExpireRoutine(item *valueItem) {
|
|
|
|
+ if item.expireDoneChan != nil {
|
|
|
|
+ item.expireDoneChan <- nil
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
func (cache *Cache) key2CacheKey(key string) string {
|
|
func (cache *Cache) key2CacheKey(key string) string {
|
|
return cache.namespace + "::" + key
|
|
return cache.namespace + "::" + key
|
|
}
|
|
}
|