簡體   English   中英

春季:春季緩存不起作用

[英]Spring : Spring-cache not working

我正在開發Spring-MVC應用程序。 在探查器經過后端之后,我注意到getCurrentlyAuthenticatedUser()是一個熱點。 由於這個原因,我想到了使用緩存。 不幸的是,它不起作用。 一旦用戶登錄,即使該用戶已登錄,我也將獲得空用戶。出了什么問題。 當然,當我從XML刪除@Cacheable批注和配置時,一切正常。 你能幫忙的話,我會很高興。

PersonServiceImpl:

@Service
@Transactional
public class PersonServiceImpl implements PersonService {

  private final PersonDAO personDAO;

   @Autowired
    public PersonServiceImpl(PersonDAO personDAO) {
        this.personDAO = personDAO;
    }
 @Override
    @Cacheable(value = "person", condition = "#person == null")
    public Person getCurrentlyAuthenticatedUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return null;
        } else {
            return personDAO.findPersonByUsername(authentication.getName());
        }
    }
}

使用緩存的配置:

  <cache:annotation-driven />

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

當我調用此方法時,無論是否登錄,我都將返回null。 你能幫忙的話,我會很高興。 謝謝。

在下面的代碼行中:

@Cacheable(value = "person", condition = "#person == null")

unless condition unless請嘗試使用

實際上,您已經提出了一個條件,即“僅當人員為null時才可以使用緩存”,這就像“除非人員為null時才可以使用緩存”。 快樂的編碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM