1
resposta

Cache do spring

Tenho esta configuração de cache

import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType;
import org.springframework.cache.annotation.Cacheable;

@Repository
public interface UsuarioRepository extends JpaRepository<Usuario, String> {

         String USERS_BY_LOGIN_CACHE = "usersByLogin";

    @EntityGraph(attributePaths = "usuarioRole", type = EntityGraphType.LOAD)
    @Cacheable(cacheNames = USERS_BY_LOGIN_CACHE)
    Optional<Usuario> findByLogin(String login);

}

Configuração

import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.context.annotation.Configuration;
import org.springframework.cache.annotation.EnableCaching;

@Configuration
@EnableCaching
public class CacheConfiguration {

@Bean
    public JCacheManagerCustomizer cacheManagerCustomizer() {
        return cm -> {
            createCache(cm, UsuarioRepository.USERS_BY_LOGIN_CACHE);
            createCache(cm, UsuarioRepository.USERS_BY_EMAIL_CACHE);
            createCache(cm, Usuario.class.getName());
            createCache(cm, Role.class.getName());
            createCache(cm, Usuario.class.getName() + ".usuarioRole");
        };
    }
}

Mas quando cadastro um usuário ele não consegue logar. Para tal tenho que reiniciar o servidor ou tirar as anotações @EntityGraph e @Cacheable. O que falta fazer ?

Tirando a página de login fica mais lenta.

1 resposta

Oi Guilherme, tudo bem ?

Vi que sua dúvida tá aberta faz um tempo, você conseguiu solucionar ?

Se sim fala pra gente como fez?