Обновить / загрузить только недавно измененное значение в кэш - PullRequest
0 голосов
/ 20 июня 2019

Я использую кеш кофеина, и в кеше около 7-8 записей Lakh, но при обновлении я не хочу обновлять кеш дыры, потому что это занимает 10-11 минут. Так есть ли способ обновить только измененное значение

Я пробовал несколько способов, таких как Ehcache, Guava, Caffeine

Caffeine.newBuilder().maximumSize(1000).refreshAfterWrite(11, TimeUnit.MINUTES)

Код контроллера

package com.cache.code.CaffeineCache.iHexa.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.cache.code.CaffeineCache.iHexa.beans.CategoryType;
import com.cache.code.CaffeineCache.iHexa.beans.Ctgry;
import com.cache.code.CaffeineCache.iHexa.beans.Factory;
import com.cache.code.CaffeineCache.iHexa.service.CategoryRelationshipService;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.ibm.db2.jcc.a.c;

@RestController
@Component
public class CategoryTypeController {

    @Autowired
    CategoryRelationshipService categoryRelationshipService;

    @RequestMapping(value = "/loadRelationShip", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public List<Ctgry> loadRelationShip() {
        List<Ctgry> ctgries = null;
        try {
            ctgries = categoryRelationshipService.loadCategoryRelationship();

        } catch (Exception ee) {
            System.out.println(ee);
        }
        return ctgries;
    }

    @RequestMapping(value = "/loadRelationShipById/{r_id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Ctgry loadRelationShipById(@PathVariable String r_id) {
        Ctgry ctgries = null;
        List<Ctgry> list = null;
        try {
            list = categoryRelationshipService.loadCategoryRelationship();
            if (list != null) {
                ctgries = list.stream().filter(x -> Integer.parseInt(r_id) == x.getCtgry_id()).findAny().orElse(null);
            }

        } catch (Exception ee) {
            System.out.println(ee);
        }
        return ctgries;
    }
}

Сервисный код

package com.cache.code.CaffeineCache.iHexa.serviceImpl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.cache.code.CaffeineCache.iHexa.beans.CategoryType;
import com.cache.code.CaffeineCache.iHexa.beans.Ctgry;
import com.cache.code.CaffeineCache.iHexa.beans.Factory;
import com.cache.code.CaffeineCache.iHexa.dao.CategoryRelationshipDao;
import com.cache.code.CaffeineCache.iHexa.service.CategoryRelationshipService;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;

@Service
public class CategoryRelationshipServiceImpl implements CategoryRelationshipService {

    @Autowired
    CategoryRelationshipDao categoryRelationshipDao;

    LoadingCache<String, List<Ctgry>> pairsCacheCtgry = null;

    @Override
    public List<Ctgry> loadCategoryRelationship() {

        if (pairsCacheCtgry == null) {

            pairsCacheCtgry = Caffeine.newBuilder().maximumSize(1000).expireAfterWrite(11, TimeUnit.MINUTES)
                    .refreshAfterWrite(11, TimeUnit.MINUTES).build(title -> { // Using a jOOQ repository

                        System.out.println("!!!!!!!!!!!!!!!!");
                        return categoryRelationshipDao.loadCategoryRelationship();
                    });
        }
        return pairsCacheCtgry.get("");
    }

}

Так что любое предложение, которое может работать для меня

...