Files
mayfly-go/server/pkg/cache/local.go

55 lines
903 B
Go
Raw Normal View History

2025-04-23 20:36:32 +08:00
package cache
import (
"strings"
"time"
2025-06-27 12:17:45 +08:00
"github.com/spf13/cast"
2025-04-23 20:36:32 +08:00
)
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()
}