DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. Spring 3.1.M1 @Cacheable Doesn’t Evict – A Workaround

Spring 3.1.M1 @Cacheable Doesn’t Evict – A Workaround

David Salter user avatar by
David Salter
·
May. 13, 11 · Interview
Like (0)
Save
Tweet
Share
17.28K Views

Join the DZone community and get the full member experience.

Join For Free

Spring 3.1 introduces a new feature to allow methods to be cached and evicted thus allowing resource heavy methods to be avoided where possible. Caching is enabled via the new @Cacheable and @CacheEvict annotations. For full details of Spring caching have a look at Costin Leau’s blog post.

One example of caching would be, for example, database activity. We can apply the @Cacheable annotation to a find operation and then apply the @CacheEvict to an update / delete operation.  In this sense, caching would work much like a second level cache in Hibernate or JPA.

To enable caching on a find method, the method needs to be annotated with the @Cacheable annotation identifying which cache to use.  Spring allows multiple caches to be defined each of which can be backed by a different caching abstraction.

@Cacheable("items")
public Item find(long itemId) {
Item item = entityManager.find(Item.class, itemId);
return item;
}

When it is time to invoke the find method, Spring checks in the specified cache to see if the results of the operation have already been cached and if the results can be therefore be returned from cache instead of invoking the method. Spring uses the method arguments as the key, so in this case the itemId parameter.

To evict an entry from the cache when an object is updated in the database, the @CacheEvict annotation can be used.  Again, this annotation takes a parameter identifying which cache to use.

@CacheEvict(value = "items", key = "#item.id")
public void updateItem(Item item) {
entityManager.merge(item);
}

You can see in this sample code, that Spring Expression Language has been used to define the key for items in the cache.  This is necessary as a long is not being used as the key as in the previous example so cache entries would not be found.

This all looks pretty straightforward, but unfortunately due to bug SPR-8015 it does not work.

To quote the bug report:

“… it's not working because the DefaultKeyGenerator simply hashes the given parameters as is. In case no key attribute is configured (just like in the above example) the parameters will be handed in as single-element-Object[] causing a different hash to be created than if the single object would be handed to the key generator directly”

This is fixed in Spring 3.1.M2, but in the meantime, can be worked around by explicitly specifying the key on the @Cacheable annotation, i.e.

@Cacheable(value = "items", key = "#itemId")
public Item find(long itemId) {
Item item = entityManager.find(Item.class, itemId);
return item;
}

From http://www.davidsalter.co.uk/1/post/2011/05/spring-31m1-cacheable-doesnt-evict-a-workaround.html

Spring Framework Cache (computing) Workaround

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Important Takeaways for PostgreSQL Indexes
  • What Is QA as a Service?
  • How We Solved an OOM Issue in TiDB with GOMEMLIMIT
  • Utilizing Database Hooks Like a Pro in Node.js

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: