rimsky90
rimsky90

Reputation: 21

Using non-constant properties or fields in key for @CacheEvict

Is it possible to use the non constant fields or properties of current class in key of the @CacheEvict annotation? For example:

public class Feature {

    private int id;

    @Autowired
    private FeaturesClient featuresClient;

    @CacheEvict(value = CacheConfiguration.FEATURES, key = 
               "T(java.lang.String).valueOf(#userId).concat(T(java.lang.String)" +
               ".valueOf( **#id** ))")
    public boolean isFeatureAvailable(long userId) {
        return featuresClient.isFeatureAvailable(userId, id);
    }

}

Upvotes: 2

Views: 475

Answers (1)

OrangeDog
OrangeDog

Reputation: 38777

Yes.

@CacheEvict(key = "#userId + #root.target.id")

Documentation:

#root.method, #root.target, and #root.caches for references to the method, target object, and affected cache(s) respectively.

Upvotes: 1

Related Questions