内容简介:在之前的文章中,讲解了使用redis解决集群环境session共享的问题这里我注入了一个StringRedisTemplate,其等价于RedisTemplate<String,String>,我们也可以自定义一个RedisTemplate,如下:本人觉得,完全没有必要自定义一个RedisTemplate,除非说有一些序列化上的需求。本文的讲解都是基于默认的StringRedisTemplate的。
在之前的文章中,讲解了使用 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数据结构的常用操作。接下来的会对其它的数据结构做进一步讲解。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 数据结构 – 用于构建文件系统的数据结构?
- 荐 用Python解决数据结构与算法问题(三):线性数据结构之栈
- 数据结构和算法面试题系列-C指针、数组和结构体
- 请问二叉树等数据结构的物理存储结构是怎样的?
- 数据结构——单链表
- 常用数据结构
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Making Things See
Greg Borenstein / Make / 2012-2-3 / USD 39.99
Welcome to the Vision Revolution. With Microsoft's Kinect leading the way, you can now use 3D computer vision technology to build digital 3D models of people and objects that you can manipulate with g......一起来看看 《Making Things See》 这本书的介绍吧!