问题描述
由于需要很长时间,因此我需要为几个数据库调用缓存结果。 我正在尝试使用EhCache。我正在使用Spring Boot。
下面是我的存储库类(与我的服务类和RestController放在一个单独的模块中)
@Cacheable(value = "product.result",key="#term")
public List<StoreProduct> getTopProductsForTerm(String term,int offset,int maxCount) {
String query = GET_TOP_STORE_CATEGORY;
Query nativeQuery = this.entityManager.createNativeQuery(query);
nativeQuery.setParameter(1,maxCount);
nativeQuery.setParameter(2,offset);
nativeQuery.setParameter(3,term);
return nativeQuery.getResultList();
}
下面是我的ehcache.xml
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="
http://www.ehcache.org/v3
http://www.ehcache.org/schema/ehcache-core-3.7.xsd">
<!-- Persistent cache directory -->
<persistence directory="spring-boot-ehcache/cache" />
<!-- Default cache template -->
<cache-template name="default">
<expiry>
<ttl unit="seconds">604800</ttl>
</expiry>
<listeners>
<listener>
<class>com.pixyfi.data.logger.CacheLogger</class>
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
<event-ordering-mode>UnorDERED</event-ordering-mode>
<events-to-fire-on>CREATED</events-to-fire-on>
<events-to-fire-on>EXPIRED</events-to-fire-on>
<events-to-fire-on>evictED</events-to-fire-on>
</listener>
</listeners>
<resources>
<heap>1000</heap>
<offheap unit="MB">10</offheap>
<disk persistent="true" unit="MB">20</disk>
</resources>
</cache-template>
<cache alias="product.result" uses-template="default">
<key-type>java.lang.String</key-type>
<value-type>java.util.ArrayList</value-type>
</cache>
</config>
由于我们正在使用Spring,因此在stackoverflow上有一个答案,建议我们不再控制定义键类型和值类型。但是当我从ehcache.xml中删除这些条目时,我又收到了另一个异常,指出密钥类型
持久键类型'java.lang.String'与
不同
如果仅定义键类型,则值类型失败,但出现相同的异常 如果我同时指定两者,则它将失败,并出现异常
缓存[product.result]指定键/值类型。使用getCache(String, 班级,班级)
我也尝试使用KeyGenerator,但同样因键类型不匹配而失败。 我按照这篇https://dimitr.im/spring-boot-cache-ehcache的帖子来设置缓存,但似乎不起作用
我猜(我可能错了),Spring专门提供了未激活的缓存管理器
下面是我使用的依赖项 在包含rest API的模块中(也尝试在数据模块中定义) org.springframework.boot 弹簧启动启动缓存
在数据模块中
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
有什么我想念的吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)