玖叶教程网

前端编程开发入门

Springboot2.0学习12 使用Redis(springboot调用redis)



一、说明

spring-data-redis针对jedis提供了如下功能:

  1. 连接池自动管理,提供了一个高度封装的“RedisTemplate”类
  2. 针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
  • ValueOperations:Key-Value操作
  • SetOperations:Set类型数据操作
  • ZSetOperations:Zset类型数据操作
  • HashOperations:Map类型的数据操作
  • ListOperations:List类型的数据操作
  1. 提供了对key的“bound”(绑定)便捷化操作API,可以通过bound封装指定的key,然后进行一系列的操作而无须“显式”的再次指定Key,即BoundKeyOperations:
  • BoundValueOperations
  • BoundSetOperations
  • BoundListOperations
  • BoundSetOperations
  • BoundHashOperations
  1. 将事务操作封装,有容器控制。
  2. 针对数据的“序列化/反序列化”,提供了多种可选择策略(RedisSerializer)
  • JdkSerializationRedisSerializer:POJO对象的存取场景,使用JDK本身序列化机制,将pojo类通过ObjectInputStream/ObjectOutputStream进行序列化操作,最终redis-server中将存储字节序列。是目前最常用的序列化策略。
  • StringRedisSerializer:Key或者value为字符串的场景,根据指定的charset对数据的字节序列编码成string,是“new String(bytes, charset)”和“string.getBytes(charset)”的直接封装。是最轻量级和高效的策略。
  • JacksonJsonRedisSerializer:jackson-json工具提供了javabean与json之间的转换能力,可以将pojo实例序列化成json格式存储在redis中,也可以将json格式的数据转换成pojo实例。因为jackson工具在序列化和反序列化时,需要明确指定Class类型,因此此策略封装起来稍微复杂。【需要jackson-mapper-asl工具支持】
  • OxmSerializer:提供了将javabean与xml之间的转换能力,目前可用的三方支持包括jaxb,apache-xmlbeans;redis存储的数据将是xml工具。不过使用此策略,编程将会有些难度,而且效率最低;不建议使用。【需要spring-oxm模块的支持】

pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

application.properties


# Redis数据库索引(默认为0)
spring.redis.database=0  
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379  
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8  
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1  
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8  
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0  
# 连接超时时间(毫秒)
spring.redis.timeout=0  

测试用例

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xundh.demo.model.User;


@RequestMapping("user")
@Controller
public class UserController {

    @Resource
    private RedisTemplate<String,String> redisTemplate;
    
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    
    @RequestMapping("/test")
    public String test(){
        redisTemplate.opsForValue().set("name","myvalue");
        String name = (String)redisTemplate.opsForValue().get("name");
        System.out.println(name);
        // 删除 
        redisTemplate.delete(name);
        
        stringRedisTemplate.opsForValue().set("name", "newvalue");
        name = stringRedisTemplate.opsForValue().get("name");
        System.out.println(name);
        return "/user/test";
    }
}

resources/templates/user/test.html 空页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>test.html</title>
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
    This is my HTML page. <br />
  </body>
</html>

运行测试,控制台输出:

2020-01-25 22:01:39.445 INFO  27 com.cn.netgosmart.RedisTest - name=myvalue
2020-01-25 22:01:39.462 INFO  35 com.cn.netgosmart.RedisTest - name=newvalue

stringRedisTemplate常用操作

stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//向redis里存入数据和设置缓存时间  

stringRedisTemplate.boundValueOps("test").increment(-1);//val做-1操作  

stringRedisTemplate.opsForValue().get("test")//根据key获取缓存中的val  

stringRedisTemplate.boundValueOps("test").increment(1);//val +1  

stringRedisTemplate.getExpire("test")//根据key获取过期时间  

stringRedisTemplate.getExpire("test",TimeUnit.SECONDS)//根据key获取过期时间并换算成指定单位  

stringRedisTemplate.delete("test");//根据key删除缓存  

stringRedisTemplate.hasKey("546545");//检查key是否存在,返回boolean值  

stringRedisTemplate.opsForSet().add("red_123", "1","2","3");//向指定key中存放set集合  

stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS);//设置过期时间  

stringRedisTemplate.opsForSet().isMember("red_123", "1")//根据key查看集合中是否存在指定数据  

stringRedisTemplate.opsForSet().members("red_123");//根据key获取set集合

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言