spring整合redis及びRedis Templateを使用する方法


必要なjarバッグ
spring-data-reedis-1.6.22.RELEASE.jar
jedis-2.7.2.jar(依存comons-pool 2-2.3.jar)
commons-pool 2-2.3.jar
spring-redis.xmlプロファイル

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/util 
   http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<!--[redis-JedisPoolConfig  ](http://blog.csdn.net/liang_love_java/article/details/50510753)-->
<!--  jedis-2.7.2.jar   jar  commons-pool2-2.3.jar 
    jedis   commons-pool2-2.3.jar           。
            http://blog.csdn.net/liang_love_java/article/details/50510753
-->
  <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
    <property name="maxIdle" value="1" /> 
    <property name="maxTotal" value="5" /> 
    <property name="blockWhenExhausted" value="true" /> 
    <property name="maxWaitMillis" value="30000" /> 
    <property name="testOnBorrow" value="true" /> 
  </bean> 

  <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 
    <property name="hostName" value="10.1.8.200" /> 
    <property name="port" value="6379"/> 
    <property name="poolConfig" ref="jedisPoolConfig" /> 
    <property name="usePool" value="true"/> 
  </bean> 

  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
    <property name="connectionFactory"  ref="jedisConnectionFactory" />  
    <property name="keySerializer">  
      <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
    </property>   
    <property name="valueSerializer">  
      <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
    </property>  
    <property name="hashKeySerializer">   
      <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>   
    </property>  
    <property name="hashValueSerializer">  
      <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>   
    </property> 
   </bean> 

</beans>

テストコード

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

public static void main(String[] args) {
    ClassPathXmlApplicationContext appCtx = new ClassPathXmlApplicationContext("spring-redis.xml");
    final RedisTemplate<String, Object> redisTemplate = appCtx.getBean("redisTemplate",RedisTemplate.class);
    //     key 
    ValueOperations<String, Object> value = redisTemplate.opsForValue();
    value.set("lp", "hello word");
    //      key   
    System.out.println(value.get("lp"));
    //      hash  
    HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
    Map<String,Object> map = new HashMap<String,Object>();
    map.put("name", "lp");
    map.put("age", "26");
    hash.putAll("lpMap", map);
    //   map
    System.out.println(hash.entries("lpMap"));
    //      list   
    ListOperations<String, Object> list = redisTemplate.opsForList();
    list.rightPush("lpList", "lp");
    list.rightPush("lpList", "26");
    //   list
    System.out.println(list.range("lpList", 0, 1));
    //      set   
    SetOperations<String, Object> set = redisTemplate.opsForSet();
    set.add("lpSet", "lp");
    set.add("lpSet", "26");
    set.add("lpSet", "178cm");
    //   set   
    System.out.println(set.members("lpSet"));
    //      set   
    ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
    zset.add("lpZset", "lp", 0);
    zset.add("lpZset", "26", 1);
    zset.add("lpZset", "178cm", 2);
    //     set   
    System.out.println(zset.rangeByScore("lpZset", 0, 2));
  }

以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。