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

苍穹外卖学习笔记(九)

由于前面已经有系统学习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
@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
@SpringBootTest
public class SpringDataRedisTest {@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void testRedisTemplate() {redisTemplate.opsForValue().set("name", "sky");System.out.println(redisTemplate.opsForValue().get("name"));}
}

完整测试代码:

@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.mrgr.cn/news/34637.html

相关文章:

  • HTML之表单学习记录
  • 校园二手交易网站毕业设计基于SpringBootSSM框架
  • 「QT」几何数据类 之 QVector2D 二维向量类
  • 【MySQL】explain之type类型
  • LeetCode【0026】删除有序数组中的重复项
  • Greenplum 可观测最佳实践
  • 微信抢红包设计
  • Vue开发前端图片上传给java后端
  • 19_Python中的上下文管理器
  • QT中添加资源文件(一看就会)
  • Linux常用命令
  • 开关频率与谐振频率对应的模态图
  • 一行命令,一分钟轻松搞定SSL证书自动续期
  • 类中的特殊内容
  • 华为高级交换技术笔记 2024-2025
  • 计算机组成原理==初识二进制运算
  • 研究生三年概括
  • Elasticsearch案例
  • Python中的“打开与关闭文件”:从入门到精通
  • X86架构(八)——32位处理器架构
  • 意得辑(Editage)润色全网最低折扣
  • JIT(Just-In-Time Compilation)编译器及其原理实现
  • 浮动静态路由
  • Java开发中那些可以提升性能/效率的小技巧(持续更新)
  • [leetcode刷题]面试经典150题之6轮转数字(简单)
  • 文件服务器FastDFS 消息队列中间件RabbitMQ