forked from LeoYoung-code/utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
37 lines (30 loc) · 675 Bytes
/
cache.go
File metadata and controls
37 lines (30 loc) · 675 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package utils
import (
"time"
"github.com/go-kratos/kratos/v2/log"
"github.com/jinzhu/copier"
"github.com/patrickmn/go-cache"
)
var cacheDriver = cache.New(1*time.Minute, 10*time.Minute)
func SetGoCache[T any](key string, val T, exp time.Duration) {
cacheDriver.Set(key, val, exp)
}
func GetGoCache[T any](key string, defaultVal T) (T, bool) {
val, ok := cacheDriver.Get(key)
if !ok {
return defaultVal, ok
}
return val.(T), ok
}
func DelGoCache(key string) {
cacheDriver.Delete(key)
}
func DeepCopy(to, form any) {
err := copier.CopyWithOption(to, form, copier.Option{
IgnoreEmpty: true,
DeepCopy: true,
})
if err != nil {
log.Error(err)
}
}