当前位置:   article > 正文

个人推荐Redis比较好的一种使用规范

个人推荐Redis比较好的一种使用规范

随着对个人项目的不断开发、迭代和重构,博主在这个过程中总结出了一套使用redis的较好的规范。主要包含Redis的key命名规范和Redis代码规范

主要内容

主要包含以下几个内容:

  • 同一应用的key在最前面添加统一的前缀,如应用名;
    • 案例:appName:业务相关的key前缀:具有代表性的字段的值
    • 例如:mhxysy:SEVER:1
  • 所有key的前缀,根据业务功能,以常量的形式保存在同一个接口中;
  • 一个业务的Redis缓存对应一个缓存类,使代码易于维护,把缓存的代码统一编写在一个类里,如果想要取消缓存,只需要修改这个缓存类的代码即可。缓存类可以根据自己的喜好来命名,如XxxRepository;
  • 为了体现面向接口的开发原则,可以让所有Reids得缓存类实现一个代表Reids缓存的顶级接口,如RedisRepository;

代码展示

角色信息的缓存类RoleAccountRepository.java

  1. package cn.edu.sgu.www.mhxysy.redis.repository.impl;
  2. import cn.edu.sgu.www.mhxysy.consts.RedisKeyPrefixes;
  3. import cn.edu.sgu.www.mhxysy.entity.role.RoleAccount;
  4. import cn.edu.sgu.www.mhxysy.redis.RedisUtils;
  5. import cn.edu.sgu.www.mhxysy.redis.repository.RedisRepository;
  6. import cn.edu.sgu.www.mhxysy.util.TimerUtils;
  7. import cn.edu.sgu.www.mhxysy.vo.role.RoleAccountVO;
  8. import com.alibaba.fastjson.JSON;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.stereotype.Component;
  11. import java.util.List;
  12. import java.util.TimerTask;
  13. import java.util.concurrent.TimeUnit;
  14. import java.util.function.Consumer;
  15. /**
  16. * @author heyunlin
  17. * @version 1.0
  18. */
  19. @Slf4j
  20. @Component
  21. public class RoleAccountRepository implements RedisRepository {
  22. private final RedisUtils redisUtils;
  23. public RoleAccountRepository(RedisUtils redisUtils) {
  24. this.redisUtils = redisUtils;
  25. }
  26. @Override
  27. public void put(String id, Object value) {
  28. String key = RedisKeyPrefixes.PREFIX_ROLE_ACCOUNT + id;
  29. redisUtils.set(key, JSON.toJSONString(value));
  30. redisUtils.expire(key, 7, TimeUnit.DAYS);
  31. }
  32. @Override
  33. public RoleAccount get(String id) {
  34. String key = RedisKeyPrefixes.PREFIX_ROLE_ACCOUNT + id;
  35. String value = redisUtils.get(key);
  36. if (value != null) {
  37. log.debug("命中缓存{}", key);
  38. }
  39. return JSON.parseObject(value, RoleAccount.class);
  40. }
  41. @Override
  42. public void putList(Object value) {
  43. String key = RedisKeyPrefixes.PREFIX_ROLE_ACCOUNTS;
  44. redisUtils.set(key, JSON.toJSONString(value), 7, TimeUnit.DAYS);
  45. }
  46. @Override
  47. public List<RoleAccount> getList() {
  48. String key = RedisKeyPrefixes.PREFIX_ROLE_ACCOUNTS;
  49. String value = redisUtils.get(key);
  50. if (value != null) {
  51. log.debug("命中缓存{}", key);
  52. return JSON.parseArray(value, RoleAccount.class);
  53. }
  54. return null;
  55. }
  56. @Override
  57. public void delete() {
  58. String key = RedisKeyPrefixes.PREFIX_ROLE_ACCOUNTS;
  59. redisUtils.delete(key);
  60. }
  61. @Override
  62. public void delete(String primaryKey) {
  63. redisUtils.delete(RedisKeyPrefixes.PREFIX_ROLE_ACCOUNT + primaryKey);
  64. }
  65. @Override
  66. public void delayDoubleDelete(String primaryKey, Consumer<String> consumer) {
  67. delete();
  68. delete(primaryKey);
  69. deleteCache(primaryKey);
  70. consumer.accept(primaryKey);
  71. // 延时双删
  72. TimerUtils.schedule(new TimerTask() {
  73. @Override
  74. public void run() {
  75. delete();
  76. delete(primaryKey);
  77. deleteCache(primaryKey);
  78. }
  79. });
  80. }
  81. public void cacheRole(String roleId, RoleAccountVO roleAccountVO) {
  82. String key = RedisKeyPrefixes.PREFIX_ROLE_DETAIL + roleId;
  83. redisUtils.set(key, JSON.toJSONString(roleAccountVO));
  84. }
  85. public RoleAccountVO getCache(String roleId) {
  86. String key = RedisKeyPrefixes.PREFIX_ROLE_DETAIL + roleId;
  87. String value = redisUtils.get(key);
  88. if (value != null) {
  89. log.debug("命中缓存{}...", key);
  90. return JSON.parseObject(value, RoleAccountVO.class);
  91. }
  92. return null;
  93. }
  94. public void deleteCache(String roleId) {
  95. String key = RedisKeyPrefixes.PREFIX_ROLE_DETAIL + roleId;
  96. redisUtils.delete(key);
  97. }
  98. }

