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服务在运行,即可成功

原文地址:https://www.cnblogs.com/wangxuemei/p/9546258.html