(二)Redis在Mac下的安装与SpringBoot中的配置
1 下载Redis
- 官网下载,下载 stable 版本,稳定版本。

建议下载5.0.8版本的Redis
2 本地安装
- 解压:
tar zxvf redis-5.0.8.tar.gz - 移动到: sudo
mv redis-5.0.8 /usr/local/ - 切换到:
cd /usr/local/redis-5.0.8/ - 编译测试
sudo make test - 编译安装
sudo make install
3 Redis 的启动与停止
启动方式:直接启动 Redis: redis-server ,成功后会看到下图:

关闭方式:登陆客户端,在客户端执行 SHUTDOWN 可关闭 redis 服务,如果关闭不了就加一个参数,执行SHUTDOWN NOSAVE可关闭redis 服务,其中:
登陆客户端方式:redis-cli
设置为后台启动:https://blog.csdn.net/ksdb0468473/article/details/52126009
设置完毕后启动命令为:redis-server redis.conf
查看是否启动成功:ps -ef | grep redis
4 客户端常用命令
| 命令 | 用途 |
|---|---|
| set key value | 设置 key 的值 |
| get key | 获取 key 的值 |
| exists key | 查看此 key 是否存在,存在返回1,不存在返回0 |
| keys * | 查看所有的 key |
| del key | 删除指定key,若成功则返回1,否则返回0 |
| flushall | 消除所有的 key |
用法如下:

5 SpringBoot整合Redis
pom依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4..RELEASE</version>
</dependency>
在配置文件application.properities中加入Redis的连接配置:
spring.redis.host=127.0.0.1
spring.redis.port=
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, , TimeUnit.SECONDS);
Object result = valueOperations.get(key);
System.out.println("读取的内容:" + result);
}
}
注意在启动前需要先将Redis启动,如果运行上述Demo后的结果与下面一致,说明Redis在SpringBoot中的配置正确。

(二)Redis在Mac下的安装与SpringBoot中的配置的更多相关文章
- Redis在Mac下的安装与使用方法
首先从Redis官网http://www.redis.io去下载最新版本的Redis安装文件(此处以Redis版本为例进行说明). Redis 2.6.16版本的下载地址:http://downl ...
- nginx详解(代理服务器的解释+nginx 在linux 下的安装+nginx.conf 中的配置解释)
一.概论 1.什么是代理服务器 代理服务器,客户机在发送请求时,不会直接发送给目的主机,而是先发送给代理服务器,代理服务接受客户机请求之后,再向主机发出,并接收目的主机返回的数据,存放在代理服务器的硬 ...
- Mac下Tomcat安装与Intellij IDEA配置Tomcat
Mac下Tomcat安装与Intellij IDEA配置Tomcat 一 安装 1 下载地址:https://tomcat.apache.org/download-90.cgi 2 将压缩包解压后移至 ...
- Mac下新安装的MySQL无法登陆root用户解决方法
一 设置MySQL命令行搜索路径 0.苹果->系统偏好设置->最下边点mysql 在弹出页面中 启动mysql服务 1.打开终端,输入: sudo vi ~/.bash_profile ...
- Mac 下 docker安装
http://www.th7.cn/system/mac/201405/56653.shtml Mac 下 docker安装 以及 处理错误Cannot connect to the Docker d ...
- Mac下Jekyll安装
之前一直用Wordpress,虽然功能强大,各种插件各种bug,如果想弄个主题,折腾得要命.最近改用jekyll+gitHub免费空间.记录一下. 我用的是Mac,所以只讲述Mac下如何安装,Wind ...
- redis 在Linux下的安装与配置
redis在Linux下的安装与配置 by:授客 QQ:1033553122 测试环境 redis-3.0.7.tar.gz 下载地址: http://redis.io/download http: ...
- Mac下Maven安装与配置
Mac下Maven安装与配置 下载maven http://maven.apache.org/download.cgi main->download菜单下的Files 下载后解压在Documen ...
- Mac下删除安装的pkg
Mac下的安装和删除都比windows更加简单清晰,这点在dmg方式下非常明显,但很多时候我们会使用pkg来进行安装,这样的安装想删除就有点麻烦了. 比如,我安装了Golang这个pkg用于g ...
随机推荐
- 【python实现卷积神经网络】Dropout层实现
代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...
- Xcode自动注释插件:VVDocumenter-Xcode
VVDocumenter-Xcode 是由 @onevcat 喵神开发的一个Xcode插件,其作用是在Xcode中输入"///"后自动生成规范的文档注释,的确非常好用而且实用. G ...
- 复杂Excel转换与导入
需求 把不同客户提供Excel 直接导入到系统中生成对应的收货单或是出货单.后端创建收货端和出货单的接口已经有现成的webservice或是标准的xml:这类需要做的就是把客户提供不同种类的Excel ...
- 编码理解的漫漫长路(Unicode、GBK、ISO)
Ø 那么现在开始康康都有哪些编码方式 1. ASCII
- python+selenium实现网页自动化与爬虫技术
举例某购物网站,通过selenium与python,实现主页上商品的搜索,并将信息爬虫保存至本地excel表内. 一.python环境与selenium环境安装 python在官网下载并安装并且设置环 ...
- 控制反转IOC,依赖注入DI理解
IOC:控制反转,常规下,高层依赖低层,项目是不稳定的.我们则应该把高层对低层的依赖去掉,换成对抽象的依赖,细节交给第三方来决定,这就是控制反转,反转的目的是为了降低依赖,增强扩展性. DI:依赖注入 ...
- CentOS 使用中问题记录
⚠️使用yum提示Error: rpmdb open failed的解决方案 清除原rpmdb文件,这一步可能不用操作,直接进行第2步 # rm -f /var/lib/rpm/__db.* 重建rp ...
- 解决cvc-complex-type.2.4.a: Invalid content was found starting with element
今天用myeclipse导入 一个项目出现后出现cvc-complex-type.2.4.a: Invalid content was found starting with element 'inf ...
- Vue 3.0 Composition API - 中文翻译
Composition API 发布转载请附原文链接 https://www.cnblogs.com/zgh-blog/articles/composition_api.html 这两天初步了解了下 ...
- 一个老牌程序员推荐的JavaScript的书籍,看了真的不后悔!
很多人问我怎么学前端?我的回答是:读书吧!相对于在网上学习,在项目中学习和跟着有经验的同事学习,书中有着相对完整的知识体系,每读一本好书都会带来一次全面的提高.而如果深一脚浅一脚的学习,写出代码的质量 ...