相关疑难解决方法(0)

Spring分别缓存列表中的所有元素

我试图将缓存添加到CRUD应用程序,我开始执行以下操作:

@Cacheable("users")
List<User> list() {
    return userRepository.findAll()
}
@CachePut(value = "users", key = "#user.id") 
void create(User user) {
    userRepository.create(user)
}
@CachePut(value = "users", key = "#user.id") 
void update(User user) {
    userRepository.update(user)
}
@CacheEvict(value = "users", key = "#user.id") 
void delete(User user) {
    userRepository.delete(user)
}
Run Code Online (Sandbox Code Playgroud)

我的问题是我希望create / update / delete操作可以为该操作更新已存储在缓存中的元素list()(请注意,list()不是从数据库中拉出而是从数据引擎中拉出),但是我无法做到这一点。

我想缓存list()单独返回的所有元素,以便所有其他操作可以使用来更新缓存#user.id。或者,可能需要执行所有操作来更新已存储在缓存中的列表。

我读到我可以在更新整个缓存时逐出整个缓存,但是我想避免这样的事情:

@CacheEvict(value = "users", allEntries=true) 
void create(User user) {
    userRepository.create(user)
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以在缓存的集合中创建/更新/删除值?还是将集合中的所有值作为单独的键缓存?

java spring caching spring-boot spring-cache

7
推荐指数
2
解决办法
7615
查看次数

在采用带有数组或集合参数的方法上使用Spring Cache存在哪些策略?

我想使用Spring的Cache抽象将方法注释为@Cacheable。但是,某些方法被设计为采用参数的数组或集合并返回一个集合。例如,考虑使用以下方法查找实体:

public Collection<Entity> getEntities(Collection<Long> ids)
Run Code Online (Sandbox Code Playgroud)

从语义上讲,我需要Entity单独缓存对象(由id键),而不是基于整个ID的集合。类似于这个问题在问什么。

简单的Spring Memcached通过其支持了我想要的ReadThroughMultiCache,但是我想使用Spring的抽象来支持轻松更改缓存存储实现(Guava,Coherence,Hazelcast等),而不仅仅是memcached。

有哪些策略可以使用Spring Cache缓存这种方法?

spring caching spring-cache

6
推荐指数
1
解决办法
5142
查看次数

使用注解将所有返回的元素放入 Spring-Boot 缓存中

使用spring-boot及其缓存机制,是否可以自动将所有作为集合返回的实体一一存储到缓存中?

例如图片下面的 Repository 方法:

@Query("...")
List<Foo> findFooByBar(Bar bar);
Run Code Online (Sandbox Code Playgroud)

我想将它们一个接一个地插入到 Spring Cache 中,这意味着会有 N 个插入(列表中的每个元素一个)而不是一个(整个列表)。

例子:

@Query("...")
@CachePut(value = "foos", key = "result.each.id")
List<Foo> findFooByBar(Bar bar);
Run Code Online (Sandbox Code Playgroud)

java caching annotations spring-boot spring-cache

5
推荐指数
1
解决办法
4649
查看次数

如何在 Spring 中缓存对象列表?

我有一个名为 getUserById 的方法

@Cacheable(value = "Account", key = "#accountId")
public Account getUserById(Long accountId) {
Run Code Online (Sandbox Code Playgroud)

另一个名为 getUserByIds 的方法

@Cacheable(?????)
public List<Account> getUserByIds(List<Long> accountIds) {
Run Code Online (Sandbox Code Playgroud)

如何通过帐户 ID 缓存所有帐户?谢谢

spring spring-boot spring-cache

5
推荐指数
1
解决办法
1万
查看次数

Spring缓存不适用于findAll方法

我最近开始从一个方法缓存结果.我正在使用@Cacheable和@CachePut来实现所需的功能.

但不知何故,保存操作不会更新findAll方法的缓存.以下是相同的代码段:

@RestController
@RequestMapping(path = "/test/v1")
@CacheConfig(cacheNames = "persons")
public class CacheDemoController {

    @Autowired
    private PersonRepository personRepository;

    @Cacheable
    @RequestMapping(method = RequestMethod.GET, path="/persons/{id}")
    public Person getPerson(@PathVariable(name = "id") long id) {
        return this.personRepository.findById(id);
    }

    @Cacheable
    @RequestMapping(method = RequestMethod.GET, path="/persons")
    public List<Person> findAll() {
        return this.personRepository.findAll();
    }

    @CachePut
    @RequestMapping(method = RequestMethod.POST, path="/save")
    public Person savePerson(@RequestBody Person person) {
        return this.personRepository.save(person);
    }
}
Run Code Online (Sandbox Code Playgroud)

对于第一次调用findAll方法,它将结果存储在"person"缓存中,对于所有后续调用,即使在其间执行了save()操作,它也会返回相同的结果.

我对缓存很新,所以对此的任何建议都会有很大的帮助.

谢谢!

caching spring-cache

4
推荐指数
1
解决办法
2933
查看次数

标签 统计

spring-cache ×5

caching ×4

spring ×3

spring-boot ×3

java ×2

annotations ×1