网站建设实训课实训心得,网站建设公众号小程序推广开发,中国建设集团门户网,四川在建项目信息查询SpringBoot 操作 Redis 数据简介 Redis 是一个开源的NoSQL数据库#xff0c;基于内存的键值存储#xff0c;速度快。Redis 支持数据结构#xff0c;如字符串#xff0c;散列#xff0c;列表#xff0c;集和带范围查询的有序集。5种主要数据类型#xff1a;字符串类型 … SpringBoot 操作 Redis 数据简介 Redis 是一个开源的NoSQL数据库基于内存的键值存储速度快。Redis 支持数据结构如字符串散列列表集和带范围查询的有序集。5种主要数据类型字符串类型 string散列类型 hash列表类型 list集合类型 set有序集合类型 zsetRedis优缺点 直接基于内存读写不用Redis直接用MySQL先不说查询性能耗时一个是直达一个是通过媒介显而易见Redis 速度很快 。不过Redis 仅适用于键值对并不能替代MySQL虽然其有持久化但是也可能会崩溃损失几秒的数据项目环境 项目工具环境IDE工具这里是 Jetbrains IDEAMavenJDK1.8Redis 服务器源码环境地址 https://github.com/Gleans/SpringBootLearn/tree/master/springboot-redis后面的基于这个项目来操作定义 RedisTemplate RedisConfig.javaimport org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.GenericToStringSerializer;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;Configurationpublic class RedisConfig { Bean JedisConnectionFactory jedisConnectionFactory() { JedisConnectionFactory jedisConnectionFactory new JedisConnectionFactory(); jedisConnectionFactory.setPassword(ekko1234); return jedisConnectionFactory; } Bean public RedisTemplate redisTemplate() { final RedisTemplate redisTemplate new RedisTemplate(); RedisSerializer stringSerializer new StringRedisSerializer(); RedisSerializer jsonString new GenericToStringSerializer(Object.class); redisTemplate.setConnectionFactory(jedisConnectionFactory()); redisTemplate.setKeySerializer(stringSerializer); redisTemplate.setValueSerializer(jsonString); redisTemplate.setHashKeySerializer(stringSerializer); redisTemplate.setHashValueSerializer(jsonString);return redisTemplate; }}操作 Redis 初体验 Spring Boot 的开箱即用的特点集成 Redis 也是显而易见在test环境新建测试类 TestRedis.javaimport lombok.extern.slf4j.Slf4j;import org.junit.FixMethodOrder;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.MethodSorters;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;//根据测试方法名字搞定执行顺序Slf4jFixMethodOrder(MethodSorters.NAME_ASCENDING)SpringBootTestRunWith(SpringJUnit4ClassRunner.class)public class TestRedis { private RedisTemplate redisTemplate;Autowiredpublic void setRedisTemplate(RedisTemplate redisTemplate) {this.redisTemplate redisTemplate; }// 做下面的操作}操作字符串在测试类中操作字符串 Test public void operateStr(){ // 存入 key为username value 为 admin redisTemplate.opsForValue().set(username,admin); // 获取 key为username String username (String) redisTemplate.opsForValue().get(username); System.out.println(username); }执行过后发现存在redis中的数据操作集合opsForList() Test public void operateList() { List userList new ArrayList(); userList.add(张三); userList.add(李四);//循环向userlist左添加值 userList.forEach(value - redisTemplate.opsForList().leftPush(userlist, value));//向userlist右添加值 redisTemplate.opsForList().rightPush(userlist, 麻子); log.info(删除前:userlist-{}, redisTemplate.opsForList().range(userlist, 0, 10));/* 三个参数 - key redis中存 key值 - count 从左或是从右删除正左负右 - value 就是需要从list移除的值 */ redisTemplate.opsForList().remove(userlist, 0, 麻子); log.info(删除后:userlist-{}, redisTemplate.opsForList().range(userlist, 0, 10)); }输出删除前:userlist-[李四, 张三, 麻子]删除后:userlist-[李四, 张三]操作不可重复集合opsForSet() Test public void operateSet(){ List trap new ArrayList(); trap.add(工具人); trap.add(工具人); trap.add(工具人); trap.add(四块五的妞); trap.add(十元妹子); System.out.print(trap.toString());//循环向userlist左添加值 trap.forEach(value-redisTemplate.opsForSet().add(userSet,value)); log.info(删除前:userSet-{},redisTemplate.opsForSet().members(userSet));// 直接根据set的key值删除 redisTemplate.opsForSet().remove(userSet,工具人); log.info(删除后:userSet-{},redisTemplate.opsForSet().members(userSet)); }输出删除前:userSet-[工具人, 工具人, 工具人, 四块五的妞, 十元妹子]删除后:userSet-[四块五的妞, 十元妹子]哈希操作opsForHash() 相当于在操作实体类 Test public void operateHash(){ //添加 redisTemplate.opsForHash().put(user,username,ekko); redisTemplate.opsForHash().put(user,address,Shanghai); redisTemplate.opsForHash().put(user,passwd,1234); //修改 redisTemplate.opsForHash().put(user,address,Beijing); //删除 redisTemplate.opsForHash().delete(user,passwd); }结果总结 数据量大且不长变的还是用缓存接收数据来回操作使用 Redis持久化时再入库做好缓存击穿的准备利用好 Redis 可以很大程度的减少 MySQL 的压力Redis 常用的操作基本满足需求小知识 方法名字前加ABCD...是为了让方法有执行顺序根据测试方法名字搞定执行顺序在方法上加注解FixMethodOrder(MethodSorters.NAME_ASCENDING)日志注解Slf4j是为了让日志书写更方便 //之前写日志 log.info(输出aindexb); //现在 log.info(输出a{}b,index);多个参数可以用多个{},总之喜欢哪个用哪个谢谢阅读原文可评论或与作者交流