Repository navigation
Expand file tree
/
Copy pathcache.go
More file actions
91 lines (80 loc) · 2.44 KB
/
Copy pathcache.go
File metadata and controls
91 lines (80 loc) · 2.44 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package v1
import (
"context"
"fmt"
"gin-web/models"
"gin-web/pkg/global"
"gin-web/pkg/response"
"github.com/piupuer/go-helper/pkg/constant"
"github.com/piupuer/go-helper/pkg/utils"
)
// CacheGetUserInfo get user info from cache by uid
func CacheGetUserInfo(ctx context.Context, uid uint) (u response.UserInfo, exists bool) {
if global.Conf.Redis.Enable {
res, err := global.Redis.HGet(ctx, getUserInfoCacheKeyPrefix(), fmt.Sprintf("%d", uid)).Result()
if err == nil && res != "" {
utils.Json2Struct(res, &u)
if u.Id > constant.Zero {
exists = true
}
return
}
}
return
}
// CacheSetUserInfo set user info to cache by uid
func CacheSetUserInfo(ctx context.Context, uid uint, data response.UserInfo) {
if global.Conf.Redis.Enable {
global.Redis.HSet(ctx, getUserInfoCacheKeyPrefix(), fmt.Sprintf("%d", uid), utils.Struct2Json(data))
}
}
// CacheDeleteUserInfo delete user info
func CacheDeleteUserInfo(ctx context.Context, uid uint) {
if global.Conf.Redis.Enable {
global.Redis.HDel(ctx, getUserInfoCacheKeyPrefix(), fmt.Sprintf("%d", uid))
}
}
// CacheFlushUserInfo clear user info cache
func CacheFlushUserInfo(ctx context.Context) {
if global.Conf.Redis.Enable {
global.Redis.Del(ctx, getUserInfoCacheKeyPrefix())
}
}
// CacheGetUser get user from cache by uid
func CacheGetUser(ctx context.Context, uid uint) (u models.SysUser, exists bool) {
if global.Conf.Redis.Enable {
res, err := global.Redis.HGet(ctx, getUserCacheKeyPrefix(), fmt.Sprintf("%d", uid)).Result()
if err == nil && res != "" {
utils.Json2Struct(res, &u)
if u.Id > constant.Zero {
exists = true
}
return
}
}
return
}
// CacheSetUser set user to cache by uid
func CacheSetUser(ctx context.Context, uid uint, data models.SysUser) {
if global.Conf.Redis.Enable {
global.Redis.HSet(ctx, getUserCacheKeyPrefix(), fmt.Sprintf("%d", uid), utils.Struct2Json(data))
}
}
// CacheDeleteUser delete user
func CacheDeleteUser(ctx context.Context, uid uint) {
if global.Conf.Redis.Enable {
global.Redis.HDel(ctx, getUserCacheKeyPrefix(), fmt.Sprintf("%d", uid))
}
}
// CacheFlushUser clear user cache
func CacheFlushUser(ctx context.Context) {
if global.Conf.Redis.Enable {
global.Redis.Del(ctx, getUserCacheKeyPrefix())
}
}
func getUserCacheKeyPrefix() string {
return fmt.Sprintf("%s_%s", global.Conf.Mysql.DSN.DBName, "v1_user")
}
func getUserInfoCacheKeyPrefix() string {
return fmt.Sprintf("%s_%s", global.Conf.Mysql.DSN.DBName, "v1_user_info")
}