php做的网站后台,龙岗网站建设报价,网站建设与管理考查方案,教育机构退费法律规定Spring Cache
从3.1开始#xff0c;Spring引入了对Cache的支持。其使用方法和原理都类似于Spring对事务管理的支持。Spring Cache是作用在方法上的
使用Spring Cache的时候我们要保证我们缓存的方法对于相同的方法参数要有相同的返回结果。 使用Spring Cache需要我们做两方面…Spring Cache
从3.1开始Spring引入了对Cache的支持。其使用方法和原理都类似于Spring对事务管理的支持。Spring Cache是作用在方法上的
使用Spring Cache的时候我们要保证我们缓存的方法对于相同的方法参数要有相同的返回结果。 使用Spring Cache需要我们做两方面的事 1. 声明某些方法使用缓存 2. 配置Spring对Cache的支持 和Spring对事务管理的支持一样Spring对Cache的支持也有基于注解和基于XML配置两种方式。 EnableCaching 开启注解缓存 Cacheable 可以标记在一个方法上也可以标记在一个类上。 当标记在一个方法上时表示该方法是支持缓存的当标记在一个类上时则表示该类所有的方法都是支持缓存的。 Cacheable可以指定三个属性value、key和condition。 参数 解释 example value 缓存的名称在 spring 配置文件中定义必须指定至少一个 例如: Cacheable(value”mycache”) Cacheable(value{”cache1”,”cache2”} key 缓存的 key可以为空如果指定要按照 Spring的·EL 表达式编写如果不指定则缺省按照方法的所有参数进行组合 Cacheable(value”testcache”,key”#userName”) condition 缓存的条件可以为空使用 Spring的EL 编写返回 true 或者 false只有为 true 才进行缓存 Cacheable(value”testcache”,condition”#userName.length()2”) CachePut Cacheable标注的方法Spring在每次执行前都会检查Cache中是否存在相同key的缓存元素如果存在就不再执行该方法而是直接从缓存中获取结果进行返回否则才会执行并将返回结果存入指定的缓存中。 CachePut也可以声明一个方法支持缓存功能。与Cacheable不同的是使用CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果而是每次都会执行该方法并将执行结果以键值对的形式存入指定的缓存中。 CacheEvict CacheEvict是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。 CacheEvict可以指定的属性有value、key、condition、allEntries和beforeInvocation。其中value、key和condition的语义与Cacheable对应的属性类似。即value表示清除操作是发生在哪些Cache上的对应Cache的名称key表示需要清除的是哪个key如未指定则会使用默认策略生成的keycondition表示清除操作发生的条件 allEntries属性 allEntries是boolean类型表示是否需要清除缓存中的所有元素。 beforeInvocation属性 清除操作默认是在对应方法成功执行之后触发的即方法如果因为抛出异常而未能成功返回时也不会触发清除操作。使用beforeInvocation可以改变触发清除操作的时间当我们指定该属性值为true时Spring会在调用该方法之前清除缓存中的指定元素。 项目结构(spring bootmybatis plus) 依赖加载 dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependencydependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactIdversion2.11.1/version/dependencydependencygroupIdredis.clients/groupIdartifactIdjedis/artifactId/dependencydependencygroupIdcom.mysql/groupIdartifactIdmysql-connector-j/artifactId/dependency
!-- mp 自动生成--dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.5.3/version/dependency
!-- 模板--dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-generator/artifactIdversion3.5.3/version/dependencydependencygroupIdorg.apache.velocity/groupIdartifactIdvelocity-engine-core/artifactIdversion2.3/version/dependencydependencygroupIdorg.freemarker/groupIdartifactIdfreemarker/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build
spring boot配置文件(.yml)
spring:redis:host: 192.168.253.16database: 0password: rootport: 6379lettuce:pool:max-active: 20max-wait: -1max-idle: 5min-idle: 0timeout: 1800000datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql:///prac?serverTimezoneUTCuseUnicodetruecharacterEncodingutf-8username: rootpassword: rootjackson:date-format: yyyy-MM-dd HH:mm:sstime-zone: GMT8serialization:write-date-keys-as-timestamps: falsemybatis-plus:configuration:map-underscore-to-camel-case: truelog-impl: org.apache.ibatis.logging.stdout.StdOutImplmapper-locations: classpath:/mapper/*.xml#逻辑删除 删除之前的值是0 之后是1global-config:db-config:logic-not-delete-value: 1logic-delete-value: 0type-aliases-package: com.tes.entityservice redis缓存数据