版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。
首先在applicationContext.xml中引入redisTemplate
classpath*:*.properties
然后写一个redis.property去保存对应的配置
然后写一个读取配置的文件
import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import java.util.HashMap; import java.util.Map; import java.util.Properties; public class PropertiesUtils extends PropertyPlaceholderConfigurer { //所有属性文件的键值对 private static MapctxPropertiesMap; /** * 添加所有属性到map中*@see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#processProperties(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, java.util.Properties) */ @Override protected synchronized void processProperties( ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); if (null==ctxPropertiesMap) {	ctxPropertiesMap = new HashMap(); } for (Object key : props.keySet()) { String keyStr = key.toString(); String value = props.getProperty(keyStr); ctxPropertiesMap.put(keyStr, value); } } public static String getContextProperty(String name) { return null == ctxPropertiesMap || ctxPropertiesMap.get(name) == null ? "" : ctxPropertiesMap.get(name).toString(); } public static String getContextProperty(String name, String defaultValue) { return null == ctxPropertiesMap || ctxPropertiesMap.get(name) == null ? defaultValue : ctxPropertiesMap.get(name).toString(); } public static Integer getInt(String name) { String value = getContextProperty(name); if ((value + "").matches("-?\\d+")) { return Integer.parseInt(value); } else { return null; } } /** * @param name 属性名 */ public static Integer getInt(String name, Integer defaultValue) { String value = getContextProperty(name); if ((value + "").matches("-?\\d+")) { return Integer.parseInt(value); } else { return defaultValue; } } }
首先定义一个接口
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple; import java.io.Serializable; import java.util.List; import java.util.Set; public interface CacheService { /** * key * * @param key * @return 获取过期时间 */ public Long ttl(final String key); /** * 生成缓存 key * * @param namespace eg : carModle:{carFamily}:{id}:year * @param keys eg: audi,123 * @return eg: carModle:audi:123