具体的角色service层方法

  1. package cn.edu.sgu.www.mhxysy.service.role.impl;
  2. import cn.edu.sgu.www.mhxysy.base.Pager;
  3. import cn.edu.sgu.www.mhxysy.dto.role.RoleAccountDTO;
  4. import cn.edu.sgu.www.mhxysy.easyexel.impl.RoleAccountExcelData;
  5. import cn.edu.sgu.www.mhxysy.entity.gang.Gang;
  6. import cn.edu.sgu.www.mhxysy.entity.role.Account;
  7. import cn.edu.sgu.www.mhxysy.entity.role.RoleAccount;
  8. import cn.edu.sgu.www.mhxysy.entity.role.RoleModeling;
  9. import cn.edu.sgu.www.mhxysy.entity.school.School;
  10. import cn.edu.sgu.www.mhxysy.entity.server.Server;
  11. import cn.edu.sgu.www.mhxysy.exception.GlobalException;
  12. import cn.edu.sgu.www.mhxysy.mapper.role.JiadianSchemaMapper;
  13. import cn.edu.sgu.www.mhxysy.mapper.role.RoleAccountMapper;
  14. import cn.edu.sgu.www.mhxysy.mapper.role.RoleAttributeMapper;
  15. import cn.edu.sgu.www.mhxysy.mapper.role.RoleJiadianMapper;
  16. import cn.edu.sgu.www.mhxysy.mapper.school.SchoolSkillCategoryMapper;
  17. import cn.edu.sgu.www.mhxysy.mapper.school.SchoolSkillMapper;
  18. import cn.edu.sgu.www.mhxysy.redis.repository.impl.RoleAccountRepository;
  19. import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
  20. import cn.edu.sgu.www.mhxysy.service.gang.GangService;
  21. import cn.edu.sgu.www.mhxysy.service.role.AccountService;
  22. import cn.edu.sgu.www.mhxysy.service.role.RoleAccountService;
  23. import cn.edu.sgu.www.mhxysy.service.role.RoleModelingService;
  24. import cn.edu.sgu.www.mhxysy.service.school.SchoolService;
  25. import cn.edu.sgu.www.mhxysy.service.server.ServerService;
  26. import cn.edu.sgu.www.mhxysy.vo.role.RoleAccountVO;
  27. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  28. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.stereotype.Service;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.Map;
  34. import java.util.function.Consumer;
  35. /**
  36. * @author heyunlin
  37. * @version 1.0
  38. */
  39. @Service
  40. public class RoleAccountServiceImpl implements RoleAccountService {
  41. private final RoleAccountMapper roleAccountMapper;
  42. private final RoleJiadianMapper roleJiadianMapper;
  43. private final SchoolSkillMapper schoolSkillMapper;
  44. private final JiadianSchemaMapper jiadianSchemaMapper;
  45. private final RoleAttributeMapper roleAttributeMapper;
  46. private final RoleAccountRepository roleAccountRepository;
  47. private final SchoolSkillCategoryMapper schoolSkillCategoryMapper;
  48. private final GangService gangService;
  49. private final ServerService serverService;
  50. private final SchoolService schoolService;
  51. private final AccountService accountService;
  52. private final RoleModelingService roleModelingService;
  53. @Autowired
  54. public RoleAccountServiceImpl(
  55. RoleAccountMapper roleAccountMapper,
  56. RoleJiadianMapper roleJiadianMapper,
  57. SchoolSkillMapper schoolSkillMapper,
  58. JiadianSchemaMapper jiadianSchemaMapper,
  59. RoleAttributeMapper roleAttributeMapper,
  60. RoleAccountRepository roleAccountRepository,
  61. SchoolSkillCategoryMapper schoolSkillCategoryMapper,
  62. GangService gangService,
  63. ServerService serverService,
  64. SchoolService schoolService,
  65. AccountService accountService,
  66. RoleModelingService roleModelingService) {
  67. this.roleAccountMapper = roleAccountMapper;
  68. this.roleJiadianMapper = roleJiadianMapper;
  69. this.schoolSkillMapper = schoolSkillMapper;
  70. this.jiadianSchemaMapper = jiadianSchemaMapper;
  71. this.roleAttributeMapper = roleAttributeMapper;
  72. this.roleAccountRepository = roleAccountRepository;
  73. this.schoolSkillCategoryMapper = schoolSkillCategoryMapper;
  74. this.gangService = gangService;
  75. this.serverService = serverService;
  76. this.schoolService = schoolService;
  77. this.accountService = accountService;
  78. this.roleModelingService = roleModelingService;
  79. }
  80. @Override
  81. public RoleAccount selectById(String roleId) {
  82. RoleAccount roleAccount = roleAccountRepository.get(roleId);
  83. if (roleAccount == null) {
  84. roleAccount = roleAccountMapper.selectById(roleId);
  85. roleAccountRepository.put(roleId, roleAccount);
  86. }
  87. return roleAccount;
  88. }
  89. }

