开发者社区 问答 正文

关于Spring3.1方法缓存的问题

@Cacheable(value="name1")
public List get1() {
    // 访问数据库
    return dao.list();
}
 
 
@Cacheable(value="name2")
public List get2() {
    List list = get1();
    ......
    return list2;
}
 
在get2方法中调用get1方法,发现没有用到缓存?
 
这是为什么?
---------------问题补充---------------
@melnnyy:get1在类以外的地方调用过,并且缓存成功 若在该类里面其他方法中调用get1,则无法用到缓存!!! 不管其他方法是否有缓存注解!!!(4年前)

@melnnyy:<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="E:/" /> <defaultCache eternal="false" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" /> <cache name="name1" eternal="true" maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false" memoryStoreEvictionPolicy="LRU" /> <cache name="name2" eternal="true" maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false" memoryStoreEvictionPolicy="LRU" /> </ehcache>

展开
收起
a123456678 2016-03-13 14:01:38 1870 分享 版权
1 条回答
写回答
取消 提交回答
  • Spring Cache基于AOP实现, AOP, 调用Proxy上的方法会被拦截, 缓存会生效, get2中的get1调用是普通的Java调用, 不会调用到Proxy对象上, 因此不会被拦截, 缓存不会生效。

    2019-07-17 19:02:31
    赞同 展开评论