Redis 是一个数据库,存放的数据结构是:key-value 这种键值对。例如:
name:“张三”
age:18
sex:true
parent: ZhangSanFatherObject
-
下载并安装 Redis.首先,redis 是一个数据库,需要有专门的运行环境,因此需要在电脑上安装redis服务来提供运行环境。
在我本机上,将其安装到了目录 D:softwareredis,安装时其他选项都默认即可。 -
注册为 Windows 服务并启动。上一步已经安装完成,接下来将运行环境启动,有两种方式启动:
-
方式一. 临时启动。进入 Redis 安装文件下,打开 cmd, 执行服务:redis-server.exe redis.windows.conf.出现窗口则成功了这个是一个临时启动的服务
-
方式二. 开机自启动(推介)。
-
- 安装目录下执行注册命令:redis-server.exe --service-install redis.windows.conf --Service-name RedisServer1 --loglevel verbose
执行完成后,在系统服务中就会出现一个 redis.有的电脑可能可能会注册失败,那么需要停止启动的临时 redis 服务,并使用管理员权限打开 cmd,再次执行。
- 安装目录下执行注册命令:redis-server.exe --service-install redis.windows.conf --Service-name RedisServer1 --loglevel verbose
-
- 服务注册成功后,点击启动即可.
- 服务注册成功后,点击启动即可.
-
在上一步,我们所有的 Key-value 值 都只能在 cmd 中查看与修改,很麻烦,所以安装图形化界面便于操作:
AnotherRedisDesktopManager
我们安装图形化界面后,新建一个连接,填写如下内容:
- host:127.0.0.1 (环境默认地址)
- port: 6379 (环境默认端口)
- password: (环境默认为空,无需添加)。
- name 可以随便填写
- 底部的auth单选框,无需勾选。
到此为止,已经完成了所有环境的安装。
在图形化界面中操作一下,存入数据,读取数据(新增一条数据,在左侧侧边栏就会显示一条):
redis 服务指令- 启动服务: redis-server.exe --service-start
- 停止服务: redis-server.exe --service-stop
- 卸载服务: redis-server.exe --service-uninstall RedisServer1
------ 如果文章对你有用,感谢文章顶部 >>> 点赞 | 收藏
在 SpringBoot 中集成 Redis
上面已经完成了运行环境的安装,那么在实际项目开发中,如何使用Redis,并进行练习开发呢?一共有3步 项目源码
- 1.pom.xml 中添加 redis 坐标
- 2.添加配置项
- 3.写个测试类运行一下
- pom.xml 中添加 redis 坐标
org.springframework.boot spring-boot-starter-data-redis org.apache.commons commons-pool2 2.6.1
- 添加配置项
spring:
redis:
host: 127.0.0.1
port: 6379
# 数据库索引
database: 0
password:
timeout: 2000
lettuce:
pool:
# 连接池中的最小空闲连接
min-idle: 5
# 连接池中的最大空闲连接
max-idle: 10
# 连接池的最大数据库连接数
max-active: 20
# #连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1ms
- 测试类运行一下。运行成功后,再去看看管理界面,可以看到数据李四已经被成功添加(可能会出现中文乱码问题,不用担心先跳过)
package com.springblog;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
public class RedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
void testSetValue() {
redisTemplate.opsForValue().set("name","李四");
}
}
springboot redis 高级 JsonRedisSerializer
我们已经知道 redis 只能存取 key-value 的键值对这种结构的数据。
如果数据值 value 不是基本数据类型,而是某个 Class 类,那么为了存取这种复杂的 value 数据,需要怎么做呢?
对 Redis 做一些序列化配置
一共有 3 个文件 + 2 个坐标, 写好之后,往后余生都不用再更改(可复制粘贴)
- pom 坐标
- RedisConfig 配置文件
- JsonRedisSerializer 序列化
- RedisUtils 便于使用而封装的工具类
pom 坐标
com.alibaba fastjson 1.2.47 com.google.code.gson gson 2.8.5
RedisConfig 配置文件
@Configuration
public class RedisConfig {
@Resource
private RedisConnectionFactory factory;
@Bean
public RedisTemplate redisTemplate() {
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JsonRedisSerializer<>(Object.class));
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new JsonRedisSerializer<>(Object.class));
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
}
JsonRedisSerializer 序列化
public class JsonRedisSerializerimplements RedisSerializer { private static ParserConfig defaultRedisConfig = new ParserConfig(); static { defaultRedisConfig.setAutoTypeSupport(true); } private Class type; public JsonRedisSerializer(Class type) { this.type = type; } @Override public byte[] serialize(T t) throws SerializationException { if (t == null) { return new byte[0]; } return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(IOUtils.UTF8); } @Override public T deserialize(byte[] bytes) throws SerializationException { if (bytes == null || bytes.length <= 0) { return null; } String str = new String(bytes, IOUtils.UTF8); return JSON.parseObject(str, type, defaultRedisConfig); } }
RedisUtils 便于使用而封装的工具类
package com.springblog.common.redis;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtils {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
public final static long DEFAULT_EXPIRE = 60 * 60 * 24L;
public final static long CAPTCHA_EXPIRE = 60 * 5L;
public final static long HOUR_ONE_EXPIRE = 60 * 60 * 1L;
public final static long HOUR_SIX_EXPIRE = 60 * 60 * 6L;
public final static long MONTH_ONE_EXPIRE = 60 * 60 * 24 * 30L;
public final static long NOT_EXPIRE = -1L;
private final static Gson gson = new Gson();
public void set(String key, Object value, long expire) {
if (!StringUtils.isBlank(key) && value != null) {
redisTemplate.opsForValue().set(key, value);
}
if (expire != NOT_EXPIRE) {
expire(key, expire);
}
}
public void set(String key, Object value) {
set(key, value, NOT_EXPIRE);
}
public Object get(String key, long expire) {
Object value = redisTemplate.opsForValue().get(key);
if (expire != NOT_EXPIRE) {
expire(key, expire);
}
return value;
}
public T get(String key, Class clazz, long expire) {
String value = stringRedisTemplate.opsForValue().get(key);
if (expire != NOT_EXPIRE) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value == null ? null : fromJson(value, clazz);
}
public T get(String key, Class clazz) {
return get(key, clazz, NOT_EXPIRE);
}
public Object get(String key) {
return get(key, NOT_EXPIRE);
}
public Set keys(String pattern) {
return redisTemplate.keys(pattern);
}
public void deleteByPattern(String pattern) {
redisTemplate.delete(keys(pattern));
}
public void delete(String key) {
redisTemplate.delete(key);
}
public void delete(Collection keys) {
redisTemplate.delete(keys);
}
public Object hGet(String key, String field) {
return redisTemplate.opsForHash().get(key, field);
}
public Map hGetAll(String key) {
HashOperations hashOperations = redisTemplate.opsForHash();
return hashOperations.entries(key);
}
public Set
在最后,让我们编写一个测试类,感受使用 Redis 存取复杂类型
package com.springblog;
import com.springblog.common.redis.RedisUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
public class RedisTest {
@Autowired
private RedisUtils redisUtils;
@Test
void testSetValueSerializer() {
Father father = new Father("李四的爹");
redisUtils.set("father", father);
Father getValue = redisUtils.get("father",Father.class);
System.out.println(getValue.getName());
}
}
class Father {
private String name;
Father(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}



