redis spring 配置

1、applicationContext-jedis.xml

<?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;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值