网站建设数据处理,wordpress添加返回顶部,j2ee 做网站,手机wap网页1. Spring Cache介绍
Spring Cache提供了一组注解#xff0c;使开发者能够轻松地在方法上定义缓存行为
Spring Cache抽象了缓存的底层实现#xff0c;允许开发者选择使用不同的缓存提供者#xff08;如 Ehcache、Redis、Caffeine 等#xff09;。通过配置相应的缓存管理器…1. Spring Cache介绍
Spring Cache提供了一组注解使开发者能够轻松地在方法上定义缓存行为
Spring Cache抽象了缓存的底层实现允许开发者选择使用不同的缓存提供者如 Ehcache、Redis、Caffeine 等。通过配置相应的缓存管理器可以方便地切换底层缓存实现而无需改变应用代码
Spring Cache的maven坐标
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactIdversion3.1.5/version
/dependencySpring Cache的常用注解
注解说明EnableCaching开启缓存注解功能通常加在启动类上Cacheable在方法执行前先查询缓存中是否有数据如果有数据则直接返回缓存数据如果没有缓存数据调用方法并将方法返回值放到缓存中CachePut将方法的返回值放到缓存中CacheEvict将一条或多条数据从缓存中删除
2. Spring Cache入门案例
CacheDemoApplication.java
package com.itheima;import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;Slf4j
SpringBootApplication
EnableCaching
public class CacheDemoApplication {public static void main(String[] args) {SpringApplication.run(CacheDemoApplication.class, args);log.info(项目启动成功...);}
}WebMvcConfiguration.java
Configuration
Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {/*** 生成接口文档配置** return*/Beanpublic Docket docket() {log.info(准备生成接口文档...);ApiInfo apiInfo new ApiInfoBuilder().title(接口文档).version(2.0).description(接口文档).build();Docket docket new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo).select()//指定生成接口需要扫描的包.apis(RequestHandlerSelectors.basePackage(com.itheima.controller)).paths(PathSelectors.any()).build();return docket;}/*** 设置静态资源映射** param registry*/protected void addResourceHandlers(ResourceHandlerRegistry registry) {log.info(开始设置静态资源映射...);registry.addResourceHandler(/doc.html).addResourceLocations(classpath:/META-INF/resources/);registry.addResourceHandler(/webjars/**).addResourceLocations(classpath:/META-INF/resources/webjars/);}
}User.java
Data
public class User implements Serializable {private static final long serialVersionUID 1L;private Long id;private String name;private int age;
}UserController.java
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;PostMappingCachePut(cacheNames userCache, key #user.id)// CachePut(cacheNames userCache, key #result.id)// CachePut(cacheNames userCache, key #p0.id)// CachePut(cacheNames userCache, key #a0.id)// CachePut(cacheNames userCache, key #root.args[0].id)public User save(RequestBody User user){userMapper.insert(user);return user;}Cacheable(cacheNames userCache, key #id)GetMappingpublic User getById(Long id){User user userMapper.getById(id);return user;}DeleteMappingCacheEvict(cacheNames userCache, key #id)public void deleteById(Long id){userMapper.deleteById(id);}DeleteMapping(/delAll)CacheEvict(cacheNames userCache, allEntries true)public void deleteAll(){userMapper.deleteAll();}
}UserMapper.java
Mapper
public interface UserMapper {Insert(insert into user(name,age) values (#{name}, #{age}))Options(useGeneratedKeys true, keyProperty id)void insert(User user);Delete(delete from user where id #{id})void deleteById(Long id);Delete(delete from user)void deleteAll();Select(select * from user where id #{id})User getById(Long id);
}application.yml
server:port: 8888
spring:datasource:druid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:13306/spring_cache_demo?serverTimezoneAsia/ShanghaiuseUnicodetruecharacterEncodingutf-8zeroDateTimeBehaviorconvertToNulluseSSLfalseallowPublicKeyRetrievaltrueusername: rootpassword: rootredis:host: localhostport: 6379password: 123456database: 1
logging:level:com:itheima:mapper: debugservice: infocontroller: infopom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.7.3/versionrelativePath//parentgroupIdcom.itheima/groupIdartifactIdspringcache-demo/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source11/maven.compiler.sourcemaven.compiler.target11/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdscopecompile/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.20/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.76/version/dependencydependencygroupIdcommons-lang/groupIdartifactIdcommons-lang/artifactIdversion2.6/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scope/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.2.0/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactIdversion1.2.1/version/dependencydependencygroupIdcom.github.xiaoymin/groupIdartifactIdknife4j-spring-boot-starter/artifactIdversion3.0.2/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdversion2.7.3/version/plugin/plugins/build
/project