title: SpringBoot Redis快速集成使用
tags: redis,springboot,redisTemplate
Redis数据类型
原生能力参考runoob教程
数据类型 | 存储的值 | 读写能力 |
---|---|---|
String | 可以是字符串、整数或浮点数,统称为元素 | 对字符串操作 对整数类型加减 |
List | 一个序列集合且每个节点都包好了一个元素 | 序列两端推入、或弹出元素 修剪、查找或移除元素 |
Set | 各不相同的元素 | 从集合中插入或删除元素 |
Hash | 有key-value的散列组,其中key是字符串,value是元素 | 按照key进行增加删除 |
Sort Set | 带分数的score-value有序集合,其中score为浮点,value为元素 | 集合插入,按照分数范围查找 |
SpringBoot 集成
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
application.yaml 文件
spring:
redis:
host: localhost
port: 6379
database: 0
RedisConfig.java
通过比较Jackson和FastJson后选择使用FastJson做value的序列化
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 使用 GenericFastJsonRedisSerializer 替换默认序列化
GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
// 设置key和value的序列化规则
redisTemplate.setKeySerializer(new GenericToStringSerializer<>(Object.class));
redisTemplate.setValueSerializer(genericFastJsonRedisSerializer);
// 设置hashKey和hashValue的序列化规则
redisTemplate.setHashKeySerializer(new GenericToStringSerializer<>(Object.class));
redisTemplate.setHashValueSerializer(genericFastJsonRedisSerializer);
// 设置支持事物
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
测试代码
RedisTests.java
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.*;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.*;
import static junit.framework.TestCase.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RedisTests {
/**
* 所有操作都可以通过redisTemplate来完成
*/
@Autowired
private RedisTemplate<String, String> redisTemplate;
/**
* String类型,value可以是String,Integer,Float,
*/
@Resource(name = "redisTemplate")
private ValueOperations<String, String> valueOperations;
/**
* List类型,从左或右推入、取出元素
* 可以做先进先出(队列)、先进后出操作
*/
@Resource(name = "redisTemplate")
private ListOperations<String, String> listOperations;
/**
* 存储不重复的元素
*/
@Resource(name = "redisTemplate")
private SetOperations<String, String> setOperations;
/**
* 存储键值对
*/
@Resource(name = "redisTemplate")
private HashOperations<String, String, String> hashOperations;
/**
* 有序集合,可以用于排序筛选操作
*/
@Resource(name = "redisTemplate")
private ZSetOperations<String, String> zSetOperations;
@Test
public void contextLoads() {
}
@Test
public void testString() {
String key = "string_key";
String value = "string_value";
// 添加
valueOperations.set(key, value);
// 查找
assertEquals(value, valueOperations.get(key));
// 删除
valueOperations.getOperations().delete(key);
// 批量添加
Map<String, String> map = new HashMap<>();
map.put(key, value);
valueOperations.multiSet(map);
// 批量查询
valueOperations.multiGet(Arrays.asList(key));
// 批量删除
valueOperations.getOperations().delete(Arrays.asList(key));
}
@Test
public void testList() {
String key = "list_key";
String value = "list_value";
// 从左侧添加一个元素
listOperations.leftPush(key, value);
// 获取指定索引位置的元素
assertEquals(value, listOperations.index(key, 0));
// 从右侧取出一个元素
assertEquals(value, listOperations.rightPop(key));
// 批量插入
listOperations.leftPushAll(key, value, value, value);
listOperations.leftPushAll(key, Arrays.asList(value, value, value));
// 删除所有值为value的元素
listOperations.remove(key, 0, value);
}
@Test
public void testSet() {
String key = "set_key";
String value = "set_value";
// 添加元素,可批量添加
setOperations.add(key, value);
// 查询元素是否存在
assertEquals(Boolean.TRUE, setOperations.isMember(key, value));
// 批量查询
Set<String> members = setOperations.members(key);
assertEquals(true, members.contains(value));
// 删除元素,可批量删除
setOperations.remove(key, value);
}
@Test
public void testHash() {
String key = "hash_key";
String hash_key = "hash_hash_key";
String hash_value = "hash_hash_value";
// 添加,修改
hashOperations.put(key, hash_key, hash_value);
// 查找
assertEquals(hash_value, hashOperations.get(key, hash_key));
// 删除,批量删除
hashOperations.delete(hash_key, hash_key);
// 批量添加
HashMap<String, String> map = new HashMap<>();
map.put(hash_key, hash_value);
hashOperations.putAll(key, map);
// 批量查找
hashOperations.multiGet(key, Arrays.asList(hash_key));
}
@Test
public void testZSet() {
String key = "set_key";
String value = "set_value";
// 添加元素
zSetOperations.add(key, value, 1.0);
// 删除元素
zSetOperations.remove(key, value);
}
}