Spring cacheable annotations do not cache null values

Source: Internet
Author: User
Tags mongodb postgresql redis

Today, when annotated with cacheable, null values are found and cached. The next time another system updates the value, from the cache, or the null value, there is a problem. So on the one hand, you want the other system to update, to clear the cache, on the other hand do not want to cache too much junk data.

Reference http://stackoverflow.com/questions/12113725/ How-do-i-tell-spring-cache-not-to-cache-null-value-in-cacheable-annotation the procedure and make changes to the program


   @Cacheable(key = "''+#id", unless="#result == null")
    public XXXPO get(int id) {
       //get from db
    }
 

This way, when a method returns a null value, it is not cached. See unless explanation:


/**
 * Spring Expression Language (SpEL) attribute used to veto method caching.
 * <p>Unlike {@link #condition()}, this expression is evaluated after the method
 * has been called and can therefore refer to the {@code result}. Default is "",
 * meaning that caching is never vetoed.
 * @since 3.2
 */

The effect is to use the value of result to decide whether to negate the method cache. So it can be used to make conditional judgments.

Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.