parent
5259599969
commit
ce5ada70f0
@ -1 +1,2 @@
|
||||
.idea
|
||||
.idea
|
||||
*.cache
|
@ -0,0 +1,42 @@
|
||||
package cache
|
||||
|
||||
import "time"
|
||||
|
||||
//ICache 定义缓存接口
|
||||
type ICache interface {
|
||||
// Add 写入不存在的项目
|
||||
Add(key string, val any, d time.Duration) bool
|
||||
// Put 存储到缓存中
|
||||
Put(key string, val any, d time.Duration)
|
||||
// Has 判断项目是否存在
|
||||
Has(key string) bool
|
||||
// Get 获取项目
|
||||
Get(key string, defaultValue any) any
|
||||
// Forever 永久写入项目
|
||||
Forever(key string, val any)
|
||||
}
|
||||
|
||||
// Cache 定义范型
|
||||
type Cache[T any] struct {
|
||||
ICache ICache
|
||||
}
|
||||
|
||||
func (c *Cache[T]) Has(key string) bool {
|
||||
return c.ICache.Has(key)
|
||||
}
|
||||
|
||||
func (c *Cache[T]) Add(key string, val any, d time.Duration) bool {
|
||||
return c.ICache.Add(key, val, d)
|
||||
}
|
||||
|
||||
func (c *Cache[T]) Put(key string, val T, d time.Duration) {
|
||||
c.ICache.Put(key, val, d)
|
||||
}
|
||||
|
||||
func (c *Cache[T]) Get(key string, defaultValue T) T {
|
||||
return c.ICache.Get(key, defaultValue).(T)
|
||||
}
|
||||
|
||||
func (c *Cache[T]) Forever(key string, val T) {
|
||||
c.ICache.Forever(key, val)
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitea.codecodify.com/golang/tool/helper"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
}
|
||||
|
||||
const EmptyName = ""
|
||||
|
||||
// location 使用cache2go实现本地缓存
|
||||
type location struct {
|
||||
Name string
|
||||
c *cache.Cache
|
||||
}
|
||||
|
||||
func NewLocation(name string) *location {
|
||||
return &location{
|
||||
Name: name,
|
||||
c: getCache(),
|
||||
}
|
||||
}
|
||||
|
||||
func (l location) Add(key string, val any, d time.Duration) bool {
|
||||
if has := l.Has(key); has == true {
|
||||
return false
|
||||
}
|
||||
l.Put(key, val, d)
|
||||
return true
|
||||
}
|
||||
|
||||
func (l location) Has(key string) bool {
|
||||
_, found := l.get(key)
|
||||
return found
|
||||
}
|
||||
|
||||
func (l location) Put(key string, val any, d time.Duration) {
|
||||
l.c.Set(key, val, d)
|
||||
_ = l.saveFile()
|
||||
}
|
||||
|
||||
func (l location) Get(key string, defaultValue any) any {
|
||||
if v, err := l.get(key); err {
|
||||
return v
|
||||
} else {
|
||||
return defaultValue
|
||||
}
|
||||
}
|
||||
|
||||
func (l location) Forever(key string, val any) {
|
||||
l.Put(key, val, cache.NoExpiration)
|
||||
}
|
||||
|
||||
func (l location) get(key string) (any, bool) {
|
||||
_ = l.loadFile()
|
||||
return l.c.Get(key)
|
||||
}
|
||||
|
||||
func getCache() *cache.Cache {
|
||||
return cache.New(2*time.Hour, 24*time.Hour)
|
||||
}
|
||||
|
||||
func getFile(name string) string {
|
||||
name = helper.Ternary(len(name) > 0, name+".cache", "cache.cache").(string)
|
||||
return fmt.Sprintf("%s/%s", helper.GetCurrentAbPath(), name)
|
||||
}
|
||||
|
||||
func (l location) saveFile() error {
|
||||
if err := l.c.SaveFile(getFile(l.Name)); err != nil {
|
||||
log.Printf("【go-cache 设置缓存文件失败】error: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l location) loadFile() error {
|
||||
if err := l.c.LoadFile(getFile(l.Name)); err != nil {
|
||||
log.Printf("【go-cache 加载缓存文件失败】error: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var key = "test"
|
||||
|
||||
func TestLocation_Add(t *testing.T) {
|
||||
location := NewLocation(EmptyName)
|
||||
cache := Cache[string]{
|
||||
ICache: location,
|
||||
}
|
||||
//
|
||||
cache.Put(key, "123456", 1*time.Minute)
|
||||
has := cache.Has("test")
|
||||
fmt.Println(has)
|
||||
|
||||
val := cache.Get(key, "")
|
||||
fmt.Println(val)
|
||||
////
|
||||
//time.Sleep(1 * time.Minute)
|
||||
//val = cache.Get(key, "")
|
||||
//fmt.Println(val)
|
||||
//
|
||||
//time.Sleep(2 * time.Minute)
|
||||
//val = cache.Get(key, "")
|
||||
//fmt.Println(val)
|
||||
}
|
||||
|
||||
func TestLocation_Has(t *testing.T) {
|
||||
location := location{}
|
||||
cache := Cache[string]{
|
||||
ICache: location,
|
||||
}
|
||||
has := cache.Has(key)
|
||||
fmt.Println(has)
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetCurrentAbPath(t *testing.T) {
|
||||
fmt.Println(GetCurrentAbPath())
|
||||
}
|
Loading…
Reference in new issue