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

【spring boot】在Java中操作缓存:

文章目录

        • 一、Jedis
        • 二、Spring Data Redis(常用)
            • 【1】pom.xml
            • 【2】application.yml
            • 【3】RedisConfig
            • 【4】RuiJiWaiMaiApplicationTests
        • 三、Spring Cache
          • 【1】常用注解:
          • 【2】使用案例
          • 【3】底层不使用redis,重启服务,内存丢失=>解决:
            • pom.xml
            • application.yml
            • 启动类:
            • Result:
            • 注解使用:


一、Jedis

在这里插入图片描述

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version>
</dependency>
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.8.0</version>
</dependency>

在这里插入图片描述

二、Spring Data Redis(常用)

【1】pom.xml
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
【2】application.yml

在这里插入图片描述

spring:redis:    #redis配置host: localhostport: 6379#password:database: 0   #默认提供16个数据库,0:0号数据库jedis:pool:  #redis连接池配置max-active: 8   #最大连接数max-idle: 4     #最大空闲连接max-wait: 1ms   #最大阻塞等待时间min-idle: 0     #最小空闲连接

在这里插入图片描述

【3】RedisConfig
package com.example.ruijiwaimai.config;import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig extends CachingConfigurerSupport {@Beanpublic RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory){RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<>();// 默认的key序列化器为:JdkSerializationRedisSerializerredisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setConnectionFactory(connectionFactory);return redisTemplate;}
}
【4】RuiJiWaiMaiApplicationTests
package com.example.ruijiwaimai;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;@SpringBootTest
class RuiJiWaiMaiApplicationTests {@Autowiredprivate RedisTemplate redisTemplate;/** 操作String类型数据=>ValueOperations:简单K-V操作* */@Testpublic void testString() {redisTemplate.opsForValue().set("city123", "shenzhen");    //  \xac\xed\x00\x05t\x00\x04city做了序列化,无法用get city获取=》config配置RedisConfigString value = (String) redisTemplate.opsForValue().get("city123");System.out.println(value);redisTemplate.opsForValue().set("key1", "value1", 10, TimeUnit.SECONDS);Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("city1234", "nanjing");System.out.println(aBoolean);}/** 操作Hash类型数据=>HashOperations:针对map类型的数据操作* */@Testpublic void testHash() {HashOperations hashOperations = redisTemplate.opsForHash();hashOperations.put("002", "name", "xiaoming");hashOperations.put("002", "age", "20");String age = (String) hashOperations.get("002", "age");System.out.println(age);// 获取所有字段Set keys = hashOperations.keys("002");for (Object key : keys) {System.out.println(key);}// 获取所有值List values = hashOperations.values("002");for (Object value : values) {System.out.println(value);}}/** 操作List类型数据=>ListOperations:针对list类型的数据操作* */@Testpublic void testList() {ListOperations listOperations = redisTemplate.opsForList();//存值listOperations.leftPush("mylist", "a");listOperations.leftPushAll("mylist", "b", "c", "d");//取值List<String> mylist = listOperations.range("mylist", 0, -1);for (String value : mylist) {System.out.println(value);}//获得列表长度Long size = listOperations.size("mylist");int lSize = size.intValue();for (int i = 0; i < lSize; i++) {//出队列Object element = listOperations.rightPop("mylist");System.out.println("出队列:" + element);}}/** 操作Set(无序集合)类型数据=>SetOperations:set类型数据操作* */@Testpublic void testSet() {SetOperations setOperations = redisTemplate.opsForSet();//存值setOperations.add("myset", "a", "b", "c", "a");//取值Set<String> myset = setOperations.members("myset");for (String o : myset) {System.out.println(o);}//删除成员setOperations.remove("myset", "a", "b");myset = setOperations.members("myset");for (String o : myset) {System.out.println("删除后的数据:" + o);}}/** 操作ZSet(有序集合)类型数据=>ZSetOperations:zset类型数据操作* */@Testpublic void testZSet() {ZSetOperations zSetOperations = redisTemplate.opsForZSet();//存值zSetOperations.add("myZset", "a", 10.0);zSetOperations.add("myZset", "b", 11.0);zSetOperations.add("myZset", "c", 12.0);zSetOperations.add("myZset", "a", 13.0);//取值Set<String> myZet = zSetOperations.range("myZset", 0, -1);for (String s : myZet) {System.out.println(s);}//修改分数zSetOperations.incrementScore("myZset", "b", 20.0);myZet = zSetOperations.range("myZset", 0, -1);for (String s : myZet) {System.out.println("修改分数: " + s);}//删除成员zSetOperations.remove("myZset", "a", "b");myZet = zSetOperations.range("myZset", 0, -1);for (String s : myZet) {System.out.println("删除后的成员: " + s);}}/** 通用操作* */@Testpublic void testCommon() {//获取redis中所有的keySet keys = redisTemplate.keys("*");for (Object key : keys) {System.out.println(key);}//判断某个key是否存在Boolean itcast = redisTemplate.hasKey("itcast");System.out.println("判断某个key是否存在:"+itcast);//删除指定keyredisTemplate.delete("myZset");//获取指定key对应的value的数据类型DataType dataType = redisTemplate.type("001");System.out.println("获取指定key对应的value的数据类型:"+dataType.name());}
}

在这里插入图片描述

三、Spring Cache

【1】常用注解:
注解说明
@EnableCaching开启缓存注解功能
@Cacheable判断是否有缓存数据,有=》返回缓存数据;没有=》放到缓存中
@CachePut将方法的返回值放到缓存中
@CacheEvict将一条或多条数据从缓存中删除
【2】使用案例
package com.itheima.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.entity.User;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {@Autowiredprivate CacheManager cacheManager;@Autowiredprivate UserService userService;/*** CachePut:将方法返回值放入缓存* value:缓存的名称,每个缓存名称下面可以有多个key* key:缓存的key*/@CachePut(value = "userCache",key = "#user.id")@PostMappingpublic User save(User user){userService.save(user);return user;}/*** CacheEvict:清理指定缓存* value:缓存的名称,每个缓存名称下面可以有多个key* key:缓存的key*/@CacheEvict(value = "userCache",key = "#p0")//@CacheEvict(value = "userCache",key = "#root.args[0]")//@CacheEvict(value = "userCache",key = "#id")@DeleteMapping("/{id}")public void delete(@PathVariable Long id){userService.removeById(id);}//@CacheEvict(value = "userCache",key = "#p0.id")//@CacheEvict(value = "userCache",key = "#user.id")//@CacheEvict(value = "userCache",key = "#root.args[0].id")@CacheEvict(value = "userCache",key = "#result.id")@PutMappingpublic User update(User user){userService.updateById(user);return user;}/*** Cacheable:在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中* value:缓存的名称,每个缓存名称下面可以有多个key* key:缓存的key* condition:条件,满足条件时才缓存数据* unless:满足条件则不缓存*/@Cacheable(value = "userCache",key = "#id",unless = "#result == null")@GetMapping("/{id}")public User getById(@PathVariable Long id){User user = userService.getById(id);return user;}@Cacheable(value = "userCache",key = "#user.id + '_' + #user.name")@GetMapping("/list")public List<User> list(User user){LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(user.getId() != null,User::getId,user.getId());queryWrapper.eq(user.getName() != null,User::getName,user.getName());List<User> list = userService.list(queryWrapper);return list;}
}
【3】底层不使用redis,重启服务,内存丢失=>解决:
pom.xml
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
</dependency>
application.yml

在这里插入图片描述

  redis:    #redis配置host: 192.168.139.128port: 6379#password:database: 0   #默认提供16个数据库,0;0号数据库cache:redis:time-to-live: 1800000 #设置缓存数据的过期时间
启动类:

在这里插入图片描述

Result:

在这里插入图片描述

注解使用:
    @PostMapping()@CacheEvict(value = "setmealCache",allEntries = true)public Result<String> save(@RequestBody SetmealDto setmealDto) {log.info("套餐信息:{}", setmealDto);setmealService.saveWithDish(setmealDto);return null;}//删除套餐@DeleteMapping()@CacheEvict(value = "setmealCache",allEntries = true)//清除setmealCache下的所有缓存public Result<String> delete(@RequestParam List<Long> ids){setmealService.removeWithDish(ids);return Result.success("套餐数据删除成功");}/*** 根据条件查询套餐数据* @param setmeal* @return*/@GetMapping("/list")@Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")public Result<List<Setmeal>> list(Setmeal setmeal){LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());queryWrapper.orderByDesc(Setmeal::getUpdateTime);List<Setmeal> list = setmealService.list(queryWrapper);return Result.success(list);}
http://www.lryc.cn/news/43385.html

相关文章:

  • 擂台赛-安全攻防之使用openssh后门获取root密码实战
  • 关于React入门基础从哪学起?
  • python玄阶斗技--tkinter库
  • 【MySQL】MySQL的介绍MySQL数据库及MySQL表的基本操作
  • 【每日随笔】社会上层与中层的博弈 ( 技术无关、没事别点进来看 | 社会上层 | 上层与中层的保护层 | 推荐学习的知识 )
  • Cookie 和 Session的区别
  • leetcode使用vscode调试C++代码
  • 树莓派Linux源码配置,树莓派Linux内核编译,树莓派Linux内核更换
  • 【C语言】深度讲解 atoi函数 使用方法与模拟实现
  • HTTPS的加密流程
  • STM32配置读取BMP280气压传感器数据
  • 【Linux】 基础IO——文件(中)
  • 蓝桥杯刷题冲刺 | 倒计时13天
  • MIPI D-PHYv2.5笔记(5) -- 不同的PHY配置方式
  • 【周末闲谈】文心一言,模仿还是超越?
  • 《一“企”谈》 | 「佛山市政」:携手企企通,让采购业务数智化
  • Spark运行架构
  • 基于卷积神经网络CNN的水果分类预测,卷积神经网络水果等级识别
  • Spring Boot 框架总结
  • 【数据结构】第五站:带头双向循环链表
  • Springboot生成二维码
  • “独裁者”何小鹏,再造小鹏汽车
  • 数据结构 | 泛型 | 擦除机制| 泛型的上界
  • C++拷贝构造函数(复制构造函数)详解
  • python学习——多线程
  • SAP 系统中过账码or记账码
  • 【FreeRTOS(一)】FreeRTOS新手入门——初识FreeRTOS
  • Python中 __init__的通俗解释是什么?
  • 网友真实面试总结出的自动化测试面试题库
  • 2023 年最佳 C++ IDE