首先在applicationContext.xml中引入redisTemplate
<bean id="propertyConfigurer" class="PropertiesUtils">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath*:*.properties</value>
</list>
</property>
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.max_total}" />
<property name="minIdle" value="${redis.min_idle}" />
<property name="maxIdle" value="${redis.max_idle}" />
<property name="maxWaitMillis" value="${redis.max_wait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="testOnReturn" value="${redis.testOnReturn}" />
<property name="testWhileIdle" value="${redis.testWhileIdle}" />
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="jedisPoolConfig" />
<property name="hostName" value="${redis.ip}" />
<property name="port" value="${redis.port}" />
<property name="database" value="${redis.db.index}" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
然后写一个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 Map<String, Object> ctxPropertiesMap;
/**
* 添加所有属性到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<String, Object>();
}
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:year
*/
public String generateCacheKey(String namespace, Object... keys);
public Boolean existsKey(String key);
/**
* 将对象添加到缓存中
*
* @param key 缓存的 key 值
* @param value 对象
*/
public void putIntoCache(final String key, final Object value);
/**
* 将对象添加到缓存中
*
* @param key 缓存的 key 值
* @param value 对象
* @param time 过期时间(多久之后过期) 单位为秒
*/
public void putIntoCache(final String key, final Object value, final long time);
/**
* 清除缓存
*
* @param key 缓存的 key 值
*/
public void clearCache(final String key);
/**
* 从缓存中获得对象
*
* @param key 缓存的 key
* @param clazz 对象的类型
* @return 缓存的 value
*/
public <T> T getFromCache(final String key, final Class<T> clazz);
/**
* 从缓存中获取字符串
* @param key
* @return
*/
String getStringFromCache(final String key);
public <T> T getMapFromCache(final String key, final Class<T> clazz);
public <T> T getObjectFromCache(final String key,final Class<T> clazz);
/**
* 根据 key ,从缓存中获取 list
*
* @param key 缓存的 key
* @param clazz 对象的类型
*/
public <T> List<T> getListFromCache(final String key, final Class<T> clazz);
/**
* 从缓存中批量获取 对象的值
*
* @param keys 缓存 key 的 list
* @param clazz 对象的类型
* @return
*/
public <T> List<T> getListFromCache(final List<String> keys,
final Class<T> clazz);
/**
* 自增 key
*
* @param key
* @return 自增之后的 id
*/
public Long incr(final String key);
/**
* 自增 key
*
* @param key
* @return 自增之后的 id
*/
public Long incr(final String key, final Long expireTime);
/**
* hashSet put操作
*
* @param hashName hash hash表的名字
* @param filedName key 值
* @param value key 对应的 value值
* @param expireTime 超时时间
* @return
*/
Boolean hset(String hashName, String filedName, Object value,
long expireTime);
/**
* hashSet 操作
*
* @param hashName hash hash表的名字
* @param filedName key 值
* @param value key 对应的value值
* @return
*/
Boolean hset(String hashName, String filedName, Object value);
/**
* hashGet hash get 操作
*
* @param hashName hash 表的名字
* @param filedName key 值
* @return 对应的 value 的值
*/
Object hget(String hashName, String filedName);
/**
* hashGet hash getAll 操作
*
* @param hashName hash 表的名
* @param filedName key 值
* @return 对应的 value 的值
*/
<T> List<T> hgetall(String hashName, final Class<T> clazz);
/**
* hashGet hash haskey 操作