当前位置: 首页 > news >正文

【Redis】Java Spring操作redis

目录

  • 引入Redis依赖
  • StringRedisTemplate
  • 使用String
  • 使用List
  • 使用Set
  • 使用hash
  • 使用zset

引入Redis依赖

在这里插入图片描述
在这里插入图片描述

StringRedisTemplate

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
此处RedisTemplate是把这些操作Redis的方法,分成了几个类别,分门别类的来组织的。
此处提供的一些接口风格,和原生的Redis命令就有一定的差异。

使用String

 @GetMapping("/testString")
    @ResponseBody
    public String testString() {
        redisTemplate.execute((RedisConnection connection) -> {
            // execute 要求回调方法中必须 return 语句,返回个东西
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForValue().set("key", "111");
        redisTemplate.opsForValue().set("key2", "222");
        redisTemplate.opsForValue().set("key3", "333");

        String value = redisTemplate.opsForValue().get("key");
        System.out.println("value:" + value);

        return "OK";
    }

在这里插入图片描述

使用List

@GetMapping("/testList")
    @ResponseBody
    public String testList() {
        // 先清除之前的数据.
        redisTemplate.execute((RedisConnection connection) -> {
            // execute 要求回调方法中必须 return 语句,返回个东西
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForList().leftPush("key", "111");
        redisTemplate.opsForList().leftPush("key", "222");
        redisTemplate.opsForList().leftPush("key", "333");

        String value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value:" + value);
        value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value:" + value);
        value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value:" + value);

        return "OK";
    }

在这里插入图片描述

使用Set

 @GetMapping("/testSet")
    @ResponseBody
    public String testSet() {
        redisTemplate.execute((RedisConnection connection) -> {
            // execute 要求回调方法中必须 return 语句,返回个东西
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForSet().add("key", "111", "222", "333");
        Set<String> result = redisTemplate.opsForSet().members("key");
        System.out.println("result:" + result);

        Boolean exists = redisTemplate.opsForSet().isMember("key", "111");
        System.out.println("exists:" + exists);

        Long count = redisTemplate.opsForSet().size("key");
        System.out.println("count:" + count);

        redisTemplate.opsForSet().remove("key", "111", "222");
        result = redisTemplate.opsForSet().members("key");
        System.out.println("result:" + result);

        return "OK";
    }

在这里插入图片描述

使用hash

@GetMapping("/testHash")
    @ResponseBody
    public String testHash() {
        redisTemplate.execute((RedisConnection connection) -> {
            // execute 要求回调方法中必须 return 语句,返回个东西
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForHash().put("key", "f1", "111");
        redisTemplate.opsForHash().put("key", "f2", "222");
        redisTemplate.opsForHash().put("key", "f3", "333");

        String value = (String) redisTemplate.opsForHash().get("key", "f1");
        System.out.println("value:" + value);

        Boolean exists = redisTemplate.opsForHash().hasKey("key", "f1");
        System.out.println("exists:" + exists);

        redisTemplate.opsForHash().delete("key", "f1", "f2");

        Long size = redisTemplate.opsForHash().size("key");
        System.out.println("size:" + size);

        value = (String) redisTemplate.opsForHash().get("key", "f1");
        System.out.println("value:" + value);

        return "OK";
    }

在这里插入图片描述

使用zset

@GetMapping("/testZSet")
    @ResponseBody
    public String testZSet() {
        redisTemplate.execute((RedisConnection connection) -> {
            // execute 要求回调方法中必须 return 语句,返回个东西
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForZSet().add("key", "zhangsan", 50.0);
        redisTemplate.opsForZSet().add("key", "lisi", 80.0);
        redisTemplate.opsForZSet().add("key", "wangwu", 100.0);

        Set<String> members = redisTemplate.opsForZSet().range("key", 0, -1);
        System.out.println("members:" + members);

        Set<ZSetOperations.TypedTuple<String>> membersWithScore = redisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
        System.out.println("membersWithScore:" + membersWithScore);

        Double score = redisTemplate.opsForZSet().score("key", "zhangsan");
        System.out.println("score:" + score);

        redisTemplate.opsForZSet().remove("key", "zhangsan");

        Long size = redisTemplate.opsForZSet().size("key");
        System.out.println("size:" + size);

        Long rank = redisTemplate.opsForZSet().rank("key", "lisi");
        System.out.println("rank:" + rank);

        return "OK";
    }

在这里插入图片描述

http://www.dtcms.com/a/2560.html

相关文章:

  • 2024上海国际智慧城市展览会(世亚智博会)智慧城市,数字中国
  • Python数据挖掘:入门、进阶与实用案例分析——自动售货机销售数据分析与应用
  • 淘宝天猫商品评论数据接口,淘宝天猫商品评论API接口,淘宝API
  • 【Rust基础①】基本类型、所有权与借用、复合类型
  • 【Vivado HLS Bug】Ubuntu环境下Vivado HLS导出IP报错:HLS ERROR: [IMPL 213-28]
  • Ant Design Vue Element-ui 中table 合并行功能,以及带分页并合并行
  • 基于SSM的学生选课管理系统
  • ideal远程Debug部署在服务器上的服务详解
  • STC89C51基础及项目第15天:小车测速、添加语言识别控制
  • 学习嵌入式系统的推荐步骤:
  • 怎么团队合作,协作开发
  • 【C++】如何使用RapidXML读取和创建XML文件
  • 百度Apollo自动驾驶
  • 【算法|前缀和系列No.2】牛客网 DP35 【模板】二维前缀和
  • QCustomPlot实现曲线拖拽
  • 浅谈大数据之Flink
  • python:从Excel或者CSV中读取因变量与多个自变量,用于训练机器学习回归模型,并输出预测结果
  • 管理系统搭建一般步骤(会话跟踪 路由导航守卫 响应拦截器)
  • NoVNC(Client)+TigerVNC(Server)搭建流程
  • 上位机在自动化中有何作用和优势?
  • Adobe发布Firefly 2,提升图像质量和用户体验
  • Android---Android 是如何通过 Activity 进行交互的
  • 使用解构赋值简化axios返回对象属性元素的提取
  • root赋权
  • nodejs+vue水浒鉴赏平台系统
  • linux下文件存储系统(inode/目录项/硬链接)
  • MySQLJDBC入门与SQL注入
  • 至强服务器BIOS/UEFI驱动开发笔记
  • 【MySQL】表的查询与连接
  • C# 图解教程 第5版 —— 第5章 类的基本概念