SpringBoot简单整合Redis
REmote DIctionary Server(Redis) 是一个由 Salvatore Sanfilippo 写的 key-value 存储系统,是跨平台的非关系型数据库。
Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库,并提供多种语言的 API。
Redis 通常被称为数据结构服务器,因为值(value)可以是字符串(String)、哈希(Hash)、列表(list)、集合(sets)和有序集合(sorted sets)等类型。
阅读本教程前,你需要了解基本的数据结构,例如以下几种:
String: 字符串
Hash: 散列
List: 列表
Set: 集合
Sorted Set: 有序集合
在阅读本教程前,你需要了解基本的数据结构。
maven
<!--集成redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
Application.propertis配置Reids
#Redis配置
spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
#Redis服务器连接密码(默认为空) 因为我没有设置密码,所以这里为空
spring.redis.password=
#连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.pool.max-idle=8
#连接池中的最小空闲连接
spring.redis.pool.min-idle=0
#连接超时时间(毫秒)
spring.redis.timeout=20000
//注意在用SpringBoot 执行Redis语句时不能去写Redis客户端,否则会报错!
别的博主都没有提这个。。可能只有我这种犯错误
-------下面我们看一下RedisTemplate的源码,如果没有!
可以看到如果没有自定义序列化或是默认使用JDK序列化的方式!
--------这样的话在Redis就会乱码,所以我们配置一个自己的RedisTemplate
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
还可以把经常使用的数据的操作封装成一个类,方便使用!这里我还没写!下次更新的时候补上!
*今天把Redis简答的Crud封装成一个工具类!*
package com.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* * Redis工具类,简单的Crud操作
* */
@Component
public class RedisTool {
@Autowired
RedisTemplate redisTemplate;
/**
* String类的存值
*
* **/
public void SetString(String Key,Object Value){
redisTemplate.opsForValue().set(Key,Value);
}
/**
* String取值
*
* **/
public String GetString(Object Key){
return (String) redisTemplate.opsForValue().get(Key);
}
/**
* List向左边边插入
*
* **/
public void SetLeftList(String Key,Object Value){
redisTemplate.opsForList().leftPush(Key,Value);
}
/**
* List向右边插入
*
* **/
public void SetRightList(String Key,Object Value){
redisTemplate.opsForList().rightPush(Key,Value);
}
/**
* List向左边批量插入数据
*
* **/
public Long SetLeftListMore(String Key, List<Object>Value){
return redisTemplate.opsForList().leftPushAll(Key,Value);
}
/**
* List向右批量插入数据
*
* **/
public Long SetRightListMore(String Key, List<Object>Value){
return redisTemplate.opsForList().rightPush(Key,Value);
}
/**
* 通过返回所有List中值
*
* **/
public List<Object> SetLeftListMore(String Key){
return redisTemplate.opsForList().range
("Key", 0,redisTemplate.opsForList().size("Key")-1);
}
/**
* 写入redis缓存(不设置expire存活时间)
* @param key
* @param value
* @return
*/
public boolean set(final String key, String value){
boolean result = false;
try {
ValueOperations operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
System.out.println("写入redis缓存失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* 写入redis缓存(设置expire存活时间)
* @param key
* @param value
* @param expire
* @return
*/
public boolean set(final String key, String value, Long expire){
boolean result = false;
try {
ValueOperations operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
System.out.println("写入redis缓存(设置expire存活时间)失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* 读取redis缓存
* @param key
* @return
*/
public Object get(final String key){
Object result = null;
try {
ValueOperations operations = redisTemplate.opsForValue();
result = operations.get(key);
} catch (Exception e) {
System.out.println("读取redis缓存失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* 判断redis缓存中是否有对应的key
* @param key
* @return
*/
public boolean exists(final String key){
boolean result = false;
try {
result = redisTemplate.hasKey(key);
} catch (Exception e) {
System.out.println("判断redis缓存中是否有对应的key失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* redis根据key删除对应的value
* @param key
* @return
*/
public boolean remove(final String key){
boolean result = false;
try {
if(exists(key)){
redisTemplate.delete(key);
}
result = true;
} catch (Exception e) {
System.out.println("redis根据key删除对应的value失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* redis根据keys批量删除对应的value
* @param keys
* @return
*/
public void remove(final String... keys){
for(String key : keys){
remove(key);
}
}
}