(二)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 ...
随机推荐
- SQL Server 2005 sa登录失败。已成功与服务器建立连接 但是在登录过程中发生错误。 provider 共享内存提供程序 error 0 管道的另一端上无任何进程。
SQL Server 2005 Express版 用户 'sa' 登录失败.该用户与可信 SQL Server 连接无关联.提示错误:已成功与服务器建立连接 但是在登录过程中发生错误. provide ...
- C++常用注意事项
new和delete:现在还没有new[][]和delete[][],所以在用这些的时候最好用循环解决,先一个指针的数组,然后再初始化,每个元素再new一下,这样就满足了多维数组的条件:比如: int ...
- Cobalt Strike系列教程第六章:安装扩展
Cobalt Strike系列教程分享如约而至,新关注的小伙伴可以先回顾一下前面的内容: Cobalt Strike系列教程第一章:简介与安装 Cobalt Strike系列教程第二章:Beacon详 ...
- Volatile的应用DCL单例模式(四)
Volatile的应用 单例模式DCL代码 首先回顾一下,单线程下的单例模式代码 /** * 单例模式 * * @author xiaocheng * @date 2020/4/22 9:19 */ ...
- 详解 HashMap
本篇博文的知识点,在我们的日常生活中,应用十分广阔.比如:每个学生,都有自己的对应的学号.每一个公民,都有自己的身份证号- - 相信看到这里,有的同学基本上已经猜到了这个类的主要用途.那么,话不多说, ...
- MYSQL和SQL Server 的区别
注意MYSQL使用注释 -- 时 要后面加上空格 使用 #不用 一.数据类型 MYSQL:支持enum和set类型 ;SQL SERVER:不支持 MYSQL:不支持nchar,nvarchar,nt ...
- 浏览器插件之王-Tampermonkey(油猴脚本)
大家电脑都在使用浏览器,相信大家对浏览器插件也不陌生,浏览器插件是安装在浏览器里面,对浏览器功能进行拓展的脚本,现在的主流浏览器都有各种各样的插件如图: 这些插件让我们的上网方便了许多,有去广告的插件 ...
- 详细分析Redis的持久化操作——RDB与AOF
一.前言 由于疫情的原因,学校还没有开学,这也就让我有了很多的时间.趁着时间比较多,我终于可以开始学习那些之前一直想学的技术了.最近这几天开始学习Redis,买了本<Redis实战>, ...
- 23-Java-Spring框架(一)
一.Spring框架了解 Spring框架是一个开源的框架,为JavaEE应用提供多方面的解决方案,用于简化企业级应用的开发,相当于是一种容器,可以集成其他框架(结构图如下). 上图反映了框架引包的依 ...
- PHP的yield是个什么玩意
来源:https://segmentfault.com/a/1190000018457194 其实,我并不是因为迭代或者生成器或者研究PHP手册才认识的yield,要不是协程,我到现在也不知道PHP中 ...