Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda