index.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package cache
  2. import (
  3. "github.com/druidcaesa/gotool"
  4. "log"
  5. "ulink-admin/modules/system/models/model"
  6. "ulink-admin/pkg/base"
  7. "ulink-admin/pkg/constant"
  8. )
  9. // GetRedisDictKey 根据key获取缓存中的字典数据
  10. func GetRedisDictKey(key string) []model.SysDictData {
  11. if exists, err := base.RedisDB.EXISTS(key); err != nil {
  12. log.Println("redis exists failed:", err)
  13. return nil
  14. } else if exists {
  15. get, err := base.RedisDB.GET(key)
  16. if err != nil {
  17. gotool.Logs.ErrorLog().Println(constant.RedisConstant{}.GetRedisError(), err.Error())
  18. return nil
  19. }
  20. list := model.SysDictData{}.UnmarshalDictList(get)
  21. return list
  22. }
  23. return nil
  24. }
  25. // SetRedisDict 保存字典数据
  26. func SetRedisDict(list []model.SysDictData) {
  27. dictList := model.SysDictData{}.MarshalDictList(list)
  28. base.RedisDB.SET(list[0].DictType, dictList)
  29. }
  30. // GetRedisConfigByKey 根据key从缓存中获取配置数据
  31. func GetRedisConfigByKey(key string) *model.SysConfig {
  32. if exists, err := base.RedisDB.EXISTS(key); err != nil {
  33. log.Println("redis exists failed:", err)
  34. return nil
  35. } else if exists {
  36. get, err := base.RedisDB.GET(key)
  37. if err != nil {
  38. gotool.Logs.ErrorLog().Println(constant.RedisConstant{}.GetRedisError(), err.Error())
  39. return nil
  40. }
  41. obj := model.SysConfig{}.UnmarshalDictObj(get)
  42. return obj
  43. }
  44. return nil
  45. }
  46. // SetRedisConfig 将配置存入缓存
  47. func SetRedisConfig(config *model.SysConfig) {
  48. s := model.SysConfig{}.MarshalDictObj(config)
  49. base.RedisDB.SET(config.ConfigKey, s)
  50. }
  51. // RemoveList 批量根据Key删除数据
  52. func RemoveList(list []string) {
  53. base.RedisDB.DELALL(list)
  54. }
  55. // SetDictCache 添加字典数据
  56. func SetDictCache(key string, value string) {
  57. base.RedisDB.SET(key, value)
  58. }
  59. // RemoveKey 根据key删除
  60. func RemoveKey(key string) int {
  61. del, err := base.RedisDB.DEL(key)
  62. if err != nil {
  63. gotool.Logs.ErrorLog().Println(err)
  64. }
  65. return del
  66. }