@@ -2,10 +2,13 @@ package cache
2
2
3
3
import (
4
4
"errors"
5
+ "log/slog"
5
6
"time"
6
7
7
8
"github.com/goburrow/cache"
9
+ "github.com/mlogclub/simple/common/jsons"
8
10
"github.com/mlogclub/simple/sqls"
11
+ "github.com/spf13/cast"
9
12
10
13
"bbs-go/internal/models"
11
14
"bbs-go/internal/repositories"
@@ -21,8 +24,9 @@ func newSysConfigCache() *sysConfigCache {
21
24
return & sysConfigCache {
22
25
cache : cache .NewLoadingCache (
23
26
func (key cache.Key ) (value cache.Value , e error ) {
24
- value = repositories .SysConfigRepository .GetByKey (sqls .DB (), key .(string ))
25
- if value == nil {
27
+ if ret := repositories .SysConfigRepository .GetByKey (sqls .DB (), key .(string )); ret != nil {
28
+ value = ret
29
+ } else {
26
30
e = errors .New ("数据不存在" )
27
31
}
28
32
return
@@ -44,12 +48,34 @@ func (c *sysConfigCache) Get(key string) *models.SysConfig {
44
48
return nil
45
49
}
46
50
47
- func (c * sysConfigCache ) GetValue (key string ) string {
48
- sysConfig := c .Get (key )
49
- if sysConfig == nil {
50
- return ""
51
+ func (c * sysConfigCache ) GetStr (key string ) string {
52
+ if t := c .Get (key ); t != nil {
53
+ return t .Value
51
54
}
52
- return sysConfig .Value
55
+ return ""
56
+ }
57
+
58
+ func (c * sysConfigCache ) GetBool (key string ) bool {
59
+ str := c .GetStr (key )
60
+ return cast .ToBool (str )
61
+ }
62
+
63
+ func (c * sysConfigCache ) GetInt (key string ) int {
64
+ str := c .GetStr (key )
65
+ return cast .ToInt (str )
66
+ }
67
+
68
+ func (c * sysConfigCache ) GetInt64 (key string ) int64 {
69
+ str := c .GetStr (key )
70
+ return cast .ToInt64 (str )
71
+ }
72
+
73
+ func (c * sysConfigCache ) GetStrArr (key string ) (ret []string ) {
74
+ str := c .GetStr (key )
75
+ if err := jsons .Parse (str , & ret ); err != nil {
76
+ slog .Warn ("config value error" , slog .Any ("key" , key ), slog .Any ("err" , err ))
77
+ }
78
+ return
53
79
}
54
80
55
81
func (c * sysConfigCache ) Invalidate (key string ) {
0 commit comments