spring保存对象到Redis的简单栗子

Redis最为优秀的NoSQL数据库,还是很有必要学习以下的。
之前那个ssm项目中用到了Redis作为缓存提高访问效率,现在开始正式学习Redis,现在是一个简单的spring保存对象到Redis的简单栗子。
pom的配置:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.8.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>2.0.6.RELEASE</version>
    </dependency>

创建一个实体类:

import java.io.Serializable;

public class Role implements Serializable {
    private static final long serialVersionUID = -5880680723667053957L;
    private long id;
    private String roleName;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
}
spring配置文件:这里value采用jdk的序列化器进行转换
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        ">
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="50"></property>
        <property name="maxTotal" value="100"></property>
        <property name="maxWaitMillis" value="20000"></property>
    </bean>
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="localhost"></property>
        <property name="port" value="6379"></property>
        <property name="poolConfig" ref="poolConfig"></property>
    </bean>
    <bean id="jdkSerializationRedisSerializer"
          class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"></property>
        <property name="keySerializer" ref="stringRedisSerializer"></property>
        <property name="valueSerializer" ref="jdkSerializationRedisSerializer"></property>
    </bean>
</beans>

测试类1:使用Spring提供的RedisTemplate操作Redis

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;


public class test {
    @Test
    public void xx(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
        Role role = new Role();
        role.setId(12);
        role.setRoleName("sd");
        redisTemplate.opsForValue().set("na1",role);
        Role role1 = (Role) redisTemplate.opsForValue().get("na1");
        System.out.print(role1.getId());
    }

}

测试类2:保证是在同一个连接池的同一个redis连接中执行,这样减少损耗

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;


public class test {
    @Test
    public void xx(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
        final Role role = new Role();
        role.setId(12);
        role.setRoleName("sd");
        SessionCallback sessionCallback = new SessionCallback() {
            @Override
            public Object execute(RedisOperations redisOperations) throws DataAccessException {
                redisOperations.boundValueOps("role2").set(role);
                return (Role)redisOperations.boundValueOps("role2").get();
            }
        };
        Role role1 = (Role) redisTemplate.execute(sessionCallback);
        System.out.print(role1.getId());
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值