1

The following works (results in the evict being performed):

fooController {
    @ApiEndpoint
    public delete(id) {
        fooService.deleteFoo(id)
    }
}

fooService {
    @CacheEvict(value = "cache1", key = "#id")
    public void deleteFoo(Long id) {
        //delete logic here
    }
}

But this does not work (nothing is evicted from the cache):

fooController {
    @ApiEndpoint
    public delete(name) {
        fooService.deleteFoo2(name)
    }
}

fooService {
    public void deleteFoo2(String name) {
        //delete logic here
        deleteFoo(name.getId())
    }

    @CacheEvict(value = "cache1", key = "#id")
    public void deleteFoo(Long id) {
        //delete logic here
    }
}

Why are my @CacheEvict annotations only called when the method is called straight from the controller?

I'm using Redis as the caching mechanism.

4

2 回答 2

3

当你的方法在类中被调用时,Aop 没有工作。当方法被另一个类调用时,它正在工作。因此,您可以在另一个服务中定义 deleteFoo。

于 2018-01-15T12:41:43.963 回答
0

要制作弹簧方面的拦截@Cache*注释,您必须进行外部调用。如果您不喜欢从另一个对象调用此方法,请使用bean 自调用方法。在这种情况下,您的类显示为两个对象,一个对象调用另一个对象:

@Resource private FooController thisBean;

public delete(id) {
  thisBean.deleteFoo(id)
}

@CacheEvict(value = "cache1", key = "#id")
public void deleteFoo(Long id) {}
于 2021-07-15T22:46:16.637 回答