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

苍穹外卖--缓存菜品Spring Cache

Spring Cache是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能。

Spring Cache提供了一层抽象,底层可以切换不同的缓存实现,例如:

①EHCache

②Caffeine

③Redis

常用注解:

代码开发:

package com.itheima.controller;import com.itheima.entity.User;
import com.itheima.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {@Autowiredprivate UserMapper userMapper;@PostMapping@CachePut(cacheNames = "userCache",key="#user.id")//如果使用Spring Cache缓存数据,key的生成:userCache::1public User save(@RequestBody User user){userMapper.insert(user);return user;}@DeleteMapping@CacheEvict(cacheNames = "userCache",key = "#id")public void deleteById(Long id){userMapper.deleteById(id);}@DeleteMapping("/delAll")@CacheEvict(cacheNames = "userCache",allEntries = true)public void deleteAll(){userMapper.deleteAll();}@GetMapping@Cacheable(cacheNames = "userCache",key = "#id")//key的生成:userCache::10public User getById(Long id){User user = userMapper.getById(id);return user;}}

http://www.xdnf.cn/news/14315.html

相关文章:

  • 在docker中部署mysql
  • 论文略读: LAYERWISE RECURRENT ROUTER FOR MIXTURE-OF-EXPERTS
  • 实现回显服务器(基于UDP)
  • 【Linux系列】HEIC文件类型
  • 使用 vscode 开发 uni-app 项目时如何解决 manifest.json 文件注释报错的问题
  • 学习设计模式《十三》——迭代器模式
  • uniapp打包安卓和ios
  • 前端资源帖
  • JUC核心解析系列(二)——显式锁深度解析
  • Flink 与 Hive 深度集成
  • Qt .pro配置gcc相关命令(三):-W1、-L、-rpath和-rpath-link
  • 删除链表的倒数第N个结点
  • 系统架构设计师 2
  • 音频水印——PerTh Watermarker
  • 《Attention Is All You Need》解读
  • 佛山SAP本地化代理商和实施公司推荐,哪家更专业?
  • Emacs定制:文件管理dired
  • 为应对激烈竞争环境,IBMS系统如何提升企业管理效率
  • [Java恶补day24] 整理模板·考点三【二分查找】
  • 2.1话题发布
  • 探索Agent的发展潜力:大模型与具身智能的融合
  • 浅谈拼写纠错
  • 调用AkShare获取A股股票基本信息
  • 布局和约束相关的生命周期(layoutIfNeeded,updateConstraintsIfNeeded)
  • iOS安全和逆向系列教程 第19篇:ARM64汇编语言基础与逆向分析
  • 使用清华大学的 Hugging Face 镜像
  • PTP时间同步实战测试clock master/slave 直连校时+PPS测试
  • 第8章——8天Python从入门到精通【itheima】-88~90-Python的文件操作(文件的写出+文件的追加+综合案例)
  • 强化学习 PPO
  • GRUB2 启动配置的工作原理与优先级规则详解