map实现缓存
测试结果
数据库表
ConfigPool.java
package com.alphajuns.ssm.util; import com.alphajuns.ssm.pojo.Config; import com.alphajuns.ssm.service.ConfigService; import java.util.HashMap; import java.util.List; import java.util.Map; public class ConfigPool { /** * 参数表的缓存map */ private static AbstractTimeCachedObject cachedConfigPool = new AbstractTimeCachedObject( AbstractTimeCachedObject.CONFIG_CACHEDTIME) { @Override public Object refreshCachedObject(Object params) { return new HashMap<String, Config>(); } }; /** * * <p> * Description: 得到缓存中的对象 * </p> * * @param enforce boolean * @return Map Map */ @SuppressWarnings("unchecked") public static Map<String, Config> getCachedConfigPool(boolean enforce) { return (Map<String, Config>) cachedConfigPool.getCachedObject(null, enforce); } /** * * <p> * Description: 得到参数 * </p> * * @param configKey String * @return Config Config */ public static Config getGroupConfig(String group,String configKey) { Map<String, Config> configMap; configMap = getCachedConfigPool(false); System.out.println("获取到的map:" + System.identityHashCode(configMap)); Config config; config = configMap.get(group+"."+configKey); if (EmptyUtil.isEmpty(config)) { ConfigService configService = null; configService = (ConfigService) com.alphajuns.ssm.util.SpringContextUtil.getBean("configServiceImpl"); Map<String,String> map =new HashMap<String,String>(); map.put("configGroup", group); map.put("configKey", configKey); config = configService.queryConfigByGroupKey(map); configMap.put(group+"."+configKey, config); } return config; } /** * 通过key得到值 * @param configKey * @return */ public static String getConfigValue(String configKey){ String value = ""; Config config = getGroupConfig(null,configKey); if(config!=null){ value = config.getConfigValue(); } return value; } /** * 通过group,key得到值 * @param configKey * @return */ public static String getConfigValue(String group, String configKey){ String value = ""; Config config = getGroupConfig(group,configKey); if(config!=null){ value = config.getConfigValue(); } return value; } public static List<Map<String,?>> getConfigsByGroup(String group){ ConfigService configService = null; configService = (ConfigService) com.alphajuns.ssm.util.SpringContextUtil.getBean("configServiceImpl"); Map<String,Object> queryConfigMap = new HashMap<String,Object>(); queryConfigMap.put("configGroup", group); List<Map<String,?>> configList = configService.queryConfigListByGroup(queryConfigMap); return configList; } }
AbstractTimeCachedObject.java
package com.alphajuns.ssm.util; public abstract class AbstractTimeCachedObject { /** * */ private static final long MILLS_PER_MINTUE = 60 * 1000; /** * CONFIG_CACHEDTIME */ public static final int CONFIG_CACHEDTIME = 30; /** * */ protected long cachedMinutes = 5L; /** * */ private long expiredTimeSlot = 0; /** * */ private Object cachedObject = null; /** * <p>Description: AbstractTimeCachedObject</p> */ public AbstractTimeCachedObject() { } /** * <p>Description: AbstractTimeCachedObject</p> * @param minutes minutes */ public AbstractTimeCachedObject(int minutes) { this.cachedMinutes = minutes; } public final boolean isExpired() { return (System.currentTimeMillis() > this.expiredTimeSlot); } /** * <p>Description: getCachedObject</p> * @param params params * @param enforce enforce * @return Object */ public final synchronized Object getCachedObject(Object params, boolean enforce) { if (enforce || isExpired()) { setCachedObject(refreshCachedObject(params)); } return this.cachedObject; } /** * <p>Description: getCachedObject</p> * @param params params * @return Object */ public final Object getCachedObject(Object params) { return getCachedObject(params, false); } /** * <p>Description: setCachedObject</p> * @param cachedObject cachedObject */ public final void setCachedObject(Object cachedObject) { this.cachedObject = cachedObject; this.expiredTimeSlot = System.currentTimeMillis() + this.cachedMinutes * MILLS_PER_MINTUE; } /** * <p>Description: refreshCachedObject</p> * @param params params * @return Object */ public abstract Object refreshCachedObject(Object params); }
Config.java
package com.alphajuns.ssm.pojo; public class Config { private String configGroup; /** * 参数键 */ private String configKey; /** * 参数值 */ private String configValue; /** * 描述 */ private String description; /** * * <p> * 总数: TODO * </p> * */ private int total; public String getConfigKey() { return this.configKey; } public void setConfigKey(String configKey) { this.configKey = configKey; } public String getConfigValue() { return this.configValue; } public void setConfigValue(String configValue) { this.configValue = configValue; } public String getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } public int getTotal() { return this.total; } public void setTotal(int total) { this.total = total; } public String getConfigGroup() { return configGroup; } public void setConfigGroup(String configGroup) { this.configGroup = configGroup; } public String toString(){ return "group:"+this.configGroup+",key:"+this.configKey+",value:"+this.configValue; } }
ConfigService.java
package com.alphajuns.ssm.service; import com.alphajuns.ssm.pojo.Config; import java.util.List; import java.util.Map; public interface ConfigService { /** * search key * @param configKey * @return */ public Config queryConfigByKey(String configKey); /** * 通过group 和key搜索 * @param queryConfigMap * @return */ public Config queryConfigByGroupKey(Map<String,?> queryConfigMap); /** * queryConfigListByGroup * @param queryConfigMap * @return */ public List<Map<String,?>> queryConfigListByGroup(Map<String,?> queryConfigMap); /** * * @Description 查询二期 * @author fanj * @date 2019年11月20日 * @param queryConfigMap * @return */ public List<Map<String,?>> queryTpmeConfigListByGroup(Map<String,?> queryConfigMap); }
ConfigServiceImpl.java
package com.alphajuns.ssm.service.impl; import com.alphajuns.ssm.mybatis.ConfigMapper; import com.alphajuns.ssm.pojo.Config; import com.alphajuns.ssm.service.ConfigService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; @Service public class ConfigServiceImpl implements ConfigService { @Autowired private ConfigMapper configMapper; @Override public Config queryConfigByKey(String configKey) { // TODO Auto-generated method stub return configMapper.queryConfigByKey(configKey); } public Config queryConfigByGroupKey(Map<String,?> queryConfigMap){ return configMapper.queryConfigByGroupKey(queryConfigMap); } public List<Map<String,?>> queryConfigListByGroup(Map<String,?> queryConfigMap){ return configMapper.queryConfigListByGroup(queryConfigMap); } @Override public List<Map<String, ?>> queryTpmeConfigListByGroup(Map<String, ?> queryConfigMap) { return configMapper.queryTpmeConfigListByGroup(queryConfigMap); } }
ConfigMapper.java
package com.alphajuns.ssm.mybatis; import com.alphajuns.ssm.pojo.Config; import org.apache.ibatis.annotations.Param; import java.util.List; import java.util.Map; public interface ConfigMapper { public abstract Config queryConfigByKey(@Param("configKey") String configKey); public abstract Config queryConfigByGroupKey(Map<String,?> queryConfigMap); public abstract List<Map<String,?>> queryConfigListByGroup(Map<String,?> queryConfigMap); public abstract List<Map<String, ?>> queryTpmeConfigListByGroup(Map<String, ?> queryConfigMap); }
ConfigMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.alphajuns.ssm.mybatis.ConfigMapper" > <resultMap id="BaseResultMap" type="com.alphajuns.ssm.pojo.Config" > <id column="config_key" property="configKey" jdbcType="VARCHAR" /> <result column="config_group" property="configGroup" jdbcType="VARCHAR" /> <result column="config_value" property="configValue" jdbcType="VARCHAR" /> <result column="description" property="description" jdbcType="VARCHAR" /> </resultMap> <select id="queryConfigByKey" resultMap="BaseResultMap" parameterType="java.lang.String" > select T.config_key, T.config_value, T.description from CONFIG T where t.config_key = #{configKey,jdbcType=VARCHAR} </select> <select id="queryConfigByGroupKey" resultMap="BaseResultMap" parameterType="map" > select T.config_key, T.config_value, T.config_group, T.description from CONFIG T where t.config_key = #{configKey,jdbcType=VARCHAR} <choose> <when test="configGroup!=null and configGroup!=''"> and T.config_group=#{configGroup,jdbcType=VARCHAR} </when> <otherwise> and t.config_group is null or t.config_group='' </otherwise> </choose> </select> <select id="queryConfigListByGroup" resultType="map" parameterType="java.lang.String" > select T.config_group, T.config_key, T.config_value, T.description from CONFIG T where t.config_group = #{configGroup,jdbcType=VARCHAR} </select> <select id="queryTpmeConfigListByGroup" resultType="map" parameterType="java.lang.String" > select T.config_group, T.config_key, T.config_value, T.description <!-- 这里查询配置,拼接一个config_key、config_value返回 fanj 20191120 --> , (T.config_value||T.config_key) AS CONFIG_VALUE_AND_CONFIG_KEY from CONFIG T where t.config_group = #{configGroup,jdbcType=VARCHAR} </select> </mapper>