Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
Spring Cacheable with Object

I can do the following while using Cacheable:

@Cacheable(value = CacheUtil.LEVEL1, key = "'Location'+#root.methodName+'_'+#country+'_'+#lang")
    public List<Location> getCities(String country, String lang)

and it works fine.

I am not sure about the following though. How can I cache the following Method with Cacheable?

public Content getContent(ContentRequest Request)

How should I write the @Cacheable so that It works?

Thanks.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need something like this:

@Cacheable(value = CacheUtil.LEVEL1, keyGenerator="contentRequestKeyGenerator")
public Content getContent(ContentRequest Request)

Where contentRequestKeyGenerator is the name of a bean that implements the KeyGenerator interface see http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/cache/interceptor/KeyGenerator.html

In the past I have had one bean that does key generation for several different cache classes, in the example below objects[0] is the class of the calling method, ContentRequest in your example :

@Component
public class MyKeyGenerator implements KeyGenerator{

    @Override
    public Object generate(Object o, Method method, Object... objects) {
        if (String.class.isInstance(objects[0])) {
            return ....
        }
        else if (....) {

    }
}

You would then use this :

@Cacheable(value = "properties", keyGenerator = "myKeyGenerator")
public Property getProperty(String key) {

Do you need to do this? The default is the hashCode of ContentRequest, usually this is good enough.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!