o2o手机网站建设难,wordpress get_post_meta,企业网站模板 首页大图,网站需要多少钱项目中#xff0c;用于缓存姓名、地名、单位名称等一些较固定名称的id-name小组件。用于减少一些表的关连操作和冗余字段。优化代码结构。扩展也方便#xff0c;写不同的枚举就行了。 具体用法#xff1a;
{NameCacheUser.USER.getName(userId);NameCacheUser.ACCOUNT.getN…项目中用于缓存姓名、地名、单位名称等一些较固定名称的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 FunctionString, String nameFunction;NameCacheUser(FunctionString, String nameFunction) {this.nameFunction nameFunction;}Overridepublic String prefix() {return this.name().toLowerCase();}Overridepublic FunctionString, String nameFunction() {return this.nameFunction;}
}public interface NameCacheBee {String prefix();FunctionString, 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(ListString keys){NameCache.remove(prefix(), keys);}
}Component
public class NameCache {public final static String CONSTANT_NAME_CACHE constant_name_cache_;private static RedisTemplateString, 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,ListString keys){if(ZYListUtils.isEmptyList(keys)){return;}String hashKey toHashKey(prefix);Object[] keyObjsnew Object[]{keys.size()};for (int i0;ikeyObjs.length;i){keyObjs[i]keys.get(i);}redisTemplate.opsForHash().delete(hashKey,keyObjs);}public static String getName(String key, String prefix, FunctionString, 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, FunctionString, 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(RedisTemplateString, 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