- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章SpringCache之 @CachePut的使用由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
使用CachePut注解,该方法每次都会执行,会清除对应的key值得缓存(或者更新), 。
如果返回值null,下次进行该key值查询时,还会查一次数据库,此时相当于@CacheEvict注解,
如果返回值不为null,此时会进行该key值缓存的更新,更新缓存值为返回的数据; 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//使用Redis缓存
@Cacheable
(value=
"Manager"
,key=
"#id"
)
public
User findById(Integer id) {
System.out.println(
"---查数据库DB-----"
);
return
userMapper.selectByPrimaryKey(id);
}
@CachePut
(value=
"Manager"
,key=
"#manager.getId()"
)
//@CacheEvict(value="Manager",key="#manager.getId()")//清除数据
public
User update(User manager) {
userMapper.updateByPrimaryKeySelective(manager);
//return userMapper.selectByPrimaryKey(manager.getId());
return
null
;
}
|
先进行数据id为1的查询,发现下次查询id为1的数据不会再查询DB,直接走缓存; 。
此时进行id为1数据更新操作,并且返回值为null; 。
进行id为1的数据查询,发现此时id为1缓存不存在,进行DB查询,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//使用Redis缓存
@Cacheable
(value=
"Manager"
,key=
"#id"
)
public
User findById(Integer id) {
System.out.println(
"---查数据库DB-----"
);
return
userMapper.selectByPrimaryKey(id);
}
@CachePut
(value=
"Manager"
,key=
"#manager.getId()"
)
//@CacheEvict(value="Manager",key="#manager.getId()")//清除数据
public
User update(User manager) {
userMapper.updateByPrimaryKeySelective(manager);
return
userMapper.selectByPrimaryKey(manager.getId());
//return null;
}
|
先进行数据id为1的查询,发现下次查询id为1的数据不会再查询DB,直接走缓存; 。
此时进行id为1数据更新操作,返回值不为null; 。
进行id为1的数据查询,发现此时id为1缓存被更新为更新的数据,且没有进行DB查询操作,
补充:@CachePut和@Cacheable的区别 。
@CachePut负责增加缓存 。
@Cacheable负责查询缓存,如果没查到,则将执行方法,并将方法的结果增加到缓存 。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我。如有错误或未考虑完全的地方,望不吝赐教.
原文链接:https://blog.csdn.net/MrLiar17/article/details/88253702 。
最后此篇关于SpringCache之 @CachePut的使用的文章就讲到这里了,如果你想了解更多关于SpringCache之 @CachePut的使用的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在使用 Spring 4 和 Hazelcast 3.2。我正在尝试使用以下代码向现有缓存添加新记录。不知何故缓存没有得到更新,同时我也没有看到任何错误。下面是代码片段供引用。 注意:- Cach
我想使用CachePut()。我特别想要 CachePut(id = _attr.path, value = attr.qryPath, region = variables.cacheRegion
我理解@Cacheput无论如何都会调用该方法并更新缓存中的结果?那么既然每次都会调用这个方法,为什么我们首先需要这个注解呢? 最佳答案 考虑一个 API,它使用 @Cacheable 注释简单地从数
下面是我的代码片段。 @cacheput 仅在第一次调用时将数据插入到 redis 中。第二次调用保存函数时不更新值。引用中的 CacheManager 是 RedisCacheManager。 @C
我正在尝试更新 spring 缓存中的值,但是 @CachePut 不会替换实际值,而是使用相同的键放置另一个值: 缓存名称:noTimeCache: 键:SYSTEM_STATUS,值:ON 键:S
我使用 Spring 以及使用 @Cacheable 和 @CachePut 注释的内置缓存。 我的 @Service 中有 2 个方法,第一个用于将值保存在数据库中,第二个用于从数据库获取值。它们都
我正在学习 Spring Boot 缓存,以便将这个概念应用到我们组织的项目中,并且我制作了一个名为员工缓存的示例项目。我的 Controller 和服务组件中有四个方法 insert、update、
public interface MyRespository extends CrudRepository { @CachePut(value = "mycache", key = "id")
注释介绍 @Cacheable @Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 @Cacheable 作用和配置方法
这是我的 ProductServiceImpl 类。 public class ProductServiceImpl implements ProductService { @Autowired G
( Will Spring's @CachePut annotation work with a void return type? ) 我遇到了同样的问题 因为这个问题已经很久了,不知道有没有解决办
@CachePut or @Cacheable(value = "CustomerCache", key = "#id") public Customer updateCustomer(Custome
maven项目中在pom.xml中依赖2个jar包,其他的spring的jar包省略: ?
我有一个存储简单 POJO 的映射,关键是 POJO 的 id 字段。从Spring的@CachePut注释中,我期望这样的东西: JobModel jm = new JobModel(); cach
在spring MVC项目中,我尝试使用@CachePut来缓存数据,但是在redis中,key和value有两个独立的数据: 同时对springboot项目做了同样的操作,得到了正常的结果: spr
我正在尝试使用 Ehcache 和 Spring 3.1 内置缓存注释(@Cacheable、@CacheEvict 和 @CachePut)在我的应用程序中实现缓存。 我创建了一个缓存如下: @Ca
我读到的越多,我对它的了解就越少:)。 只有一个简单的问题,如果我希望使用更新中的新内容刷新 listAll,则在 update 方法中使用什么?什么是更安全/更好的实践,逐出或缓存?这让我有点困惑,
我有一些学生,我想将他们同时保存在数据库和 Redis 缓存中以提高性能。 我有学生 POJO: @NoArgsConstructor @AllArgsConstructor @Builder @Da
我尝试了一些新的 Spring 特性,发现 @CachePut 和 @CacheEvict 注释没有效果。可能是我做错了什么。你能帮帮我吗? 我的 applicationContext.xml。
我是一名优秀的程序员,十分优秀!