windows 安装 Redis
Redis 是一个数据库,存放的数据结构是:key-value 这种键值对 。例如: name:“张三” age:18 sex:true parent: ZhangSanFatherObject
-
下载并安装 Redis.首先,redis 是一个数据库,需要有专门的运行环境,因此需要在电脑上安装redis服务来提供运行环境。 在我本机上,将其安装到了目录 D:\software\redis ,安装时其他选项都默认即可。 -
注册为 Windows 服务并启动。上一步已经安装完成,接下来将运行环境启动,有两种方式启动:
windows 安装 Redis 图形化界面管理器
在上一步,我们所有的 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 坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.1</version>
</dependency>
- 添加配置项
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 坐标
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
RedisConfig 配置文件
@Configuration
public class RedisConfig {
@Resource
private RedisConnectionFactory factory;
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> 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 JsonRedisSerializer<T> implements RedisSerializer<T> {
private static ParserConfig defaultRedisConfig = new ParserConfig();
static {
defaultRedisConfig.setAutoTypeSupport(true);
}
private Class<T> type;
public JsonRedisSerializer(Class<T> 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<String, Object> 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> T get(String key, Class<T> 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> T get(String key, Class<T> clazz) {
return get(key, clazz, NOT_EXPIRE);
}
public Object get(String key) {
return get(key, NOT_EXPIRE);
}
public Set<String> 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<String> keys) {
redisTemplate.delete(keys);
}
public Object hGet(String key, String field) {
return redisTemplate.opsForHash().get(key, field);
}
public Map<String, Object> hGetAll(String key) {
HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
return hashOperations.entries(key);
}
public Set<Object> hGetField(String key) {
Set<Object> fieldSet = redisTemplate.opsForHash().keys(key);
return fieldSet;
}
public void hMSet(String key, Map<String, Object> map) {
hMSet(key, map, NOT_EXPIRE);
}
public void hMSet(String key, Map<String, Object> map, long expire) {
redisTemplate.opsForHash().putAll(key, map);
if (expire != NOT_EXPIRE) {
expire(key, expire);
}
}
public void hSet(String key, String field, Object value) {
hSet(key, field, value, NOT_EXPIRE);
}
public void hSet(String key, String field, Object value, long expire) {
redisTemplate.opsForHash().put(key, field, value);
if (expire != NOT_EXPIRE) {
expire(key, expire);
}
}
public void expire(String key, long expire) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
public void hDel(String key, Object... fields) {
redisTemplate.opsForHash().delete(key, fields);
}
public void leftPush(String key, Object value) {
leftPush(key, value, NOT_EXPIRE);
}
public void leftPush(String key, Object value, long expire) {
redisTemplate.opsForList().leftPush(key, value);
if (expire != NOT_EXPIRE) {
expire(key, expire);
}
}
public Object rightPop(String key) {
return redisTemplate.opsForList().rightPop(key);
}
public void addToSet(String key, long expire, String... value) {
stringRedisTemplate.opsForSet().add(key, value);
if (expire != NOT_EXPIRE) {
expire(key, expire);
}
}
public void removeFromSet(String key, String value) {
stringRedisTemplate.opsForSet().remove(key, value);
}
public Set<String> membersOfSet(String key) {
Set<String> members = stringRedisTemplate.opsForSet().members(key);
return members;
}
public boolean setContainsWarn(String key, String value) {
return stringRedisTemplate.opsForSet().isMember(key, value);
}
public void addSet(String key, Object value, long expire) {
redisTemplate.opsForSet().add(key, value);
if (expire != NOT_EXPIRE) {
expire(key, expire);
}
}
public Set<Object> members(String key) {
return redisTemplate.opsForSet().members(key);
}
public void removeOfSet(String key, Object value) {
stringRedisTemplate.opsForSet().remove(key, value);
}
public long inc(String key) {
return redisTemplate.opsForValue().increment(key);
}
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
public List<Object> getAllByKey(String keysPattern) {
Set<String> keys = redisTemplate.keys(keysPattern);
return redisTemplate.opsForValue().multiGet(keys);
}
private <T> T fromJson(String json, Class<T> clazz){
return gson.fromJson(json, clazz);
}
}
在最后,让我们编写一个测试类,感受使用 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;
}
}
|