Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot @CachePut, how it works

I'm trying to update values in spring cache, however @CachePut does not replace actual value, but put another value with the same key:

Cachename: noTimeCache:

  • Key: SYSTEM_STATUS, val: ON
  • Key: SYSTEM_STATUS, val: OFF

1.Why?

My cache service:

@CachePut(value = "noTimeCache", key = "#setting.getSetting().name()")
public String updateGlobalCacheValue(GlobalSettings setting) {
    System.out.println("Setting: " + setting.getSetting().name() + ", val: " + setting.getValue());
    return setting.getValue();
}

I know it's looks funny but i need to work with jpa repositories. Maybe someone have better solution.

2.Why can't I use function argument as @Cacheable key like this?

@Cacheable(value = "noTimeCache", key = "#setting.name()", unless = "#result == null")
@Query("SELECT gs.value FROM GlobalSettings gs WHERE gs.setting = ?1")
String getGlobalSettingValue(Settings setting);

It tells me setting is null.

3.Is the way to override @Repositository interface methods, for example save() to add annotation @Cacheable to them?

like image 372
CSniper Avatar asked Nov 15 '25 10:11

CSniper


1 Answers

Thanks for answers. It helps me solve problems.

@CachePut replace values, but there was a problem in keys. I use something like this:

@Cacheable(value = "globalSettings", unless = "#result == null")
@Query("SELECT gs.value FROM GlobalSettings gs WHERE gs.setting = ?1")
String getGlobalSettingValue(Settings setting);

Settings is enum and key for default is enum name, for example SYSTEM_STATUS.

and this:

@Cacheable(value = "globalSettings", key = "#setting.name()")
public String getGlobalSettingEnumValue(Settings setting) {
    return Settings.valueOf(setting.name()).getDefaultValue();
}

will also save key as STSYEM_STATUS.

Key's was the same but cache interprets this like 2 different cache values, I don't know why.

U can't use variable name in repository class like #setting, it must be argument index like #p0, probably beacause of spring data use his own proxy classes.

This solve all my problems and now cache work properly.

like image 167
CSniper Avatar answered Nov 17 '25 09:11

CSniper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!