Spring JCache与Hazelcast数据驱逐

时间:2016-07-22 14:45:19

标签: java spring-boot hazelcast jcache

我对Spring启动应用程序中的缓存有疑问。我有简单的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;
    }
}

0 个答案:

没有答案