gpt4 book ai didi

java - Spring Cache - 生成自定义缓存结果

转载 作者:行者123 更新时间:2023-12-01 09:49:19 25 4
gpt4 key购买 nike

我正在使用 Spring Boot 和 EhCache 开发日历应用程序。

一些用户可以请求特定一周的音乐会,其他用户则可以请求整个月的音乐会。所有响应都会被缓存,生存时间为 300 秒,以减少响应时间。

缓存方法的输入参数为startDateendDate用户名

问题是,当用户请求整个月的音乐会,然后在同月内的一周内,该方法将执行两次,即使“周”响应可以根据“月”计算出来响应,因为该周可能是缓存月份的子集。

示例:

1. 
INPUT
startDate:08.06.2016, endDate:07.07.2016, username:exampleUser
//the method is executed, response cached
OUTPUT
concert "Example Concert" on 13.06.2016

2.

INPUT
startDate:08.06.2016, endDate:15.06.2016, username:exampleUser
//the method is executed, response cached
OUTPUT
concert "Example Concert" on 13.06.2016

正如您在示例中看到的,该方法将执行两次,但实际上没有必要执行第二个示例中的方法,可以从第一次请求返回的缓存数据中提取响应。

我正在考虑为音乐会缓存创建一个自定义“生成器”,它将能够循环遍历所有缓存数据并查找“包含”请求的startDate日期范围和结束日期

这是我到目前为止所拥有的:

缓存方法:

@Override
@CachePut("concerts")
public List<Event> refreshEventsCache(String eventsForUser, Date startDate, Date endDate) throws Exception {

return fetchEventsFromTheServer(eventsForUser, startDate, endDate);
}


@Override
@Cacheable(value = "concerts")
public List<Event> getEvents(String eventsForUser, Date startDate, Date endDate) throws Exception {

return fetchEventsFromTheServer(eventsForUser, startDate, endDate);
}

ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<defaultCache maxElementsInMemory="500" eternal="false"
overflowToDisk="false" memoryStoreEvictionPolicy="LFU" />

<diskStore path="java.io.tempdir"/>

<cache name="concerts"
maxElementsInMemory="5000"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU" />
</ehcache>

Application.java:

...
@SpringBootApplication
@ComponentScan
@EnableCaching
@EnableScheduling
public class SpringBootWebApplication extends SpringBootServletInitializer {

public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}

private static Class<SpringBootWebApplication> applicationClass = SpringBootWebApplication.class;



}

如何构建“智能”缓存生成器来返回缓存数据的子集?

最佳答案

这里不能使用Spring的默认缓存机制,因为在下面的方法中,

@Override
@Cacheable(value = "concerts")
public List<Event> getEvents(String eventsForUser, Date startDate, Date endDate) throws Exception {

return fetchEventsFromTheServer(eventsForUser, startDate, endDate);
}

缓存 key 将根据所有三个参数eventsForUser、startDate、endDate生成。

因此,在您的第二个查询中,即前一个 startDateendDate 之间的一周,生成的 key 将有所不同,因此会出现缓存未命中,因此它会直接从数据库加载。

正如您所要求的,您需要实现一个“智能”缓存,这只是一个想法,

您可以注入(inject)CacheManager并使用它根据您生成的缓存键检索缓存列表。

例如:- startDate:08.06.2016、endDate:07.07.2016、username:exampleUser 的缓存 key 可以是 08.06.2016_07.07.2016_exampleUser

您可以使用

Cache cache = cacheManager.getCache("08.06.2016_15.06.2016_exampleUser");

首先尝试获取本周的缓存,如果缓存未命中,那么您可以转到 endDate 月份的最后一个日期并检查月份的缓存

Cache cache = cacheManager.getCache("08.06.2016_07.07.2016_exampleUser");

如果您找到了月份的缓存,您可以迭代从中获取周的缓存,或者您必须从数据库中获取周的缓存。

关于java - Spring Cache - 生成自定义缓存结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37709474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com