Mar*_*yeh 6 java spring caching hibernate spring-boot
我通过添加以下配置在我的 sprint boot 2.1.8 应用程序中启用了休眠二级缓存:
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
Run Code Online (Sandbox Code Playgroud)
我还添加了eh缓存依赖如下:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.3.11.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我注释了一个实体是可缓存的:
@Entity(name = "entity")
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table(name = "entity")
@AttributeOverride(name = "id", column = @Column(name = "entity_id"))
public class Entity extends ItemBase {
private String n1;
private String n2;
private String n3;
private int n4;
@Column(name = "n_5")
private int n5;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
每次我调用存储库类(扩展 JpaCrudRepository)并调用 findAll(); 或 findOneById(),我看到休眠日志 HQL 查询和数据库实际上被调用。
所需的行为是从数据库中获取一次实体列表并将它们保存在缓存中,应该使用读写策略在那里更新它们。
知道如何实现吗?
| 归档时间: |
|
| 查看次数: |
284 次 |
| 最近记录: |