增加go-cache缓存

dev v1.0.1
Chris 1 year ago
parent 5259599969
commit ce5ada70f0

3
.gitignore vendored

@ -1 +1,2 @@
.idea
.idea
*.cache

42
cache/define.go vendored

@ -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)
}

88
cache/location.go vendored

@ -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)
}

@ -3,6 +3,7 @@ module gitea.codecodify.com/golang/tool
go 1.18
require (
github.com/patrickmn/go-cache v2.1.0+incompatible
gorm.io/driver/mysql v1.4.4
gorm.io/gorm v1.24.2
)

@ -6,6 +6,8 @@ github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkr
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
gorm.io/driver/mysql v1.4.4 h1:MX0K9Qvy0Na4o7qSC/YI7XxqUw5KDw01umqgID+svdQ=
gorm.io/driver/mysql v1.4.4/go.mod h1:BCg8cKI+R0j/rZRQxeKis/forqRwRSYOR8OM3Wo6hOM=
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=

@ -1,5 +1,14 @@
package helper
import (
"log"
"os"
"path"
"path/filepath"
"runtime"
"strings"
)
// Ternary 三目运算符
func Ternary(condition bool, trueVal, falseVal interface{}) interface{} {
if condition {
@ -7,3 +16,33 @@ func Ternary(condition bool, trueVal, falseVal interface{}) interface{} {
}
return falseVal
}
// GetCurrentAbPath 最终方案-全兼容
func GetCurrentAbPath() string {
dir := GetCurrentAbPathByExecutable()
tmpDir, _ := filepath.EvalSymlinks(os.TempDir())
if strings.Contains(dir, tmpDir) {
return GetCurrentAbPathByCaller()
}
return dir
}
// GetCurrentAbPathByExecutable 获取当前执行文件绝对路径
func GetCurrentAbPathByExecutable() string {
exePath, err := os.Executable()
if err != nil {
log.Fatal(err)
}
res, _ := filepath.EvalSymlinks(filepath.Dir(exePath))
return res
}
// GetCurrentAbPathByCaller 获取当前执行文件绝对路径go run
func GetCurrentAbPathByCaller() string {
var abPath string
_, filename, _, ok := runtime.Caller(0)
if ok {
abPath = path.Dir(filename)
}
return abPath
}

@ -0,0 +1,10 @@
package helper
import (
"fmt"
"testing"
)
func TestGetCurrentAbPath(t *testing.T) {
fmt.Println(GetCurrentAbPath())
}
Loading…
Cancel
Save