spring中添加redis缓存
1.单机版的添加
spring里面配置
<bean id="redisClient" class="redis.clients.jedis.JedisPool">
<!-- 构造方法 -->
<constructor-arg name="host" value="192.168.1.191"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
</bean>
@Test
public void testSpringDedisSingle()
{
//初始化xml
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
JedisPool pool=(JedisPool) applicationContext.getBean("redisClient");
Jedis jedis=pool.getResource();
String s= jedis.get("key1");
System.out.println(s);
jedis.close();
pool.close();
}
**************************************************************************************
2.集群版
<!-- jedis集群版配置 --> <!-- jedis集群版配置 -->
<bean id="redisClient" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.1.191"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.1.191"></constructor-arg>
<constructor-arg name="port" value="7002"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.1.191"></constructor-arg>
<constructor-arg name="port" value="7003"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.1.191"></constructor-arg>
<constructor-arg name="port" value="7004"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.1.191"></constructor-arg>
<constructor-arg name="port" value="7005"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.1.191"></constructor-arg>
<constructor-arg name="port" value="7006"></constructor-arg>
</bean>
</set>
</constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
测试
@Test
public void testSpringDedisJiQun()
{
//初始化xml
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
JedisCluster jedisCluster=(JedisCluster) applicationContext.getBean("redisClient");
String s= jedisCluster.get("chb1");
System.out.println(s);
jedisCluster.close();
}