1、applicationContext-jedis.xml
2、cn.focus.dc.util.RedisUtil 类
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="masterJedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="10.16.24.2"/>
<property name="port" value="1046"/>
<property name="password" value="**************"/>
<property name="usePool" value="true"/>
</bean>
<bean id="slaveJedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="10.16.24.1"/>
<property name="port" value="1050"/>
<property name="password" value="*************"/>
<property name="usePool" value="true"/>
</bean>
<bean id="masterRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="masterJedisConnectionFactory"/>
</bean>
<bean id="slaveRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="slaveJedisConnectionFactory"/>
</bean>
<bean id="redisUtil" class="cn.focus.dc.util.RedisUtil">
<constructor-arg index="0" type="org.springframework.data.redis.core.RedisTemplate" ref="masterRedisTemplate" >
</constructor-arg>
<constructor-arg index="1" type="org.springframework.data.redis.core.RedisTemplate" ref="slaveRedisTemplate" >
</constructor-arg>
</bean>
</beans>
2、cn.focus.dc.util.RedisUtil 类
/**
* @author qiaowang
*/
public class RedisUtil {
private RedisTemplate<String, String> masterTemplate;
private RedisTemplate<String, String> slaveTemplate;
//设置默认属性,可以通过set方法进行注册,同时是public保证可以进行函数注入
public RedisUtil() {
}
public RedisUtil(RedisTemplate<String, String> masterTemplate, RedisTemplate<String, String> slaveTemplate) {
this.masterTemplate = masterTemplate;
this.slaveTemplate = slaveTemplate;
}
public void set(String key, String value) {
masterTemplate.opsForValue().set(key, value);
}
public String getValue(String key) {
return slaveTemplate.opsForValue().get(key);
}
public void delete(String key) {
masterTemplate.opsForValue().getOperations().delete(key);
}
public void setMasterTemplate(RedisTemplate<String, String> masterTemplate) {
this.masterTemplate = masterTemplate;
}
public void setSlaveTemplate(RedisTemplate<String, String> slaveTemplate) {
this.slaveTemplate = slaveTemplate;
}
}