本文介绍: 项目中,用于缓存姓名、地名、单位名称等一些较固定名称idname组件用于减少一些表的关连操作和冗余字段扩展也方便,写不同的枚举就行了。对于性能要求较高的场景可必为本地缓存。[在这里插入图片描述](https://imgblog.csdnimg.cn/direct/868531a473f241e4bf9d7d83bfb551c3.png

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

{
	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;
    }

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

    @Override
    public 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);
            }
        }
    }

    @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://imgblog.csdnimg.cn/direct/868531a473f241e4bf9d7d83bfb551c3.png

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

原文地址:https://blog.csdn.net/qq_37148232/article/details/134717358

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_18037.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注