你们是否知道Java Map或类似的标准数据存储在给定的超时后自动清除条目?这意味着老化,旧的过期条目会自动"老化".
最好是在可通过Maven访问的开源库中?
我知道自己实现这些功能的方法,并且过去曾多次这样做过,所以我不是在这方面寻求建议,而是指向一个好的参考实现.
基于WeakReference的解决方案(如WeakHashMap)不是一个选项,因为我的键很可能是非实习字符串,我想要一个不依赖于垃圾收集器的可配置超时.
Ehcache也是一个我不想依赖的选项,因为它需要外部配置文件.我正在寻找一个仅限代码的解决方案.
从同一个bean的另一个方法调用缓存方法时,Spring缓存不起作用.
这是一个以清晰的方式解释我的问题的例子.
组态:
<cache:annotation-driven cache-manager="myCacheManager" />
<bean id="myCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="myCache" />
</bean>
<!-- Ehcache library setup -->
<bean id="myCache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true">
<property name="configLocation" value="classpath:ehcache.xml"></property>
</bean>
<cache name="employeeData" maxElementsInMemory="100"/>
Run Code Online (Sandbox Code Playgroud)
缓存服务:
@Named("aService")
public class AService {
@Cacheable("employeeData")
public List<EmployeeData> getEmployeeData(Date date){
..println("Cache is not being used");
...
}
public List<EmployeeEnrichedData> getEmployeeEnrichedData(Date date){
List<EmployeeData> employeeData = getEmployeeData(date);
...
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
aService.getEmployeeData(someDate);
output: Cache is not being used
aService.getEmployeeData(someDate);
output:
aService.getEmployeeEnrichedData(someDate);
output: Cache is not being used
Run Code Online (Sandbox Code Playgroud)
该getEmployeeData方法调用使用缓存employeeData …
我在tomcat 9.0.2上使用Spring Boot 1.5.9 并尝试使用spring @Cacheable调度在应用程序启动时运行的缓存刷新作业来缓存查找,并且每24小时重复一次,如下所示:
@Component
public class RefreshCacheJob {
private static final Logger logger = LoggerFactory.getLogger(RefreshCacheJob.class);
@Autowired
private CacheService cacheService;
@Scheduled(fixedRate = 3600000 * 24, initialDelay = 0)
public void refreshCache() {
try {
cacheService.refreshAllCaches();
} catch (Exception e) {
logger.error("Exception in RefreshCacheJob", e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
缓存服务如下:
@Service
public class CacheService {
private static final Logger logger = LoggerFactory.getLogger(CacheService.class);
@Autowired
private CouponTypeRepository couponTypeRepository;
@CacheEvict(cacheNames = Constants.CACHE_NAME_COUPONS_TYPES, allEntries = true)
public void clearCouponsTypesCache() {} …Run Code Online (Sandbox Code Playgroud) 我正在为maven多模块项目开发缓存实现(exstremescale),我在其中添加了maven依赖项
<dependency>
<groupId>com.ibm.extremescale</groupId>
<artifactId>ogclient</artifactId>
<version>8.6.0.20150901-215917</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
添加了缓存注释
@Override
@Cacheable(value = "productDetails", key = "#productId + #orgId")
public Product productRead(final String productId, final String productKey, final String orgId, final CRApplicationEnum sourceSystem) throws IntegrationException {
Run Code Online (Sandbox Code Playgroud)
缓存manager.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager" primary="true">
<property name="caches">
<set>
<bean class="com.ibm.websphere.objectgrid.spring.ObjectGridCache"
p:name="eventDetails" p:map-name="${iev.eventDetails.mapName}"
p:object-grid-client-ref="wxsGridClient" />
<bean class="com.ibm.websphere.objectgrid.spring.ObjectGridCache"
p:name="eventValidationDetails" p:map-name="${iev.eventValidationDetails.mapName}"
p:object-grid-client-ref="wxsGridClient" />
<bean class="com.ibm.websphere.objectgrid.spring.ObjectGridCache"
p:name="productDetails" p:map-name="${ipr.productDetails.mapName}"
p:object-grid-client-ref="wxsGridClient" />
</set>
</property>
</bean>
<bean id="wxsCSDomain"
class="com.ibm.websphere.objectgrid.spring.ObjectGridCatalogServiceDomainBean"
p:catalog-service-endpoints="${xscale.catalogServiceEndpoint}" /> …Run Code Online (Sandbox Code Playgroud) I'm trying to call a @Cacheable method from within the same class.
And it didn't work. Because of:
In proxy mode (the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation (in effect, a method within the target object that calls another method of the target object) does not lead to actual caching at runtime even if the invoked method is marked with @Cacheable. Consider using the aspectj mode in this case. Also, …
如果我为没有任何参数的方法定义了一个ehcache.
但在我的用例中,我需要通过它的密钥访问我构建的缓存.
所以请为我提供更好的方法来分配密钥.
关注是我的代码:
@Override
@Cacheable(value = "cacheName", key = "cacheKey")
public List<String> getCacheMethod() throws Exception{
Run Code Online (Sandbox Code Playgroud)
PS当我尝试从其他地方访问此方法时,我收到以下错误.
org.springframework.expression.spel.SpelEvaluationException:EL1008E:(pos 0):在'org.springframework.cache.interceptor.CacheExpressionRootObject'类型的对象上找不到字段或属性'cacheKey'
java ×5
spring ×5
caching ×4
ehcache ×3
spring-cache ×2
aspectj ×1
dictionary ×1
spring-boot ×1
tomcat ×1