我对 Spring Boot 应用程序中的缓存有疑问。我有一个简单的 Web 应用程序,我在其中管理一个实体,使用 Spring Data JPA、JCache 和 Hazelcast 实现。我创建了两个缓存:
- 一个通过 id 缓存实体
- 另一种是实体字段值和实体列表作为缓存值
当我从系统中删除单个值时,我只是从第一个缓存中逐出该值,如果存在具有相应键(字段值)的记录,则从第二个缓存中逐出整个实体列表。我的问题:是否有任何解决方案不从第二个缓存中删除整个记录而是更新它,只需从缓存值列表中删除一个实体?
例子:
import com.google.gwt.thirdparty.guava.common.collect.Lists;
import com.test.mmm.jcache.entity.Person;
import com.test.mmm.jcache.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PersonService {
private final PersonRepository personRepository;
@Caching(put = @CachePut(value = "persons", key = "#person.id"),
evict = @CacheEvict(value = "personsByLastName", key = "#person.lastName"))
public Person save(Person person) {
return personRepository.save(person);
}
@Caching(evict = {
@CacheEvict(value = "persons", key = "#person.id"),
@CacheEvict(value = "personsByLastName", key = "#person.lastName")
})
public void delete(Person person) {
personRepository.delete(person.getId());
}
@Cacheable(value = "persons", key = "#id")
public Person findOne(Long id) {
return personRepository.findOne(id);
}
@Cacheable(value = "personsByLastName", key = "#lastName")
public List<Person> findByLastName(String lastName) {
return personRepository.findByLastName(lastName);
}
public List<Person> findAll() {
return Lists.newArrayList(personRepository.findAll());
}
@Autowired
public PersonService(PersonRepository personRepository) {
this.personRepository = personRepository;
}
}