Skip to content

"SpEL" does not work with the cache #1

@Yannx79

Description

@Yannx79

CRUDImpl

package com.nk.realstateapi.services.impl;

import com.nk.realstateapi.repositories.IGenericRepository;
import com.nk.realstateapi.services.ICRUD;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
public abstract class CRUDImpl<T, K> implements ICRUD<T, K> {

    protected abstract IGenericRepository<T, K> getRepo();

    @Override
    @CacheEvict(value = "#{T(this).getCacheName()}", allEntries = true)
    public T save(T t) throws Exception {
        return getRepo().save(t);
    }

    @Override
    @CacheEvict(value = "#{T(this).getCacheName()}", allEntries = true)
    public T update(T t, K k) throws Exception {
        getRepo().findById(k).orElseThrow(() -> new Exception("ID NOT FOUND: " + k));
        return getRepo().save(t);
    }

    @Override
    @Cacheable(value = "#{T(this).getCacheName()}", keyGenerator = "versionedKeyGenerator")
    public List<T> readAll() throws Exception {
        return getRepo().findAll();
    }

    @Override
    @Cacheable(value = "#{T(this).getCacheName()}", keyGenerator = "versionedKeyGenerator")
    public T readById(K k) throws Exception {
        return getRepo().findById(k).orElseThrow(() -> new Exception("ID NOT FOUND: " + k));
    }

    @Override
    @CacheEvict(value = "#{T(this).getCacheName()}", allEntries = true)
    public void delete(K k) throws Exception {
        getRepo().findById(k).orElseThrow(() -> new Exception("ID NOT FOUND: " + k));
        getRepo().deleteById(k);
    }

    protected String getCacheName() {
        return this.getClass().getSimpleName() + "Cache";
    }

}

Redis

127.0.0.1:6379> keys *
1) "#{T(this).getCacheName()}::realStateAPI:v1:CompanyServiceImpl:readAll"
2) "#{T(this).getCacheName()}::realStateAPI:v1:HousingServiceImpl:readAll"

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingquestionFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions