1

我正在尝试使用 Spring Caching 注释@Cacheable@CacheEvict与 GuavaCacheManager 一起使用。

我用这两个测试创建了一个测试用例:

  1. cachesById- 验证对带有注释的方法的两次调用是否@Cacheable返回相同的对象
  2. evict@CacheEvict- 如果在这两个调用之间调用带有注释的方法,则验证是否返回两个不同的实例

当我没有为 指定密钥时,两者都可以正常工作@CacheEvict,但是当我这样做时,会出现以下异常:

java.lang.NullPointerException
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:210)
    at com.google.common.cache.LocalCache$LocalManualCache.invalidate(LocalCache.java:4764)
    at org.springframework.cache.guava.GuavaCache.evict(GuavaCache.java:135)
    at org.springframework.cache.interceptor.AbstractCacheInvoker.doEvict(AbstractCacheInvoker.java:95)
    at org.springframework.cache.interceptor.CacheAspectSupport.performCacheEvict(CacheAspectSupport.java:409)
    at org.springframework.cache.interceptor.CacheAspectSupport.processCacheEvicts(CacheAspectSupport.java:392)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:362)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:299)
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
    at com.myorg.caching.CacheTest$Repo$$EnhancerBySpringCGLIB$$eed50f3e.update(<generated>)
    at com.myorg.caching.CacheTest.evict(CacheTest.java:50)

这可以通过执行以下测试来重现。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
        classes = { Repo.class, CacheTest.SpringConfig.class },
        loader = AnnotationConfigContextLoader.class)
public class CacheTest {

    private static final String CACHE_NAME = "cacheName";

    @Inject
    private Repo repo;

    @Test
    public void cachesById() {
        Entity aResult1 = repo.getEntity(1);
        Entity aResult2 = repo.getEntity(1);
        assertEquals(aResult1.getId(), aResult2.getId());
        assertSame(aResult1, aResult2);
    }

    @Test
    public void evict() {
        Entity aResult1 = repo.getEntity(1);
        repo.update(aResult1);
        Entity aResult2 = repo.getEntity(1);
        assertEquals(aResult1.getId(), aResult2.getId());
        assertNotSame(aResult1, aResult2);
    }

    /** Mock repository/entity classes below. */

    @Component
    public static class Repo {

        @Cacheable(value = CACHE_NAME, key = "#id")
        public Entity getEntity(int id) {
            return new Entity(id);
        }

        @CacheEvict(value = CACHE_NAME, key = "#id")
        public void update(Entity e) {    
        }
    }


    public static class Entity {
        private int id;

        public Entity(int id) {
            super();
            this.id = id;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }
    }

    /** Guava Cachemanager Spring configuration */

    @Configuration
    @EnableCaching
    public static class SpringConfig {
        @Bean
        public CacheManager cacheManager() {
            GuavaCacheManager manager = new GuavaCacheManager(CACHE_NAME);
            manager.setCacheBuilder(CacheBuilder.newBuilder().expireAfterWrite(
                    1, TimeUnit.MINUTES).recordStats());
            return manager;
        }
    }
}

但是,如果我改变测试通过

@CacheEvict(value = CACHE_NAME, key = "#id")
public void update(Entity e) {

进入:

@CacheEvict(value = CACHE_NAME)
public void update(Entity e) {

..但是我错过了需要为Entity. 有谁知道我错过了什么?

谢谢!

4

1 回答 1

12

你必须修复你的组件类

@Component
public static class Repo {

    @Cacheable(value = CACHE_NAME, key = "#id")
    public Entity getEntity(int id) {
        return new Entity(id);
    }

    @CacheEvict(value = CACHE_NAME, key = "#id")
    public void update(Entity e) {    
    }
}

@Component
public static class Repo {

    @Cacheable(value = CACHE_NAME, key = "#id")
    public Entity getEntity(int id) {
        return new Entity(id);
    }

    @CacheEvict(value = CACHE_NAME, key = "#e?.id")
    public void update(Entity e) {    
    }
}

为什么?在getEntity使用 缓存Entity对象的方法中int id,您必须将其传递给int id@CacheEvict注释的方法。您不必更改方法的签名 - 通过使用 SPEL,您可以“进入”实体并使用其 id 字段。

希望我有所帮助。

于 2015-10-29T12:48:56.583 回答