refactor: code optimization

This commit is contained in:
meilin.huang
2025-04-23 20:36:32 +08:00
parent 798ab7d18b
commit 2170509d92
33 changed files with 445 additions and 380 deletions

54
server/pkg/cache/local.go vendored Normal file
View File

@@ -0,0 +1,54 @@
package cache
import (
"strings"
"time"
"github.com/may-fly/cast"
)
type LocalCache struct {
defaultCache
tm *TimedCache
}
var _ (Cache) = (*LocalCache)(nil)
func NewLocalCache() *LocalCache {
lc := &LocalCache{
tm: NewTimedCache(time.Minute*time.Duration(5), 30*time.Second),
}
lc.c = lc
return lc
}
func (lc *LocalCache) Set(key string, value any, duration time.Duration) error {
return lc.tm.Add(key, value, duration)
}
func (lc *LocalCache) Get(k string) (any, bool) {
return lc.tm.Get(k)
}
func (lc *LocalCache) Delete(k string) error {
lc.tm.Delete(k)
return nil
}
func (lc *LocalCache) DeleteByKeyPrefix(keyPrefix string) error {
for key := range lc.tm.Items() {
if strings.HasPrefix(cast.ToString(key), keyPrefix) {
lc.tm.Delete(key)
}
}
return nil
}
func (lc *LocalCache) Count() int {
return lc.tm.Count()
}
func (lc *LocalCache) Clear() {
lc.tm.Clear()
}