Redis的key前缀常量接口代码展示

  1. package cn.edu.sgu.www.mhxysy.consts;
  2. /**
  3. * redis的key前缀的常量接口
  4. * @author heyunlin
  5. * @version 1.0
  6. */
  7. public interface RedisKeyPrefixes {
  8. String PREFIX_BASE = "mhxysy:";
  9. /**
  10. * 用户的邮箱
  11. */
  12. String PREFIX_EMAIL = PREFIX_BASE + "EMAIL:";
  13. /**
  14. * 用户登录的验证码
  15. */
  16. String PREFIX_CAPTCHA = PREFIX_BASE + "CAPTCHA:";
  17. /**
  18. * 用户的侧栏菜单
  19. */
  20. String PREFIX_USER_LEFT_MENUS = PREFIX_BASE + "USER_LEFT_MENUS:";
  21. /**
  22. * 角色的菜单树
  23. */
  24. String PREFIX_ROLE_MENU_TREE = PREFIX_BASE + "ROLE_MENU_TREE:";
  25. /**
  26. * 用户登录的次数
  27. */
  28. String PREFIX_USER_LOGIN_TIMES = PREFIX_BASE + "USER_LOGIN_TIMES:";
  29. /**
  30. * shiro缓存认证管理
  31. */
  32. String PREFIX_CACHE_MANAGER = PREFIX_BASE + "AUTHORIZATION:CACHE_MANAGER:";
  33. /**
  34. * 角色高级属性
  35. */
  36. String PREFIX_ADVANCED_PROPERTY = PREFIX_BASE + "ROLE_GJSX:";
  37. /**
  38. * 角色详情
  39. */
  40. String PREFIX_ROLE_DETAIL = PREFIX_BASE + "ROLE_DETAIL:";
  41. /**
  42. * 角色
  43. */
  44. String PREFIX_ROLE_ACCOUNT = PREFIX_BASE + "ROLE_ACCOUNT:";
  45. /**
  46. * 角色列表
  47. */
  48. String PREFIX_ROLE_ACCOUNTS = PREFIX_BASE + "ROLE_ACCOUNTS:";
  49. /**
  50. * 角色造型
  51. */
  52. String PREFIX_ROLE_MODELING = PREFIX_BASE + "ROLE_MODELING:";
  53. /**
  54. * 角色造型列表
  55. */
  56. String PREFIX_ROLE_MODELINGS = PREFIX_BASE + "ROLE_MODELINGS";
  57. /**
  58. * 帮派
  59. */
  60. String PREFIX_GANG = PREFIX_BASE + "GANG:";
  61. /**
  62. * 帮派列表
  63. */
  64. String PREFIX_GANGS = PREFIX_BASE + "GANGS";
  65. /**
  66. * 帮派职务
  67. */
  68. String PREFIX_GANG_JOB = PREFIX_BASE + "GANG_JOB:";
  69. /**
  70. * 帮派职务列表
  71. */
  72. String PREFIX_GANG_JOBS = PREFIX_BASE + "GANG_JOBS";
  73. /**
  74. * 门派
  75. */
  76. String PREFIX_SCHOOL = PREFIX_BASE + "SCHOOL:";
  77. /**
  78. * 门派列表
  79. */
  80. String PREFIX_SCHOOLS = PREFIX_BASE + "SCHOOLS";
  81. /**
  82. * 宠物
  83. */
  84. String PREFIX_CHONGWU = PREFIX_BASE + "CHONGWU:";
  85. /**
  86. * 宠物列表
  87. */
  88. String PREFIX_CHONGWUS = PREFIX_BASE + "CHONGWUS";
  89. /**
  90. * 宠物类型
  91. */
  92. String PREFIX_CHONGWU_CATEGORY = PREFIX_BASE + "CHONGWU_CATEGORY:";
  93. /**
  94. * 宠物类型列表
  95. */
  96. String PREFIX_CHONGWU_CATEGORIES = PREFIX_BASE + "CHONGWU_CATEGORIES";
  97. /**
  98. * 大区
  99. */
  100. String PREFIX_AREA = PREFIX_BASE + "AREA:";
  101. /**
  102. * 大区列表
  103. */
  104. String PREFIX_AREAS = PREFIX_BASE + "AREAS";
  105. /**
  106. * 服务器
  107. */
  108. String PREFIX_SERVER = PREFIX_BASE + "SERVER:";
  109. /**
  110. * 服务器列表
  111. */
  112. String PREFIX_SERVERS = PREFIX_BASE + "SERVERS";
  113. /**
  114. * 账号
  115. */
  116. String PREFIX_ACCOUNT = PREFIX_BASE + "ACCOUNT:";
  117. /**
  118. * 账号列表
  119. */
  120. String PREFIX_ACCOUNTS = PREFIX_BASE + "ACCOUNTS";
  121. }

Redis缓存顶级接口

  1. package cn.edu.sgu.www.mhxysy.redis.repository;
  2. import java.util.function.Consumer;
  3. /**
  4. * redis仓库的顶级接口:为了满足开闭原则设计了此接口
  5. * @author heyunlin
  6. * @version 1.0
  7. */
  8. public interface RedisRepository {
  9. void put(String key, Object value);
  10. Object get(String key);
  11. void putList(Object value);
  12. Object getList();
  13. /**
  14. * 删除数据缓存
  15. */
  16. void delete();
  17. /**
  18. * 删除指定ID的数据缓存
  19. * @param primaryKey 数据的ID
  20. */
  21. void delete(String primaryKey);
  22. /**
  23. * 延时双删
  24. * @param primaryKey 数据的ID
  25. * @param consumer Consumer<String>
  26. */
  27. default void delayDoubleDelete(String primaryKey, Consumer<String> consumer) { }
  28. }

好了,文章就分享到这里了,后续会持续优化文章内容,感谢大家的支持~

最后,如果看完这篇文章觉得对你有所帮助或者启发,不要忘了点赞+收藏哦~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/359331?site
推荐阅读
相关标签
  

闽ICP备14008679号