我正在嘗試設定 Redisson Hibernate 2L 快取,但我看到每次都執行休眠查詢,即使結果清楚地快取在我的 Redis 實體上。
除錯時,我可以看到它通過休眠并執行查詢,然后按預期進入putIntoCache
from 。RedissonStorage.java
當我檢查 Redis 時,我可以看到新的快取值。但是,在隨后對我的服務的呼叫中,它再次通過休眠executeQueryStatement
進行完全相同的休眠查詢,但有趣的是,它隨后進入getFromCache
fromRedissonStorage.java
并似乎從 Redis 回傳值。為什么每次都執行查詢而不是先檢查redis?
應用程式.yml
spring.profiles.active: local
server:
port: 9000
spring:
config:
active:
on-profile: local
cache:
type: redis
jpa:
database: POSTGRESQL
generate-ddl: true
properties:
hibernate:
jdbc:
time_zone: UTC
ddl-auto: create-drop
show_sql: true
cache:
use_query_cache: true
use_second_level_cache: true
factory_class: org.redisson.hibernate.RedissonRegionFactory
redisson:
fallback: true
config: redisson/redisson.yml
datasource:
platform: postgres
url: jdbc:postgresql://localhost:5432/test
username: postgres
password: admin
driverClassName: org.postgresql.Driver
initialization-mode: always
redisson.yml
singleServerConfig:
address: "redis://localhost:6379"
構建.gradle
plugins {
id 'org.springframework.boot' version '2.6.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-cache:2.6.4'
implementation 'org.redisson:redisson-hibernate-53:3.16.8'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.postgresql:postgresql'
compileOnly 'org.apache.logging.log4j:log4j-api:2.17.1'
compileOnly 'org.apache.logging.log4j:log4j-core:2.17.1'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
物體類
@Data
@Entity
@Table(name = "employees")
@Cacheable
@Cache(region = "employeeCache", usage = CacheConcurrencyStrategy.READ_WRITE)
public class EmployeeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long employeeId;
private String employeeName;
private String employeeLastName;
}
@Component
public class EmployeeDAO {
@Autowired
private EmployeeRepository employeeRepository;
public EmployeeEntity findByEmployeeId(Long employeeId) {
return employeeRepository.findByEmployeeId(employeeId);
}
@Repository
public interface EmployeeRepository extends CrudRepository<EmployeeEntity, Long> {
EmployeeEntity findByEmployeeId(Long employeeId);
}
uj5u.com熱心網友回復:
看起來 JPA 快取將適用于默認值findById
,但不適用于findAll
自定義findByType
或在這種情況下findByEmployeeId
。
但是,我找到了一種通過使用@QueryHints
.
import org.springframework.data.jpa.repository.QueryHints;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import javax.persistence.QueryHint;
import static org.hibernate.jpa.QueryHints.HINT_CACHEABLE;
@Repository
public interface EmployeeRepository extends CrudRepository<EmployeeEntity, Long> {
@QueryHints(value = { @QueryHint(name = HINT_CACHEABLE, value = "true")})
EmployeeEntity findByEmployeeId(Long employeeId);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/440785.html