DictUtils.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package com.ruoyi.common.utils;
  2. import java.util.Collection;
  3. import java.util.List;
  4. import com.ruoyi.common.constant.Constants;
  5. import com.ruoyi.common.core.domain.entity.SysDictData;
  6. import com.ruoyi.common.core.redis.RedisCache;
  7. import com.ruoyi.common.utils.spring.SpringUtils;
  8. /**
  9. * 字典工具类
  10. *
  11. * @author ruoyi
  12. */
  13. public class DictUtils
  14. {
  15. /**
  16. * 设置字典缓存
  17. *
  18. * @param key 参数键
  19. * @param dictDatas 字典数据列表
  20. */
  21. public static void setDictCache(String key, List<SysDictData> dictDatas)
  22. {
  23. SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(key), dictDatas);
  24. }
  25. /**
  26. * 获取字典缓存
  27. *
  28. * @param key 参数键
  29. * @return dictDatas 字典数据列表
  30. */
  31. public static List<SysDictData> getDictCache(String key)
  32. {
  33. Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
  34. if (StringUtils.isNotNull(cacheObj))
  35. {
  36. List<SysDictData> DictDatas = StringUtils.cast(cacheObj);
  37. return DictDatas;
  38. }
  39. return null;
  40. }
  41. /**
  42. * 清空字典缓存
  43. */
  44. public static void clearDictCache()
  45. {
  46. Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(Constants.SYS_DICT_KEY + "*");
  47. SpringUtils.getBean(RedisCache.class).deleteObject(keys);
  48. }
  49. /**
  50. * 设置cache key
  51. *
  52. * @param configKey 参数键
  53. * @return 缓存键key
  54. */
  55. public static String getCacheKey(String configKey)
  56. {
  57. return Constants.SYS_DICT_KEY + configKey;
  58. }
  59. }