【快学springboot】13.操作redis之String数据结构
前言
在之前的文章中,讲解了使用redis解决集群环境session共享的问题【快学springboot】11.整合redis实现session共享,这里已经引入了redis相关的依赖,并且通过springboot的配置,实现了session共享。下面,我们就通过springboot提供的RedisTemplate来操作redis。
注入RedisTemplate
@Autowired
private StringRedisTemplate redisTemplate;
这里我注入了一个StringRedisTemplate,其等价于RedisTemplate<String,String>,我们也可以自定义一个RedisTemplate,如下:
@Configuration
public class RedisTemplateConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置连接工厂
template.setConnectionFactory(factory);
return template;
}
}
本人觉得,完全没有必要自定义一个RedisTemplate,除非说有一些序列化上的需求。本文的讲解都是基于默认的StringRedisTemplate的。
设置/获取值
我们可以通过opsForValue().set(k,v)方法设置一个值opsForValue().get(k)方法获取值
@Test
public void testsetAndGetString() {
redisTemplate.opsForValue().set("name", "happyjava");
String name = redisTemplate.opsForValue().get("name");
System.out.println(name);
}
执行结果:
查看redis数据库上的值,如下:
设置值并且同时设置过期时间
opsForValue().set方法还支持同时设置键对应的过期时间
@Test
public void testSetWithExpireTime() {
redisTemplate.opsForValue().set("name2", "happyjava2", 10, TimeUnit.SECONDS);
String name2 = redisTemplate.opsForValue().get("name2");
System.out.println(name2);
}
执行结果:
happyjava2
获取键的过期时间
我们可以通过redisTemplate.getExpire方法获得键的过期时间
@Test
public void testSetWithExpireTime() {
redisTemplate.opsForValue().set("name2", "happyjava2", 10, TimeUnit.SECONDS);
String name2 = redisTemplate.opsForValue().get("name2");
System.out.println(name2);
Long expire = redisTemplate.getExpire("name2", TimeUnit.SECONDS);
System.out.println(expire);
}
执行结果如下:
happyjava2
9
设置键的过期时间
我们可以通过redisTemplate.expire方法设置键的过期时间
@Test
public void testSetExpire() {
redisTemplate.expire("name",120,TimeUnit.SECONDS);
Long expire = redisTemplate.getExpire("name", TimeUnit.SECONDS);
System.out.println(expire);
}
之前设置了name是非过期的,这里给它设置个过期时间。执行结果如下:
119
getAndSet
我们可以通过opsForValue().getAndSet方法获取此时的值,然后设置一个新的值。
@Test
public void test() {
redisTemplate.opsForValue().set("name", "123456");
String name = redisTemplate.opsForValue().getAndSet("name", "happyjava3");
System.out.println(name);
name = redisTemplate.opsForValue().get("name");
System.out.println(name);
}
输出结果如下:
123456
happyjava3
append追加
通过redisTemplate.opsForValue().append方法可以追加内容。
@Test
public void test() {
redisTemplate.opsForValue().append("name","append");
String name = redisTemplate.opsForValue().get("name");
System.out.println(name);
}
这里向之前的name键追加了一个字符串“append”,输出结果如下:
happyjava3append
自增
自增是redis里非常常用的方法,常常用该方法来实现计数器。我们可以通过redisTemplate.opsForValue().increment方法实现自增
@Test
public void test() {
Long count = redisTemplate.opsForValue().increment("count");
System.out.println(count);
Long count1 = redisTemplate.opsForValue().increment("count", 11);
System.out.println(count1);
}
如果键不存在,则会默认从0开始自增,我们也可以设置自增的值的大小。
自减
我们可以通过redisTemplate.opsForValue().decrement方法来实现自减
@Test
public void test() {
Long count = redisTemplate.opsForValue().decrement("count");
System.out.println(count);
Long count1 = redisTemplate.opsForValue().decrement("count", 10);
System.out.println(count1);
}
如果存在则设置/如果不存在则设置
setIfAbsent:如果不存在,则设置。
并且可以通过重载的方法设置过期时间,这个方法是很重要的,可以基于该方法实现一个分布式锁。
setIfPresent:如果存在,则设置。
@Test
public void test() {
Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("name", "happy");
System.out.println(aBoolean);
Boolean aBoolean1 = redisTemplate.opsForValue().setIfPresent("name", "happy2");
System.out.println(aBoolean1);
}
因为之前已经存在name的值,该代码的预期输出结果是false true。
总结
这里介绍了redis string数据结构的常用操作。接下来的会对其它的数据结构做进一步讲解。
【快学springboot】13.操作redis之String数据结构的更多相关文章
- 【快学springboot】14.操作redis之list
前言 之前讲解了springboot(StringRedisTemplate)操作redis的string数据结构,这篇文章将会讲解list数据结构 list数据结构具有的操作 下图列出了redis ...
- 【快学SpringBoot】Spring Cache+Redis实现高可用缓存解决方案
前言 之前已经写过一篇文章介绍SpringBoot整合Spring Cache,SpringBoot默认使用的是ConcurrentMapCacheManager,在实际项目中,我们需要一个高可用的. ...
- 【快学springboot】8.JPA乐观锁OptimisticLocking
介绍 当涉及到企业应用程序时,正确地管理对数据库的并发访问是至关重要的.为此,我们可以使用Java Persistence API提供的乐观锁定机制.它导致在同一时间对同一数据进行多次更新不会相互干扰 ...
- Python操作redis字符串(String)详解 (三)
# -*- coding: utf-8 -*- import redis #这个redis不能用,请根据自己的需要修改 r =redis.Redis(host=") 1.SET 命令用于设置 ...
- 【快学springboot】12.实现拦截器
前言 之前在[快学springboot]6.WebMvcConfigurer配置静态资源和解决跨域里有用到WebMvcConfigurer接口来实现静态资源的映射和解决跨域请求,并且在文末还说了Web ...
- 【快学springboot】4.接口参数校验
前言 在开发接口的时候,参数校验是必不可少的.参数的类型,长度等规则,在开发初期都应该由产品经理或者技术负责人等来约定.如果不对入参做校验,很有可能会因为一些不合法的参数而导致系统出现异常. 上一篇文 ...
- PHP操作redis之String(字符串)、List(列表)(一)
Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key – value 缓存产品有以下三个特点: Redis支持数据的持久 ...
- 【快学springboot】11.整合redis实现session共享
前言 这里都是基于前面的项目基础上的.springboot整合redis非常的方便,这也是springboot的宗旨,简化配置.这篇文章就教大家如何使用springboot整合redis来实现sess ...
- 【快学SpringBoot】快速上手好用方便的Spring Cache缓存框架
前言 缓存,在开发中是非常常用的.在高并发系统中,如果没有缓存,纯靠数据库来扛,那么数据库压力会非常大,搞不好还会出现宕机的情况.本篇文章,将会带大家学习Spring Cache缓存框架. 原创声明 ...
随机推荐
- about Base64
用webservice传送文件的时候发现,如果发送的文件中有0x00字符,会被认为是字符串结尾,后面的内容就发送不过去,因此需要对不是纯文本格式的文件做BASE64编码,这样文件中就不会有0x00这样 ...
- MYSQL数据库索引、事务。
=============================================================================================== inno ...
- 不要在mutation回调函数之外,修改vuex仓库里属性的状态
[vuex] do not mutate vuex store state outside mutation handlers. import * as types from './mutation- ...
- ASP.NET Core搭建多层网站架构【5-网站数据库实体设计及映射配置】
2020/01/29, ASP.NET Core 3.1, VS2019, EntityFrameworkCore 3.1.1, Microsoft.Extensions.Logging.Consol ...
- /var/lib/gems/2.5.0/gems/seccomp-tools-1.3.0/lib/seccomp-tools/dumper.rb:125: warning: Insecure world writable dir /home/python/.local in PATH, mode 040777 解决方案
/var/lib/gems/2.5.0/gems/seccomp-tools-1.3.0/lib/seccomp-tools/dumper.rb:125: warning: Insecure worl ...
- dwz中的(tree)树形菜单的默认收缩
做网站后台时,为了方便管理,可能会用到dwz中的树形菜单,如下: 树形菜单的收缩有默认属性值,可以对其进行一定的初始设定: DWZ的树结构是按<ul>,<li>的嵌套格式构成, ...
- 利用 systemd 实现 Clash 开机自启
利用 systemd 实现 Clash 开机自启 首先准备 Clash 的服务脚本,并保存为/etc/systemd/system/clash.service.内容如下: [Unit] Descrip ...
- WordPress 网站迁移
最近想把本地的WordPress迁移到我的Linux虚拟机里面,是不是很无聊,哈哈哈,接下来就是一过程了,其实这个和迁移到线上是一样的, 1.首先将本地的文件WordPress通过FTP传到虚拟机上: ...
- Spring Boot 整合MaBatis如何在控制台打印执行的SQL语句
yml文件:logging: level: com.XXX.Mapper: debug (红色部分为Dao层的包名,注意不是XML文件的包名) properties文件: logging.level. ...
- CRS-1硬件维护
一.CRS-1硬件介绍CRS-1 路由器是Cisco 推出的新的大容量骨干路由器,是一个支持多机箱扩展的路由系统.其设计容量可以扩展至72 个线卡机箱.8 个矩阵机箱,总交换容量达到92Tbps,具有 ...