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

寫在最前面

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

  2.jar包版本建議統一

需要準備jar包

  1.aopalliance-1.0.jar

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

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

正文

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

<!-- 連接池配置 -->
    <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服務在運行,即可成功

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章