安装redis,参考教程
懒人必备-windows 批处理命令
::启动redis的命令
@echo off
::这是简单的输出,相当于java中的system.out.println()
echo redis start......
::设置启动窗口的标题
title redis-server
::定义一个变量ENV_HOME
set ENV_HOME="D:\Program Files\Java\Redis"
D:
::设置启动窗口的颜色
color 0a
cd %ENV_HOME%
redis-server.exe redis.windows.conf
exit
代码集成
1.引入配置
在公共模块pom引用
<!-- 集成Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
若想指定版本号 在主模块引用
<!-- 集成Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>${redis.version}</version>
</dependency>
2.redis配置参数
yaml配置
spring:
redis:
#Redis服务器连接地址端口
host: 127.0.0.1
port: 6379
#Redis服务器连接密码(默认为空)
password: ""
jedis:
pool:
#连接池最大连接数(使用负值表示没有限制)
max-active: 8
#连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
#连接池中的最大空闲连接
max-idle: 8
#连接池中的最小空闲连接
min-idle: 0
#连接超时时间(毫秒)
timeout: 30000
3.测试
配置工具类
package com.ruoyi.common.utils.redis;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* @date 2021-08-15 18:44
*/
@Slf4j
@Component
public class ReidsCacheUtils {
@Resource
private StringRedisTemplate StringRedisTemplate;
private final String DEFAULT_KEY_PREFIX = "";
private final int EXPIRE_TIME = 1;
private final TimeUnit EXPIRE_TIME_TYPE = TimeUnit.DAYS;
/**
* 数据缓存至redis
*
* @param key
* @param value
* @return
*/
public <K, V> void add(K key, V value)
{
try
{
if (value != null)
{
StringRedisTemplate
.opsForValue()
.set(DEFAULT_KEY_PREFIX + key, JSON.toJSONString(value));
}
} catch (Exception e)
{
log.error(e.getMessage(), e);
throw new RuntimeException("数据缓存至redis失败");
}
}
/**
* 数据缓存至redis并设置过期时间
*
* @param key
* @param value
* @return
*/
public <K, V> void add(K key, V value, long timeout, TimeUnit unit)
{
try
{
if (value != null)
{
StringRedisTemplate
.opsForValue()
.set(DEFAULT_KEY_PREFIX + key, JSON.toJSONString(value), timeout, unit);
}
} catch (Exception e)
{
log.error(e.getMessage(), e);
throw new RuntimeException("数据缓存至redis失败");
}
}
/**
* 写入 hash-set,已经是key-value的键值,不能再写入为hash-set
*
* @param key must not be {@literal null}.
* @param subKey must not be {@literal null}.
* @param value 写入的值
*/
public <K, SK, V> void addHashCache(K key, SK subKey, V value)
{
StringRedisTemplate.opsForHash().put(DEFAULT_KEY_PREFIX + key, subKey, value);
}
/**
* 写入 hash-set,并设置过期时间
*
* @param key must not be {@literal null}.
* @param subKey must not be {@literal null}.
* @param value 写入的值
*/
public <K, SK, V> void addHashCache(K key, SK subKey, V value, long timeout, TimeUnit unit)
{
StringRedisTemplate.opsForHash().put(DEFAULT_KEY_PREFIX + key, subKey, value);
StringRedisTemplate.expire(DEFAULT_KEY_PREFIX + key, timeout, unit);
}
/**
* 获取 hash-setvalue
*
* @param key must not be {@literal null}.
* @param subKey must not be {@literal null}.
*/
public <K, SK> Object getHashCache(K key, SK subKey)
{
return StringRedisTemplate.opsForHash().get(DEFAULT_KEY_PREFIX + key, subKey);
}
/**
* 从redis中获取缓存数据,转成对象
*
* @param key must not be {@literal null}.
* @param clazz 对象类型
* @return
*/
public <K, V> V getObject(K key, Class<V> clazz)
{
String value = this.get(key);
V result = null;
if (!StringUtils.isEmpty(value))
{
result = JSONObject.parseObject(value, clazz);
}
return result;
}
/**
* 从redis中获取缓存数据,转成list
*
* @param key must not be {@literal null}.
* @param clazz 对象类型
* @return
*/
public <K, V> List<V> getList(K key, Class<V> clazz)
{
String value = this.get(key);
List<V> result = Collections.emptyList();
if (!StringUtils.isEmpty(value))
{
result = JSONArray.parseArray(value, clazz);
}
return result;
}
/**
* 功能描述:Get the value of {@code key}.
*
* @param key must not be {@literal null}.
* @return java.lang.String
* @date 2021/9/19
**/
public <K> String get(K key)
{
String value;
try
{
value = StringRedisTemplate.opsForValue().get(DEFAULT_KEY_PREFIX + key);
} catch (Exception e)
{
log.error(e.getMessage(), e);
throw new RuntimeException("从redis缓存中获取缓存数据失败");
}
return value;
}
/**
* 删除key
*/
public void delete(String key)
{
StringRedisTemplate.delete(key);
}
/**
* 批量删除key
*/
public void delete(Collection<String> keys)
{
StringRedisTemplate.delete(keys);
}
/**
* 序列化key
*/
public byte[] dump(String key)
{
return StringRedisTemplate.dump(key);
}
/**
* 是否存在key
*/
public Boolean hasKey(String key)
{
return StringRedisTemplate.hasKey(key);
}
/**
* 设置过期时间
*/
public Boolean expire(String key, long timeout, TimeUnit unit)
{
return StringRedisTemplate.expire(key, timeout, unit);
}
/**
* 设置过期时间
*/
public Boolean expireAt(String key, Date date)
{
return StringRedisTemplate.expireAt(key, date);
}
/**
* 移除 key 的过期时间,key 将持久保持
*/
public Boolean persist(String key)
{
return StringRedisTemplate.persist(key);
}
/**
* 返回 key 的剩余的过期时间
*/
public Long getExpire(String key, TimeUnit unit)
{
return StringRedisTemplate.getExpire(key, unit);
}
/**
* 返回 key 的剩余的过期时间
*/
public Long getExpire(String key)
{
return StringRedisTemplate.getExpire(key);
}
}
测试缓存
在登录请求里缓存账号密码
查看redis