@Cacheable [Spring MVC] не работает - PullRequest
0 голосов
/ 02 мая 2018

В моем весеннем проекте есть упаковка для банок. Я использую Spring 4.3.2 В моей папке ресурсов. У меня есть XML-файл конфигурации: abc-servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:beans="http://www.springframework.org/schema/beans"
         xmlns:cache="http://www.springframework.org/schema/cache"
         xmlns:p="http://www.springframework.org/schema/p"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/mvc
                    http://www.springframework.org/schema/mvc/spring-mvc.xsd
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context.xsd
                    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

<cache:annotation-driven />

<context:component-scan base-package="sayak.auth,
                        sayak.member"/>

<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
    <property name="caches">
        <set>
            <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
                    p:name="memberInLastMonth"/>
        </set>
    </property>
</bean>

Я использую @Cacheable для метода dao, как показано ниже, а также аннотировал класс с помощью @Component:

@Cacheable(value="memberInLastMonth" , key="#authenticationId")
public List<AuthenticationAuditTrail> findByAuthenticationId(long authenticationId) {
//DB call to fetch data
}

Результат этого метода не кэшируется при вызове с тем же аутентификационным идентификатором, я использовал простое утверждение, чтобы проверить его и не получить желаемый результат. Любое руководство будет полезно.

Метод испытания для этого:

@Test
public void testMemberInLastMonth() {
Member member=getMember();
List<AuthenticationAuditTrail> records = getDAO()
    .findByAuthenticationId(member.getAuthentication().getId(),
        DateUtil.getPastMonth(1));
List<AuthenticationAuditTrail> cachedRecords = getDAO()
    .findByAuthenticationId(member.getAuthentication().getId(),
        DateUtil.getPastMonth(1));

for(int i=0;i<records.size();i++){
  AssertUtil.assertTrue(records.get(0) == cachedRecords.get(0));
}

}

...