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

@Cacheable 注解爆红(不兼容的类型。实际为 java. lang. String‘,需要 ‘boolean‘)

文章目录

  • 1、org.springframework.cache.annotation.Cacheable
  • 2、javax.persistence.Cacheable

在这里插入图片描述

    @Cacheable(value = "findPAUserById", key = "#id")public Optional<PAUser> findById(Integer id) {return paUserRepository.findById(id);}

我真的要笑死,findPAUserById 下面爆红波浪线,我以为是代码的问题,其实是注解导入的不正确,我真是醉了,我要导入的应该是 import org.springframework.cache.annotation.Cacheable;这个注解,结果导成了import javax.persistence.Cacheable;这个注解。


org.springframework.cache.annotation.Cacheablejavax.persistence.Cacheable 这两个注解的区别。

Featureorg.springframework.cache.annotation.Cacheablejavax.persistence.Cacheable
所属框架Spring Framework (Spring Cache Abstraction)Java Persistence API (JPA)
作用声明一个方法的结果可以被缓存。 Spring 会在方法调用时拦截,并检查缓存,返回缓存结果或执行方法。声明一个实体类应该被缓存。 指示 JPA 提供者启用二级缓存。
应用范围方法级别实体类级别 (@Entity)
缓存层级方法级的缓存,通常用于业务逻辑层, 可以缓存方法的返回值。实体类的二级缓存,通常在持久层, 用于缓存实体类的数据。
主要功能声明方法返回值可缓存,并指定缓存名称和键的生成规则。 使用 Spring 的缓存管理器来实际管理缓存。声明实体类可以使用二级缓存。 JPA 提供者可能会使用自己的缓存实现,例如 Ehcache 或其他缓存。
缓存生命周期由 Spring 的缓存管理器管理缓存的生命周期和失效策略。由 JPA 提供者管理二级缓存的生命周期和失效策略。
缓存类型可以搭配多种缓存提供者使用, 例如:内存缓存、Ehcache、Redis、Caffeine 等,并且可以自由切换。由 JPA 提供者决定使用的二级缓存类型,通常与 JPA 提供者相关。
配置方式通过 Spring Boot 的 application.propertiesapplication.yml 配置,以及 Spring 缓存注解 (@EnableCaching, @CacheManager, @CacheConfig等)通过 JPA 提供者的配置文件 (persistence.xml 或其他 JPA 实现的配置) 或者注解,例如 shared-cache-mode
缓存控制@CacheEvict, @CachePut, @Caching 等 Spring 缓存注解可以更精细地控制缓存的更新和删除。通常使用 JPA 的 API 或者配置来管理二级缓存。 可以使用 JPA 的注解 @Cache 或者配置文件中指定缓存模式。
适用场景适用于需要缓存方法结果,以提高性能的应用场景, 例如读取数据,计算结果等。适用于需要缓存实体数据以减少数据库访问的场景。 例如,频繁读取的实体数据。
使用方式@Cacheable(value = "cacheName", key = "#id") 标记一个可以缓存返回值的方法。@Cacheable 标记一个实体可以使用二级缓存。
关键属性value: 指定缓存名称。 key: 指定缓存键,通常使用 SpEL。condition: 表示缓存的条件。 unless: 表示不缓存的条件。通常只使用@Cacheable, 有时会配合 shared-cache-mode使用, 用来指定缓存模式。
是否需要缓存管理器必须 使用 Spring 缓存管理器 (CacheManager) 来管理实际的缓存。通常需要配置 JPA 提供者的二级缓存配置。
例子java @Cacheable(value = "userCache", key = "#id") public User getUserById(int id) { ... }java @Entity @Cacheable public class User { ... }

总结

  • org.springframework.cache.annotation.Cacheable: 用于方法级别的缓存,由 Spring 缓存抽象管理,重点是提高方法调用的性能。
  • javax.persistence.Cacheable: 用于实体级别的缓存,由 JPA 提供者管理,重点是减少数据库的访问,提高数据查询性能。

简而言之, 前者是 Spring Cache 的,用于缓存方法的返回值;后者是 JPA 的,用于缓存实体类数据。 两者从应用范围,使用场景,管理方式上都不同, 不要混淆。

1、org.springframework.cache.annotation.Cacheable

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package org.springframework.cache.annotation;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Cacheable {@AliasFor("cacheNames")String[] value() default {};@AliasFor("value")String[] cacheNames() default {};String key() default "";String keyGenerator() default "";String cacheManager() default "";String cacheResolver() default "";String condition() default "";String unless() default "";boolean sync() default false;
}

2、javax.persistence.Cacheable

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package javax.persistence;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Cacheable {boolean value() default true;
}
http://www.lryc.cn/news/514438.html

相关文章:

  • java相互加密解密
  • PostgreSQL中FIRST_VALUE、LAST_VALUE、LAG 和 LEAD是窗口函数,允许返回在数据集的特定窗口(或分区)内访问行的相对位置
  • 树莓派之旅-第一天 系统的烧录和设置
  • 数据库工程师进阶秘籍:云计算基础知识题目精选与答案(附PDF)
  • 【HAProxy】如何在Ubuntu下配置HAProxy服务器
  • C#编写的盘符图标修改器 - 开源研究系列文章
  • (四)配置有线网口、SSH登陆、文件传输以及运行交叉编译程序测试
  • 离线的方式:往Maven的本地仓库里安装依赖
  • 《深入浅出HTTPS​​​​​​​​​​​​​​​​​》读书笔记(22):密钥协商算法
  • kubernetes学习-Service
  • Springcloud项目-前后端联调(一)
  • 洛谷P1525 [NOIP2010 提高组] 关押罪犯(种子并查集基础)
  • 【算法刷题指南】模拟
  • 学习笔记078——Java Properties类使用详解
  • 若依使用 Undertow 替代 Tomcat 容器
  • 多输入多输出 | Matlab实现WOA-CNN鲸鱼算法优化卷积神经网络多输入多输出预测
  • Elasticsearch:基础概念
  • Spring MVC的@ResponseBody与@RequestBody
  • 智能商业分析 Quick BI
  • LUA基础语法
  • SpringBoot的pom.xml文件中,scope标签有几种配置?
  • Leetcode729: 我的日程安排表 I
  • 青少年编程与数学 02-006 前端开发框架VUE 02课题、创建工程
  • Redis的生态系统和社区支持
  • Tomcat解析
  • UML之组合与聚合
  • 数据结构理论篇(期末突击)
  • 《一文读懂PyTorch核心模块:开启深度学习之旅》
  • 摆脱Zotero存储限制:WebDAV结合内网穿透打造个人文献管理云平台
  • Flutter封装一个三方ViewPager学习