当前位置: 首页 > news >正文

用于缓存一些固定名称的小组件

项目中,用于缓存姓名、地名、单位名称等一些较固定名称的id-name小组件。用于减少一些表的关连操作和冗余字段。优化代码结构。扩展也方便,写不同的枚举就行了。
具体用法:

{NameCacheUser.USER.getName(userId);NameCacheUser.ACCOUNT.getName(accountId);NameCacheUser.OFFICE.getName(officeId);
}
public enum NameCacheUser implements NameCacheBee {USER(userId -> {UserMapper userMapper = ZYSpringUtils.getBean(UserMapper.class);User user = userMapper.selectById(userId);return null != user ? user.getUserName() : null;}),ACCOUNT(accountId -> {UserAccountMapper userMapper = ZYSpringUtils.getBean(UserAccountMapper.class);UserAccount user = userMapper.selectById(accountId);return null != user ? user.getUserName() : null;}),OFFICE(officeId -> {OfficeMapper officeMapper = ZYSpringUtils.getBean(OfficeMapper.class);Office office = officeMapper.selectById(officeId);return null != office ? office.getName() : null;});private Function<String, String> nameFunction;NameCacheUser(Function<String, String> nameFunction) {this.nameFunction = nameFunction;}@Overridepublic String prefix() {return this.name().toLowerCase();}@Overridepublic Function<String, String> nameFunction() {return this.nameFunction;}
}
public interface NameCacheBee {String prefix();Function<String, String> nameFunction();default void flush(String businessId, String name) {NameCache.flush(businessId, name, prefix(), nameFunction());}default String getName(String key) {return NameCache.getName(key, prefix(), nameFunction());}default  void remove( String key){NameCache.remove(prefix(), key);}default   void remove(List<String> keys){NameCache.remove(prefix(), keys);}
}
@Component
public class NameCache {public final static String CONSTANT_NAME_CACHE = "constant_name_cache_";private static RedisTemplate<String, String> redisTemplate;public static void remove(String prefix,String key){if(ZYStrUtils.isNotNull(key)){remove(prefix,Collections.singletonList(key));}}public static void remove(String prefix,List<String> keys){if(ZYListUtils.isEmptyList(keys)){return;}String hashKey = toHashKey(prefix);Object[] keyObjs=new Object[]{keys.size()};for (int i=0;i<keyObjs.length;i++){keyObjs[i]=keys.get(i);}redisTemplate.opsForHash().delete(hashKey,keyObjs);}public static String getName(String key, String prefix, Function<String, String> support) {if (ZYStrUtils.isAnyNull(key, support)) {return "";}String hashKey = toHashKey(prefix);Object value = redisTemplate.opsForHash().get(hashKey, key);if (ZYStrUtils.isNotNull(value)) {return String.valueOf(value);}String name = support.apply(key);if (ZYStrUtils.isNotNull(name)) {redisTemplate.opsForHash().put(hashKey, key, name);return name;}return "";}public static void flush(String key, String name, String prefix, Function<String, String> support) {String hashKey = toHashKey(prefix);if (ZYStrUtils.isNotNull(name)) {redisTemplate.opsForHash().put(hashKey, key, name);} else {String findName = support.apply(key);if (ZYStrUtils.isNotNull(findName)) {redisTemplate.opsForHash().put(hashKey, key, findName);}}}@StringRedisTemplatepublic void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {NameCache.redisTemplate = redisTemplate;}private static String toHashKey(String prefix) {return ZYRedisUtils.wrapperKey(CONSTANT_NAME_CACHE + prefix).toLowerCase();}}

在缓存中的效果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/868531a473f241e4bf9d7d83bfb551c3.png

在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/254602.html

相关文章:

  • Python 读取电子发票PDF 转成Excel
  • 我的项目问题
  • 【c】杨辉三角
  • 算法刷题之数组篇
  • TR转发路由器测评—云企业网实现跨地域跨VPC的网络互通测评实战【阿里云产品测评】
  • 1.1美术理论基础
  • 【Java 基础】21 多线程同步与锁
  • Python语言基础知识(一)
  • Xilinx FPGA平台DDR3设计详解(三):DDR3 介绍
  • 字典的遍历
  • Linux环境下的MySQL安装
  • 梦想与魔法:编程之路的挑战与荣耀
  • qt 5.15.2 主窗体菜单工具栏树控件功能
  • Day15——File类与IO流
  • 【Qt】QLineEdit显示输入十六进制,位数不足时按照规则填充显示及每两个字符以空格填充
  • GPT 中文提示词技巧:参照 OpenAI 官方教程
  • 原生微信小程序将字符串生成二维码图片
  • 深入理解HTTPS加密协议
  • 路径规划之PRM算法
  • 深入理解数据在内存中是如何存储的,位移操作符如何使用(能看懂文字就能明白系列)文章超长,慢慢品尝
  • ArcGIS提示当前许可不支持影像服务器
  • Android P 9.0 增加以太网静态IP功能
  • Android12之MediaCodec硬编解码调试手段(四十九)
  • 2.Ansible的copy模块,我最常用的模块
  • python程序将部分文件复制到指定目录
  • 5分钟教你利用服务器,打造1个 7*24H直播的直播间
  • 卡通渲染总结《二》
  • 严蔚敏数据结构p17(2.19)——p18(2.24) (c语言代码实现)
  • 0007Java程序设计-ssm基于微信小程序的在线考试系统
  • php 使用多线程