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

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

具体用法:

java 复制代码
{
	NameCacheUser.USER.getName(userId);
	NameCacheUser.ACCOUNT.getName(accountId);
	NameCacheUser.OFFICE.getName(officeId);
}
java 复制代码
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;
    }

    @Override
    public String prefix() {
        return this.name().toLowerCase();
    }

    @Override
    public Function<String, String> nameFunction() {
        return this.nameFunction;
    }
}
java 复制代码
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);
    }
}
java 复制代码
@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);
            }
        }
    }

    @StringRedisTemplate
    public 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


相关推荐
该用户已不存在1 小时前
OpenJDK、Temurin、GraalVM...到底该装哪个?
java·后端
TT哇2 小时前
@[TOC](计算机是如何⼯作的) JavaEE==网站开发
java·redis·java-ee
Tina学编程2 小时前
48Days-Day19 | ISBN号,kotori和迷宫,矩阵最长递增路径
java·算法
青川入梦2 小时前
MyBatis极速通关上篇:Spring Boot环境搭建+用户管理实战
java·开发语言·mybatis
执子手 吹散苍茫茫烟波2 小时前
leetcode415. 字符串相加
java·leetcode·字符串
小韩博2 小时前
网络安全(Java语言)脚本 汇总(二)
java·安全·web安全
萤丰信息2 小时前
技术赋能安全:智慧工地构建城市建设新防线
java·大数据·开发语言·人工智能·智慧城市·智慧工地
带刺的坐椅3 小时前
Java MCP 的鉴权?好简单的啦
java·鉴权·mcp·solon-ai
Pocker_Spades_A3 小时前
飞算JavaAI家庭记账系统:从收支记录到财务分析的全流程管理方案
java·开发语言
33255_40857_280593 小时前
掌握分页艺术:MyBatis与MyBatis-Plus实战指南(10年Java亲授)
java·mybatis