简体   繁体   English

Spring Boot @CachePut值为null

[英]Spring Boot @CachePut value is null

I have a map that stores simple POJOs, the key is the id field of the POJO. 我有一个存储简单POJO的地图,键是POJO的id字段。 From Spring's @CachePut annotation, I expected something like this: 从Spring的@CachePut批注中,我期望这样的事情:

JobModel jm = new JobModel();
cachemap.put(jm.getId(), jm);

Still, it inserts null values to the cache every time. 尽管如此,它每次仍将空值插入高速缓存。 If I disallow null values when I configure the Cache, I get en exception saying null is being inserted. 如果在配置缓存时不允许使用null值,则会出现异常,提示正在插入null。

Code: 码:

@SpringBootApplication
@EnableScheduling
@EnableAutoConfiguration
@EnableCaching
public class Application {

    public static void main(String[] args) {
        applicationContext = SpringApplication.run(Application.class, args);
    }

... ...

private GuavaCache jobCache() {
    return new GuavaCache(CacheNames.CACHE_JOB, CacheBuilder.newBuilder()
            .maximumSize(999999)
            .build(),
            false);// or true, still null values
}
   @Bean(name = "cacheManager")
    public CacheManager cacheManager() {
        SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
        simpleCacheManager.setCaches(Arrays.asList(
                jobCache(),....
        ));
        return simpleCacheManager;
    }

And the Dao class, implementing an interface, the Annotations are declared in the Dao's (the class that implements the interface, not in the interface itself) 还有实现接口的Dao类,则在Dao的声明中声明Annotations(实现接口的类,而不是在接口本身中)

@CachePut(value = CacheNames.CACHE_JOB,key = "jm.id")
    @Transactional
    @Override
    public void insert(JobModel jm) {...}

I tried jm.id , jm.getid() , #a0.getId() . 我尝试了jm.idjm.getid()#a0.getId() Still, everytime I get and exception from com.google.common.base.Preconditions.checkNotNull() (or just a simple null insert). 不过,每次我都从com.google.common.base.Preconditions.checkNotNull()获得异常(或者只是简单的null插入)。 I placed a breakpoint there and I can see that the key is what I expected it to be (a guid, string), but the value is null. 我在此处放置了一个断点,可以看到该键是我期望的键(GUID,字符串),但该值为空。

Per the spring docs at http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/annotation/CachePut.html 根据http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/annotation/CachePut.html上的spring文档

it always causes the method to be invoked and its result to be stored in the associated cache.

Your insert method needs to return the value you want cached. 您的insert方法需要返回要缓存的值。

Could be something as simple as: 可能很简单:

@CachePut(value = CacheNames.CACHE_JOB,key = "jm.id")
@Transactional
@Override
public JobModel insert(JobModel jm) {
  return jm;
}

though this doesn't feel like the best way to design the interaction. 尽管这似乎不是设计交互的最佳方法。 You may want to look at moving the annotation to whatever method is constructing the JobModel passed to the insert method or if the JobModel is being read from a database, the method which saves the JobModel to the database may be a good place as well. 您可能需要查看将注释移动到构造传递给insert方法的JobModel的任何方法,或者如果正在从数据库读取JobModel,那么将JobModel保存到数据库的方法也可能是一个好地方。

Also, you can use unless conditional property on the cache annotations as below. 此外,您可以使用以下条件, unless对高速缓存注释使用条件属性。 This parameter takes a SpEL expression that is evaluated to either true or false . 此参数采用SpEL表达式,其计算结果为truefalse If true , the method is cached - if not, it behaves as if the method is not cached . 如果为true ,则该方法被缓存-如果不缓存,则其行为就像未缓存该方法一样

@CachePut(value = CacheNames.CACHE_JOB, key = "jm.id", unless = "#result == null")
@Transactional
@Override
public void insert(JobModel jm) {
    return jm;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM