Redis 和 spring 或 spring boot 一起使用时默认的缓存策略是什么? [英] What is the default cache strategy when using Redis with spring or spring boot?

查看:49
本文介绍了Redis 和 spring 或 spring boot 一起使用时默认的缓存策略是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有多种缓存策略,例如:Cache Aside、Read Through、Write Through、Write behind、Write Around.

There are various cache strategies like: Cache Aside, Read Through, Write Through, Write Behind, Write Around.

当使用spring-boot-starter-data-redis依赖项与spring boot一起使用Redis时,默认缓存策略是什么.以及如何改变这种情况.任何参考将不胜感激.

When Redis is used with spring boot using spring-boot-starter-data-redis dependency, what is the default Cache Strategy. And how can this be changed. Any reference will be greatly appreciated.

推荐答案

默认情况下,您将缓存放在一边,我们在 Spring boot 应用程序中的缓存使用看起来与此类似

By default, you get cache aside, our cache usage in the Spring boot app looks something similar to this

@Cacheable(cacheNames = "someCache")
public String cacheThis(String id){
    return "this Is it";
}

在spring boot app的大部分场景中,我们都会缓存JPA或者其他DB查询的结果.在这种情况下,我们在查询方法上添加 Cacheable,这为我们提供了缓存功能.

In most of the scenarios in the spring boot app, we cache the result of JPA or other DB queries. In such cases, we add Cacheable on the query method, which gives us cache aside feature.

应用程序可以模拟读取缓存的功能通过实施缓存侧策略.此策略加载数据按需进入缓存.

An application can emulate the functionality of read-through caching by implementing the cache-aside strategy. This strategy loads data into the cache on demand.

参考:https://docs.microsoft.com/en-us/azure/architecture/patterns/cache-aside

使用缓存搁置模式并不是问题的总解决方案,根据您的用例,您可能需要更改缓存策略.除了我们从 Spring 框架中知道的一些注释(如

Using cache aside pattern is not the always solution to a problem, depending on your use case you might have to change the caching strategy. Changing caching strategy is not straight forward except some annotations we know from the Spring framework like

  • 可缓存
  • CacheEvict
  • 缓存放置

您需要更新应用程序代码以使用其他缓存策略,但您可以使用这些注释构建任何缓存策略.如果你不喜欢使用这些注解,那么就使用实际的缓存对象,你可以随时调用 Cache 方法来修改缓存.

You need to update your application code to use other caching strategies, though you can build any caching strategy using these annotations. If you don't like to use these annotations then play with the actual cache object, at any time you can call Cache methods to modify the cache.

例如

Cache myCache = cacheManager.getCache("myCache"); 

一旦有了缓存对象,就可以调用所有相关的方法,由于底层缓存的限制,有些方法可能无法正常工作.

Once you have a cache object, you can call all relevant methods, some methods may not work as expected due to the limitation of the underlying cache.

这篇关于Redis 和 spring 或 spring boot 一起使用时默认的缓存策略是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