Poorna Subhash
Poorna Subhash

Reputation: 2128

Spring cache constant as a key

As part of my code I have a method with empty parameters. For e..g,

public MasterData fetchMasterData() {
// DO something.
}

I want to add a @Cacheable with key as 'masterdata'. I tried the following, but it looks for a bean named 'masterdata'. I tried @Cacheable(cache="master", key="masterdata")

If I leave the key attribute, I know it takes as empty key. But I want to explicitly give a constant as key.

Is there a way to do that?

Upvotes: 10

Views: 6510

Answers (1)

Stephane Nicoll
Stephane Nicoll

Reputation: 33091

The key attribute is a SpEL expression so if you want the key to be masterdata you would write something like this

@Cacheable(cache="master", key="'masterdata'")
public MasterData fetchMasterData() { ... }

Upvotes: 26

Related Questions