Spring boot Cache не работает, возвращаются существующие записи - PullRequest
0 голосов
/ 06 мая 2020

Я использую springBootVersion = 1.5.2.RELEASE и gradle version = 5.4.1.

Я передаю токен (рассмотрим учетную запись) в запросе. Может быть несколько каналов, связанных с учетной записью (запрошенный токен), я хочу получить все каналы, связанные с запросом учетной записи и сохранением их в списке.

Если пользователь не выполняет никаких действий в течение 2 минут для этого пользователя, истек срок действия только кеша.

По порядку чтобы получить идентификаторы каналов и сохранить их в списке, я использовал аннотацию Spring boot @Cacheable, но она не работает, поскольку извлекает существующие данные списка.

Пожалуйста, рассмотрите фиктивный сценарий, чтобы понять.

Если я запрашиваю token = 123, API извлекает все каналы, связанные с этим токеном, и сохраняет их в списке, что нормально.

но когда я запрашиваю token = 987, API все равно возвращает token = 123 каналов, API должен возвращать каналы, связанные с token = 987

Вот фрагмент кода

Главный класс Spring Boot Runner

@SpringBootApplication
@EnableCaching
@ComponentScan(basePackages = { "some packages to scan" })
public class ChannelApiApplication {
    public static void main(String[] args) {
        SpringApplication.run(ChannelApiApplication.class, args);
    }
}

UserController. java * 1 032 *

@RestController
public class UserController {
public ResponseEntity<AccountsAssociatedChannels> getAccountsAssociatedChannels(
        @PathVariable Integer userId,
        @RequestHeader("Cookie") String btCookie,
        @RequestParam(value = "page", required = false, defaultValue = "1") int page,
        @RequestParam(value = "pageSize", required = false, defaultValue = "25") int pageSize,
        UriComponentsBuilder uriBuilder, HttpServletResponse response, HttpServletRequest request)
        throws Exception {
    try {
        List<AccountAssociatedChannels> accountResponse = userService.getAccountsAssociatedChannels(userId,
                btCookie, page, pageSize);

UserService. java

@Service
@Configurable
public class UserService {

@Autowired
ChannelServiceClient channelServiceClient;

public List<AccountAssociatedChannels> getAccountsAssociatedChannels(Integer userId, String btCookie, int page,
            int pageSize) throws Exception {

    // Calling Rest API to get channels associated to btCookie
    List<MyChannelResponse> myChannelResponse = channelServiceClient.getMyChannelResponse(btCookie, page,
            pageSize, null);

ChannelServiceClient. java

@Service
public class ChannelServiceClient {

    @Cacheable(value = "channelIds", key = "#root.args[0]")
    public List<MyChannelResponse> getMyChannelResponse(String btCookie, Integer page, Integer pageSize, String nextLinkURL)
            throws Exception {

        List<MyChannelResponse> channelIds = new ArrayList<>();

        // Fetch all channelIds associated user and store into list.
        while (true) {

            // invoke getChannels() method to get channels
            MyChannelResponses myChannelResponse = getChannels(btCookie, page, pageSize, nextLinkURL);
            List<PaginationLinks> links = myChannelResponse.getLinks();
            channelIds.addAll(myChannelResponse.getChannels());
            //Some logic
        }
    } // getMyChannelResponse() method close



    @Cacheable(value = "mychannel", key = "#root.args[0]")
    public MyChannelResponses getChannels(String btCookie, Integer page, Integer pageSize, String uri)
            throws Exception {
        try {
            log.debug("Entering Cookie: " + btCookie + " to get channels.");    
            // Calling Spring Boot Rest API to get channel associated btCookie

        } catch (Exception e) { 
            // throwing some exception
        }
    }// getChannels() method close
}       

Спасибо.

Ответы [ 2 ]

1 голос
/ 06 мая 2020

Измените ключ в приведенном выше коде с обоих методов на этот

@Cacheable(value = "mychannel", key = "#btCookie")
0 голосов
/ 07 мая 2020

Ниже кода очищать кеш каждые 2 минуты

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.interceptor.SimpleKeyGenerator;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.cache.CacheBuilder;

@Configuration
public class CacheConfiguration extends CachingConfigurerSupport {

    private static final Logger log = LoggerFactory.getLogger(CacheConfiguration.class);

    @Bean
    @Override
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();

        log.info("cacheConfig");

        Cache getChannels = new ConcurrentMapCache("getChannels",
                CacheBuilder.newBuilder().expireAfterWrite(2, TimeUnit.MINUTES).build().asMap(), false);

        cacheManager.setCaches(Arrays.asList(getChannels));

        return cacheManager;
    }

    @Override
    public KeyGenerator keyGenerator() {
        return new SimpleKeyGenerator();
    }

    @Override
    public CacheErrorHandler errorHandler() {
        return null;
    }

    @Override
    public CacheResolver cacheResolver() {
        return null;
    }

}

И метод Cacheable будет

@Cacheable(value = "getChannels", key = "#btCookie", cacheManager="cacheManager")
    public List<MyChannelResponse> getMyChannelResponse(String btCookie, Integer page, Integer pageSize)
            throws Exception {
...