苍穹外卖学习笔记(九)

由于前面已经有系统学习Redis的文章,这里不再详细书写Redis的入门知识(数据结构、常用命令)

一. Java中操作Redis

Redis的Java客户端

常见的几种:

  1. Jedis
  2. Lettuce
  3. Spring Data Redis(建议)

Spring Data Redis使用方式

操作步骤:

  1. 导入Spring data Redis的Maven坐标
    pom.xml
xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  1. 配置Redis数据源
    application.yml
yml 复制代码
spring:
    redis:
        host: localhost
        port: 6379
  1. 编写配置类,创建RedisTemplate对象
    RedisConfiguration.java
java 复制代码
@Configuration
@Slf4j
public class RedisConfiguration {

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        log.info("开始创建Redis模板对象.....");
        //设置Redis工厂对象
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置key的序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //设置value的序列化器
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}
  1. 通过RedisTemplate操作Redis
    SpringDataRedisTest.java
java 复制代码
@SpringBootTest
public class SpringDataRedisTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testRedisTemplate() {
        redisTemplate.opsForValue().set("name", "sky");
        System.out.println(redisTemplate.opsForValue().get("name"));
    }
}

完整测试代码:

java 复制代码
@SpringBootTest
public class SpringDataRedisTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testRedisTemplate() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        HashOperations hashOperations = redisTemplate.opsForHash();
        ListOperations listOperations = redisTemplate.opsForList();
        SetOperations setOperations = redisTemplate.opsForSet();
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
    }

    /**
     * 测试字符串
     */
    @Test
    public void testString() {
        //set get setex setnx
        redisTemplate.opsForValue().set("city", "beijing");
        String city = (String) redisTemplate.opsForValue().get("city");
        System.out.println(city);
        redisTemplate.opsForValue().set("code", "1234", 3, TimeUnit.MINUTES);
        redisTemplate.opsForValue().setIfAbsent("lock", "1");
        redisTemplate.opsForValue().setIfAbsent("lock", "2");
    }

    /**
     * 测试hash
     */
    @Test
    public void testHash() {
        //hset hget hdel hkeys hvals
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put("100", "name", "tom");
        hashOperations.put("100", "age", "20");
        String name = (String) hashOperations.get("100", "name");
        System.out.println(name);
        hashOperations.keys("100"); //[name, age]
        hashOperations.values("100");//[tom, 20]
        hashOperations.delete("100", "age");
    }

    /**
     * 测试list
     */
    @Test
    public void testList() {
        //lpush rpush lpop rpop lrange
        ListOperations listOperations = redisTemplate.opsForList();
        listOperations.leftPush("list", "a");
        listOperations.leftPush("list", "b");
        listOperations.leftPush("list", "c");
        listOperations.rightPush("list", "d");
        listOperations.rightPush("list", "e");
        listOperations.rightPush("list", "f");
        String value = (String) listOperations.leftPop("list");
        System.out.println(value);
        listOperations.range("list", 0, -1);
    }
    
    /**
     * 测试set
     */
    @Test
    public void testSet() {
        //sadd smembers srem
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add("set", "a", "b", "c", "d", "e");
        setOperations.remove("set", "a", "b");
        setOperations.members("set");
    }
    
    /**
     * 测试zset
     */
    @Test
    public void testZSet() {
        //zadd zrange zrem
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.add("zset", "a", 1);
        zSetOperations.add("zset", "b", 2);
        zSetOperations.add("zset", "c", 3);
        zSetOperations.add("zset", "d", 4);
        zSetOperations.add("zset", "e", 5);
        zSetOperations.range("zset", 0, -1);
        zSetOperations.remove("zset", "a", "b");
    }

    /**
     * 通用 
     */
    @Test
    public void testCommon() {
        Set keys = redisTemplate.keys("*");
        System.out.println(keys);

        Boolean city = redisTemplate.hasKey("city");
        Boolean code = redisTemplate.hasKey("code");

        for (Object key : keys) {
            DataType type = redisTemplate.type(key);
            System.out.println(type.name());
        }
        redisTemplate.delete("lock");
    }
}

不用了记得注释,不然影响启动速度

相关推荐
新手小袁_J5 分钟前
JDK11下载安装和配置超详细过程
java·spring cloud·jdk·maven·mybatis·jdk11
呆呆小雅6 分钟前
C#关键字volatile
java·redis·c#
Monly217 分钟前
Java(若依):修改Tomcat的版本
java·开发语言·tomcat
Ttang239 分钟前
Tomcat原理(6)——tomcat完整实现
java·tomcat
goTsHgo10 分钟前
在 Spring Boot 的 MVC 框架中 路径匹配的实现 详解
spring boot·后端·mvc
钱多多_qdd20 分钟前
spring cache源码解析(四)——从@EnableCaching开始来阅读源码
java·spring boot·spring
waicsdn_haha22 分钟前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
飞的肖30 分钟前
前端使用 Element Plus架构vue3.0实现图片拖拉拽,后等比压缩,上传到Spring Boot后端
前端·spring boot·架构
Q_192849990632 分钟前
基于Spring Boot的摄影器材租赁回收系统
java·spring boot·后端
Code_流苏34 分钟前
VSCode搭建Java开发环境 2024保姆级安装教程(Java环境搭建+VSCode安装+运行测试+背景图设置)
java·ide·vscode·搭建·java开发环境