redis整合Spring之序列化對象與反序列化

寫在最前面redis

  1.Spring必須是4.2.6及以上版本才支持redisspring

  2.jar包版本建議統一數據結構

須要準備jar包測試

  1.aopalliance-1.0.jarthis

  2.spring-data-commons-1.8.4.RELEASE.jarspa

  3.spring-data-redis-1.8.4.RELEASE.jar3d

正文code

  1.在spring配置文件中添加配置 xml

<!-- 鏈接池配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大鏈接數 -->
        <property name="maxTotal" value="30" />
        <!-- 最大空閒鏈接數 -->
        <property name="maxIdle" value="10" />
        <!-- 每次釋放鏈接的最大數目 -->
        <property name="numTestsPerEvictionRun" value="1024" />
        <!-- 釋放鏈接的掃描間隔(毫秒) -->
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        <!-- 鏈接最小空閒時間 -->
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        <!-- 鏈接空閒多久後釋放, 當空閒時間>該值 且 空閒鏈接>最大空閒鏈接數 時直接釋放 -->
        <property name="softMinEvictableIdleTimeMillis" value="10000" />
        <!-- 獲取鏈接時的最大等待毫秒數,小於零:阻塞不肯定的時間,默認-1 -->
        <property name="maxWaitMillis" value="1500" />
        <!-- 在獲取鏈接的時候檢查有效性, 默認false -->
        <property name="testOnBorrow" value="false" />
        <!-- 在空閒時檢查有效性, 默認false -->
        <property name="testWhileIdle" value="true" />
        <!-- 鏈接耗盡時是否阻塞, false報異常,ture阻塞直到超時, 默認true -->
        <property name="blockWhenExhausted" value="false" />
    </bean>

    <!-- redis單機 經過鏈接池 -->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="poolConfig" ref="jedisPoolConfig" />
        <property name="hostName" value="127.0.0.1" />
        <property name="port" value="6379" />
    </bean> 

 

  2.Spring配置文件中配置redistemplate(高亮部分的配置使得序列化對象得以實現)
    
             RedisTemplate中須要聲明4種serializer,默認爲「JdkSerializationRedisSerializer」:
                1) keySerializer :對於普通K-V操做時,key採起的序列化策略
                2) valueSerializer:value採起的序列化策略
                3) hashKeySerializer: 在hash數據結構中,hash-key的序列化策略
                4) hashValueSerializer:hash-value的序列化策略
<bean id="redisTemplate"
      class="org.springframework.data.redis.core.RedisTemplate">
       <property name="connectionFactory" ref="connectionFactory"/>
        <property name="keySerializer"> 
        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> 
               </property> 
       <property name="hashKeySerializer"> 
        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> 
       </property> 
       <property name="valueSerializer"> 
        <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> 
       </property> 
       <property name="hashValueSerializer"> 
        <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> 
       </property> 
       </bean>

  3.測試一下對象

  首先建立一個User的model類(必須繼承Serializable類纔可序列化)

public class User implements Serializable{
    private String name;
    private String age;
    private String sex;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public void print(){
        System.out.println("姓名:"+name);
        System.out.println("年齡:"+age);
        System.out.println("性別:"+sex);
    }
}

  寫一個測試類

public class TestOrder {
    @Test
    public void test(){
    ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
    RedisTemplate r = context.getBean(RedisTemplate.class);;
    User user = new User();
    user.setName("馮吉榮");
    user.setAge("22");
    user.setSex("");
    r.opsForValue().set("user_1", user);
    User user1 = (User)r.opsForValue().get("user_1");
    user1.print();
    }
}

  結果

  此次真有圖

  

  寫在最後

對象的存儲是hash,保證jar包的版本統一,同時保證redis服務在運行,便可成功

相關文章
相關標籤/搜索