springboot整合redis系列集合
- 1.springboot整合redis序列化(3步搞定)
- 2.springboot整合redis发布订阅(4步搞定)
- 3.springboot整合redis五种数据结构API - 本文
- 4.springboot整合redis分布式锁
springboot整合redis五种数据结构API
pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
1、string(字符串)类型
使用场景:key-value缓存、计数
操作对象:redisTemplate.opsForValue()
添加数据:set(Object k, Object v);
获取数据:get(Object k);
获取数据长度:size(Object k);
拼接内容:append(Object k, String s);
数值加一:increment(Object k);
数值减一:decrement(Object k);
package com.phy.demo;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void test() {
// 添加redis 字符类型数据 strKey1
redisTemplate.opsForValue().set("strKey1","一段话。。。");
// 添加redis 字符类型数据 strKey2
JSONObject json = new JSONObject();
json.put("dog","狗");
json.put("cat"<