mirror of
https://github.com/tiger1103/gfast.git
synced 2026-09-21 18:20:47 +00:00
标签缓存功能,按标签清空缓存
This commit is contained in:
@@ -241,25 +241,16 @@ func (c *Auth) EditRole(r *ghttp.Request) {
|
||||
response.FailJson(true, r, "获取权限处理器失败")
|
||||
}
|
||||
gp := enforcer.GetFilteredNamedPolicy("p", 0, fmt.Sprintf("g_%d", id))
|
||||
g.Log().Debug(gp)
|
||||
gpMap := map[int64]int64{}
|
||||
for _, v := range gp {
|
||||
gpMap[gconv.Int64(gstr.SubStr(v[1], 2))] = gconv.Int64(gstr.SubStr(v[1], 2))
|
||||
}
|
||||
//关联选中的权限
|
||||
for k, v := range mList {
|
||||
if _, has := gpMap[gconv.Int64(v["id"])]; has {
|
||||
v["isChecked"] = true
|
||||
} else {
|
||||
v["isChecked"] = false
|
||||
}
|
||||
mList[k] = v
|
||||
gpSlice := make([]int, len(gp))
|
||||
for k, v := range gp {
|
||||
gpSlice[k] = gconv.Int(gstr.SubStr(v[1], 2))
|
||||
}
|
||||
mList = utils.PushSonToParent(mList)
|
||||
res := g.Map{
|
||||
"parentList": pList,
|
||||
"menuList": mList,
|
||||
"role": role,
|
||||
"parentList": pList,
|
||||
"menuList": mList,
|
||||
"role": role,
|
||||
"checkedRules": gpSlice,
|
||||
}
|
||||
response.SusJson(true, r, "成功", res)
|
||||
}
|
||||
@@ -325,7 +316,7 @@ func (c *Auth) AddUser(r *ghttp.Request) {
|
||||
|
||||
//修改管理员
|
||||
func (c *Auth) EditUser(r *ghttp.Request) {
|
||||
id := r.GetRequestInt64("id")
|
||||
id := r.GetRequestInt("id")
|
||||
if r.Method == "POST" {
|
||||
requestData := r.GetFormMap()
|
||||
err := auth_service.EditUser(requestData)
|
||||
@@ -353,9 +344,16 @@ func (c *Auth) EditUser(r *ghttp.Request) {
|
||||
response.FailJson(true, r, "获取角色数据失败")
|
||||
}
|
||||
roleList = utils.ParentSonSort(roleList, 0, 0, "parent_id", "id", "flg", "name")
|
||||
//获取已选择的角色信息
|
||||
checkedRoleIds, err := user_service.GetAdminRoleIds(id)
|
||||
if err != nil {
|
||||
g.Log().Error(err)
|
||||
response.FailJson(true, r, "获取用户角色数据失败")
|
||||
}
|
||||
res := g.Map{
|
||||
"roleList": roleList,
|
||||
"userInfo": userEntity,
|
||||
"roleList": roleList,
|
||||
"userInfo": userEntity,
|
||||
"checkedRoleIds": checkedRoleIds,
|
||||
}
|
||||
response.SusJson(true, r, "成功", res)
|
||||
}
|
||||
@@ -379,7 +377,7 @@ func (c *Auth) UserList(r *ghttp.Request) {
|
||||
users := make([]g.Map, len(userList))
|
||||
for k, u := range userList {
|
||||
users[k] = gconv.Map(u)
|
||||
roles, err := user_service.GetAdminRole(gconv.Int(u.Id))
|
||||
roles, err := user_service.GetAdminRole(u.Id)
|
||||
if err != nil {
|
||||
g.Log().Error(err)
|
||||
response.FailJson(true, r, "获取用户角色数据失败")
|
||||
|
||||
@@ -300,7 +300,7 @@ func AddUserRole(roleIds interface{}, userId int64) (err error) {
|
||||
}
|
||||
|
||||
//修改用户角色信息
|
||||
func EditUserRole(roleIds interface{}, userId int64) (err error) {
|
||||
func EditUserRole(roleIds interface{}, userId int) (err error) {
|
||||
enforcer, e := casbin_adapter_service.GetEnforcer()
|
||||
if e != nil {
|
||||
err = e
|
||||
|
||||
@@ -48,6 +48,19 @@ func GetAdminList(where g.Map, page int) (total int, userList []*user.Entity, er
|
||||
|
||||
//获取管理员的角色信息
|
||||
func GetAdminRole(userId int) (roles []*role.Entity, err error) {
|
||||
roleIds, err := GetAdminRoleIds(userId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if roleIds != nil {
|
||||
//获取角色信息
|
||||
roles, err = role.Model.Where("id in(?)", roleIds).All()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//获取管理员对应的角色ids
|
||||
func GetAdminRoleIds(userId int) (roleIds []int, err error) {
|
||||
enforcer, e := casbin_adapter_service.GetEnforcer()
|
||||
if e != nil {
|
||||
err = e
|
||||
@@ -56,13 +69,11 @@ func GetAdminRole(userId int) (roles []*role.Entity, err error) {
|
||||
//查询关联角色规则
|
||||
groupPolicy := enforcer.GetFilteredGroupingPolicy(0, fmt.Sprintf("u_%d", userId))
|
||||
if len(groupPolicy) > 0 {
|
||||
roleIds := make([]int, len(groupPolicy))
|
||||
roleIds = make([]int, len(groupPolicy))
|
||||
//得到角色id的切片
|
||||
for k, v := range groupPolicy {
|
||||
roleIds[k] = gconv.Int(gstr.SubStr(v[1], 2))
|
||||
}
|
||||
//获取角色信息
|
||||
roles, err = role.Model.Where("id in(?)", roleIds).All()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
package cache_service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/crypto/gmd5"
|
||||
"github.com/gogf/gf/os/gcache"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CacheTagService struct {
|
||||
tagKey interface{}
|
||||
}
|
||||
|
||||
func New() *CacheTagService {
|
||||
return &CacheTagService{}
|
||||
}
|
||||
|
||||
//设置tag缓存的keys
|
||||
func (c *CacheTagService) cacheTagKey(key interface{}, tag interface{}) {
|
||||
c.setTagKey(tag)
|
||||
if c.tagKey != nil {
|
||||
tagValue := []interface{}{key}
|
||||
value := gcache.Get(c.tagKey)
|
||||
if value != nil {
|
||||
keyValue := gconv.SliceAny(value)
|
||||
hasKey := false
|
||||
for _, v := range keyValue {
|
||||
if reflect.DeepEqual(key, v) {
|
||||
hasKey = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasKey {
|
||||
tagValue = append(tagValue, gconv.SliceAny(value)...)
|
||||
}
|
||||
}
|
||||
gcache.Set(c.tagKey, tagValue, 0)
|
||||
}
|
||||
}
|
||||
|
||||
//获取带标签的键名
|
||||
func (c *CacheTagService) setTagKey(tag interface{}) {
|
||||
if tag != nil {
|
||||
c.tagKey = interface{}(fmt.Sprintf("tag_%s", gmd5.MustEncryptString(gconv.String(tag))))
|
||||
}
|
||||
}
|
||||
|
||||
// Set sets cache with <tagKey>-<value> pair, which is expired after <duration>.
|
||||
// It does not expire if <duration> <= 0.
|
||||
func (c *CacheTagService) Set(key interface{}, value interface{}, duration time.Duration, tag interface{}) {
|
||||
c.cacheTagKey(key, tag)
|
||||
gcache.Set(key, value, duration)
|
||||
}
|
||||
|
||||
// SetIfNotExist sets cache with <tagKey>-<value> pair if <tagKey> does not exist in the cache,
|
||||
// which is expired after <duration>. It does not expire if <duration> <= 0.
|
||||
func (c *CacheTagService) SetIfNotExist(key interface{}, value interface{}, duration time.Duration, tag interface{}) bool {
|
||||
c.cacheTagKey(key, tag)
|
||||
return gcache.SetIfNotExist(key, value, duration)
|
||||
}
|
||||
|
||||
// Sets batch sets cache with tagKey-value pairs by <data>, which is expired after <duration>.
|
||||
//
|
||||
// It does not expire if <duration> <= 0.
|
||||
func (c *CacheTagService) Sets(data map[interface{}]interface{}, duration time.Duration, tag interface{}) {
|
||||
if tag != nil {
|
||||
for k, _ := range data {
|
||||
c.cacheTagKey(k, tag)
|
||||
}
|
||||
gcache.Sets(data, duration)
|
||||
} else {
|
||||
gcache.Sets(data, duration)
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns the value of <tagKey>.
|
||||
// It returns nil if it does not exist or its value is nil.
|
||||
func (c *CacheTagService) Get(key interface{}) interface{} {
|
||||
return gcache.Get(key)
|
||||
}
|
||||
|
||||
// GetOrSet returns the value of <tagKey>,
|
||||
// or sets <tagKey>-<value> pair and returns <value> if <tagKey> does not exist in the cache.
|
||||
// The tagKey-value pair expires after <duration>.
|
||||
//
|
||||
// It does not expire if <duration> <= 0.
|
||||
func (c *CacheTagService) GetOrSet(key interface{}, value interface{}, duration time.Duration, tag interface{}) interface{} {
|
||||
c.cacheTagKey(key, tag)
|
||||
return gcache.GetOrSet(key, value, duration)
|
||||
}
|
||||
|
||||
// GetOrSetFunc returns the value of <tagKey>, or sets <tagKey> with result of function <f>
|
||||
// and returns its result if <tagKey> does not exist in the cache. The tagKey-value pair expires
|
||||
// after <duration>. It does not expire if <duration> <= 0.
|
||||
func (c *CacheTagService) GetOrSetFunc(key interface{}, f func() interface{}, duration time.Duration, tag interface{}) interface{} {
|
||||
c.cacheTagKey(key, tag)
|
||||
return gcache.GetOrSetFunc(key, f, duration)
|
||||
}
|
||||
|
||||
// GetOrSetFuncLock returns the value of <tagKey>, or sets <tagKey> with result of function <f>
|
||||
// and returns its result if <tagKey> does not exist in the cache. The tagKey-value pair expires
|
||||
// after <duration>. It does not expire if <duration> <= 0.
|
||||
//
|
||||
// Note that the function <f> is executed within writing mutex lock.
|
||||
func (c *CacheTagService) GetOrSetFuncLock(key interface{}, f func() interface{}, duration time.Duration, tag interface{}) interface{} {
|
||||
c.cacheTagKey(key, tag)
|
||||
return gcache.GetOrSetFuncLock(key, f, duration)
|
||||
}
|
||||
|
||||
// Contains returns true if <tagKey> exists in the cache, or else returns false.
|
||||
func (c *CacheTagService) Contains(key interface{}) bool {
|
||||
return gcache.Contains(key)
|
||||
}
|
||||
|
||||
// Remove deletes the <tagKey> in the cache, and returns its value.
|
||||
func (c *CacheTagService) Remove(key interface{}) interface{} {
|
||||
return gcache.Remove(key)
|
||||
}
|
||||
|
||||
// Removes deletes <keys> in the cache.
|
||||
func (c *CacheTagService) Removes(keys []interface{}) {
|
||||
gcache.Removes(keys)
|
||||
}
|
||||
|
||||
// Remove deletes the <tag> in the cache, and returns its value.
|
||||
func (c *CacheTagService) RemoveByTag(tag interface{}) {
|
||||
c.setTagKey(tag)
|
||||
//删除tagKey 对应的 key和值
|
||||
keys := c.Get(c.tagKey)
|
||||
if keys != nil {
|
||||
ks := gconv.SliceAny(keys)
|
||||
c.Removes(ks)
|
||||
}
|
||||
c.Remove(c.tagKey)
|
||||
}
|
||||
|
||||
// Removes deletes <tags> in the cache.
|
||||
func (c *CacheTagService) RemoveByTags(tag []interface{}) {
|
||||
for _, v := range tag {
|
||||
c.RemoveByTag(v)
|
||||
}
|
||||
}
|
||||
|
||||
// Data returns a copy of all tagKey-value pairs in the cache as map type.
|
||||
func (c *CacheTagService) Data() map[interface{}]interface{} {
|
||||
return gcache.Data()
|
||||
}
|
||||
|
||||
// Keys returns all keys in the cache as slice.
|
||||
func (c *CacheTagService) Keys() []interface{} {
|
||||
return gcache.Keys()
|
||||
}
|
||||
|
||||
// KeyStrings returns all keys in the cache as string slice.
|
||||
func (c *CacheTagService) KeyStrings() []string {
|
||||
return gcache.KeyStrings()
|
||||
}
|
||||
|
||||
// Values returns all values in the cache as slice.
|
||||
func (c *CacheTagService) Values() []interface{} {
|
||||
return gcache.Values()
|
||||
}
|
||||
|
||||
// Size returns the size of the cache.
|
||||
func (c *CacheTagService) Size() int {
|
||||
return gcache.Size()
|
||||
}
|
||||
Reference in New Issue
Block a user