手机端访问 php网站,品牌设计法则,php访问网站,上城区商城网站建设在使用EhCache缓存之前#xff0c;我们需要了解的是EhCache缓存是啥#xff1f;
Ehcache的概述
Ehcache是一个开源的Java缓存框架#xff0c;用于提供高效的内存缓存解决方案#xff0c;他可以用于缓存各种类型的数据#xff0c;包括对象#xff0c;查询结果#xff0…在使用EhCache缓存之前我们需要了解的是EhCache缓存是啥
Ehcache的概述
Ehcache是一个开源的Java缓存框架用于提供高效的内存缓存解决方案他可以用于缓存各种类型的数据包括对象查询结果方法调用的结果等。
Ehcache具有以下特点:
内存缓存Ehcache主要用于将数据存储在内存中用以提供更快速的访问速度他使用内存管理技术可以有效的管理缓存中的对象。可拓展性Ehcache支持分布式缓存可以在多个节点上部署用以提供更高的容量和吞吐量它还支持缓存的持久化可以将缓存数据写入磁盘以防止数据丢失。灵活的配置选项Ehcache提供了丰富的配置选项可以根据应用程序的需求进行灵活的配置。可以通过设置缓存的大小过期时间啊淘汰策略等等。支持缓存策略Ehcache支持多种缓存策略包括LRU最近最少使用、LFU最不经常使用、FIFO先进先出等。你可以根据数据的访问模式选择适合的缓存策略。与Spring集成Ehcache可以与Spring框架无缝集成通过简单的配置即可在Spring应用程序中使用。它还支持与其他Java框架的集成如Hibernate、MyBatis等。
在Spring Boot中当我们使用缓存功能的时候Spring Boot中自动侦测可用的缓存提供者Spring Boot有按照相关的顺序进行缓存提供者侦测
GenericSpring Boot首先尝试使用通用的缓存抽象即org.springframework.cache.CacheManager接口的实现。这个抽象可以适用于多种缓存提供者如Ehcache、Caffeine、Redis等。JCache (JSR-107)如果没有找到通用的缓存提供者Spring Boot会尝试使用JCache标准JSR-107的实现。JCache是Java EE的一部分定义了一套缓存API和规范。Ehcache 2.x如果没有找到JCache的实现Spring Boot会尝试使用Ehcache 2.x作为缓存提供者。Ehcache是一个流行的Java缓存框架它提供了丰富的功能和配置选项。Hazelcast如果没有找到Ehcache 2.xSpring Boot会尝试使用Hazelcast作为缓存提供者。Hazelcast是一个开源的分布式缓存和计算平台它提供了高性能和可扩展性。Infinispan如果没有找到HazelcastSpring Boot会尝试使用Infinispan作为缓存提供者。Infinispan是一个高度可扩展的分布式缓存平台它支持多种缓存模式和数据分布策略。Couchbase如果没有找到InfinispanSpring Boot会尝试使用Couchbase作为缓存提供者。Couchbase是一个面向文档的NoSQL数据库它也提供了缓存功能。Redis如果没有找到CouchbaseSpring Boot会尝试使用Redis作为缓存提供者。Redis是一个高性能的键值存储数据库它也可以用作缓存。Caffeine如果没有找到RedisSpring Boot会尝试使用Caffeine作为缓存提供者。Caffeine是一个基于Java的高性能缓存库它提供了快速的内存缓存解决方案。
除了按照以上顺序侦测外我们可以通过配置属性spring.cache.type来强制指定,假设没有强制指定那么我们该如何知道我们当前使用了什么缓存呢很简单此时debug就派上用场了我们可以通过debug去查看cacheManager对象的实例来判断当前使用了什么缓存。
当我们不指定具体的第三方的缓存时候Spring Boot的Cache模块会使用ConcurrentHashMap来存储而实际生产使用的时候因为我们可以可能需要更多其他特性往往就会采用其他缓存框架。
使用EhCache
引入相关依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-jpa/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId/dependencydependencygroupIdnet.sf.ehcache/groupIdartifactIdehcache/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdscopeprovided/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope在application.yml中配置我们需要的配置
spring.datasource.urljdbc:mysql://localhost:3306/test?useUnicodetruecharacterEncodingutf-8useSSLtrueserverTimezoneUTC
spring.datasource.usernameroot
spring.datasource.password123456
spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driverspring.jpa.show-sqltrue
spring.jpa.hibernate.ddl-autocreate-drop创建一个实体类
Entity
//Data
//NoArgsConstructor
public class User {IdGeneratedValueprivate Long id;private String name;private Integer age;public User(String name, Integer age) {this.name name;this.age age;}public Long getId() {return id;}public void setId(Long id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}public User() {}
}User实体类的数据访问内部含缓存注解
CacheConfig(cacheNames users)
public interface UserRepository extends JpaRepositoryUser, Long {CacheableUser findByName(String name);
}在resources目录下创建一个ehcache.xml文件
ehcache xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:noNamespaceSchemaLocationehcache.xsdcache nameusersmaxEntriesLocalHeap200timeToLiveSeconds600/cache/ehcache创建一个测试类单元测试
Slf4j
RunWith(SpringRunner.class)
SpringBootTest
public class ApplicationTests {Autowiredprivate UserRepository userRepository;Autowiredprivate CacheManager cacheManager;Testpublic void test() throws Exception {System.out.println(CacheManager type : cacheManager.getClass());// 创建1条记录userRepository.save(new User(AAA, 10));User u1 userRepository.findByName(AAA);System.out.println(第一次查询 u1.getAge());User u2 userRepository.findByName(AAA);System.out.println(第二次查询 u2.getAge());}
}至此我们可以看到第一行CacheManager type : class org.springframework.cache.ehcache.EhCacheCacheManager并且第二次查询的时候并没有输出SQL语句所以走的事缓存获取。