苍穹外卖学习笔记(九)

embedded/2024/9/24 4:21:47/

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

一. Java中操作Redis

Redis的Java客户端

常见的几种:

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

Spring Data Redis使用方式

操作步骤:

  1. 导入Spring data Redis的Maven坐标
    pom.xml
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  1. 配置Redis数据源
    application.yml
spring:redis:host: localhostport: 6379
  1. 编写配置类,创建RedisTemplate对象
    RedisConfiguration.java
java">@Configuration
@Slf4j
public class RedisConfiguration {@Beanpublic 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 {@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void testRedisTemplate() {redisTemplate.opsForValue().set("name", "sky");System.out.println(redisTemplate.opsForValue().get("name"));}
}

完整测试代码:

java">@SpringBootTest
public class SpringDataRedisTest {@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void testRedisTemplate() {ValueOperations valueOperations = redisTemplate.opsForValue();HashOperations hashOperations = redisTemplate.opsForHash();ListOperations listOperations = redisTemplate.opsForList();SetOperations setOperations = redisTemplate.opsForSet();ZSetOperations zSetOperations = redisTemplate.opsForZSet();}/*** 测试字符串*/@Testpublic void testString() {//set get setex setnxredisTemplate.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*/@Testpublic void testHash() {//hset hget hdel hkeys hvalsHashOperations 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*/@Testpublic void testList() {//lpush rpush lpop rpop lrangeListOperations 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*/@Testpublic void testSet() {//sadd smembers sremSetOperations setOperations = redisTemplate.opsForSet();setOperations.add("set", "a", "b", "c", "d", "e");setOperations.remove("set", "a", "b");setOperations.members("set");}/*** 测试zset*/@Testpublic void testZSet() {//zadd zrange zremZSetOperations 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");}/*** 通用 */@Testpublic 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");}
}

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


http://www.ppmy.cn/embedded/115898.html

相关文章

Cilium + ebpf 系列文章-ebpf-tool(三)

本篇文章: 主要是在介绍和演示用户空间侧ebpf调试工具-ebpf-tool 一、项目地址 从 GitHub 获取 bpftool 虽然 bpftool 主要包含在 Linux 内核源代码中,但你也可以从 GitHub 上的镜像仓库中获取。 克隆 bpftool 仓库: git clone https://github.com/libbpf/bpftool.git cd bp…

基于LSTM的文本摘要生成实战教程

基于LSTM的文本摘要生成实战教程 文本摘要生成是自然语言处理&#xff08;NLP&#xff09;中的一个重要任务。其目标是将长篇文章或文档自动生成简洁的摘要&#xff0c;而保证保留原文的关键信息。近年来&#xff0c;基于深度学习的模型&#xff0c;如LSTM&#xff08;长短期记…

Springboot的三层架构

package com.wzb.ThreeLevelsExercise20240919;public class Exercise {// 内聚&#xff1a;内聚是指一个模块或内部各元素的紧密程度。高内聚则是一个模块或类中的所有功能都是紧密相关的&#xff0c;专注于完成单一任务// 高内聚的好处&#xff1a;// 1.易于维护&#xff1a;…

四十一、完成内容添加功能(使用go测试方法)

目录 一、添加model 二、完成相关dao 三、使用测试类进行测试 1、把光标防止要测试的方法上&#xff0c;右击并选择 2、自动会生成一个以dao文件加_test命名的文件 3、在其中完善方法并完成测试 四、完成content_create_handle 一、添加model 按数据库字段以及字段格式完…

力扣 LCR 020 回文子串 -Python

题目链接&#xff1a;LCR 020. 回文子串 - 力扣&#xff08;LeetCode&#xff09; 题目描述&#xff1a; 给定一个字符串 s &#xff0c;请计算这个字符串中有多少个回文子字符串。 具有不同开始位置或结束位置的子串&#xff0c;即使是由相同的字符组成&#xff0c;也会被视…

网页通知设计灵感:CSS 和 JS 的 8 大创意实现

文章目录 前言正文1.霓虹灯风格的通知系统2.垂直时间轴通知3.动画徽章通知4.项目式通知5.多种状态通知&#xff1a;成功、错误、警告6.信息、警告、提示组件7.扁平化风格通知8.社交媒体风格弹出通知 总结 前言 网页通知如今已成为电商、社交平台等网站的常见功能&#xff0c;它…

苹果为什么不做折叠屏手机?

苹果为什么不做折叠屏手机&#xff1f;折叠屏手机在最近这些年里边&#xff0c;可以说是市场的一个主要在手机上的增长点。你像华W最近推出这个三折叠手机&#xff0c;引起了整个市场的轰动。 可是&#xff0c;为什么苹果到今天为止不为所动&#xff0c;还在那不停地在现在的这…

JIT(Just-In-Time Compilation)编译器及其原理实现

什么是JIT编译器&#xff1f; JIT编译器是一种特殊的编译器&#xff0c;它在程序运行期间动态地将字节码&#xff08;Bytecode&#xff09;转换为机器码。这种转换发生在程序执行时&#xff0c;而不是在程序启动之前。JIT编译器的主要目的是提高程序的执行速度和效率&#xff…