tar zxvf redis-6.0.1.tar.gz
mv redis-6.0.1 /usr/local/
cd /usr/local/redis-6.0.1/
sudo make test
sudo make install
启动方式:直接启动 Redis: redis-server
,成功后会看到下图:
关闭方式:登陆客户端,在客户端执行 SHUTDOWN
可关闭 redis 服务,如果关闭不了就加一个参数,执行SHUTDOWN NOSAVE
可关闭redis 服务,其中:
登陆客户端方式:redis-cli
命令 | 用途 |
---|---|
set key value | 设置 key 的值 |
get key | 获取 key 的值 |
exists key | 查看此 key 是否存在,存在返回1,不存在返回0 |
keys * | 查看所有的 key |
del key | 删除指定key,若成功则返回1,否则返回0 |
flushall | 消除所有的 key |
用法如下:
pom依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.7.RELEASE</version> </dependency>
在配置文件application.properities中加入Redis的连接配置:
spring.redis.host=127.0.0.1 spring.redis.port=6379
Redis自定义注入Bean组件配置:
package com.practice.demo.config; import org.springframework.beans.factory.annotation.Autowired; 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.core.StringRedisTemplate; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * 配置Redis的两个操作组件:RedisTemplate & StringRedisTemplate * */ @Configuration public class CommonConfig { @Autowired private RedisConnectionFactory redisConnectionFactory; @Bean public RedisTemplate<String, Object> redisTemplate(){ RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); return redisTemplate; } @Bean public StringRedisTemplate stringRedisTemplate(){ StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); stringRedisTemplate.setConnectionFactory(redisConnectionFactory); return stringRedisTemplate; } }
Redis读写字符串测试Demo如下,需要注意的是,Redis除了支持字符串外,还支持列表、集合、有序集合、哈希存储等多种数据结构。
package com.practice.demo.config; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.assertj.core.util.Lists; import org.junit.jupiter.api.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.ListOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.SetOperations; import org.springframework.data.redis.core.ValueOperations; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.*; import java.util.concurrent.TimeUnit; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest public class RedisTest { @Autowired private RedisTemplate redisTemplate; /********************字符串类型********************/ /** * 读写变量 */ @Test public void writeAndRead() { String key = "redisKey"; String value = "redisValue"; ValueOperations valueOperations = redisTemplate.opsForValue(); // 设置数据存在的时间 valueOperations.set(key, value, 10, TimeUnit.SECONDS); Object result = valueOperations.get(key); System.out.println("读取的内容:" + result); } }
注意在启动前需要先将Redis启动,如果运行上述Demo后的结果与下面一致,说明Redis在SpringBoot中的配置正确。
(二)Redis在Mac下的安装与SpringBoot中的配置
原文:https://www.cnblogs.com/huaiheng/p/12839989.html