(二)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 ...
随机推荐
- 第十三节:telnetlib、redis、threading模块
telnetlib模块案例: import telnetlib,re class TelnetInfo(): def telnetdo(self, host, port, command): tn = ...
- C#——继承
在某基类中声明 virtual 并在一个或多个派生类中被重新定义的成员函数称为虚函数. 虚函数的作用就是实现多态性(Polymorphism),多态性是将接口与实现进行分离. C#作为完全面向对象语言 ...
- sql 系统表协助集合
一.判断字段是否存在: select * from syscolumns where id=object_id('表') and name='字段'
- C - Mind Control CodeForces - 1291C
菜到家了,题意都读不懂. 题目大意: 总共有n个人和n个数字 n个人拍成一队,n个数字也是有顺序的 你排在第m个位置 按照顺序的每个人可以拿走这个序列中的第一个数字或者最后一个数字 你可以在所有人操作 ...
- 文本文件的合并操作方法 - Python
我们有时候,看到几k的日志文件,一大堆,一个一个打开又很麻烦,少看几个,又担心遗漏,这个时候,如果有一个可以合并所有文本文件的工具就好了. 下面这个代码就可以实现,它不局限于.txt格式,基本上字符型 ...
- python 基础篇 模块化
在做项目的时候,虽然你不可能把全世界的代码都放到一个文件夹下,但是类似模块化的思想还是要有的--那就是以项目的根目录作为最基本的目录,所有的模块调用,都要通过根目录一层层向下索引的方式来 import ...
- tensorflow1.0 变量加法
import tensorflow as tf state = tf.Variable(0,name='counter') print(state.name) one = tf.constant(1) ...
- 理解java容器底层原理--手动实现ArrayList
为了照顾初学者,我分几分版本发出来 版本一:基础版本 实现对象创建.元素添加.重新toString() 方法 package com.xzlf.collection; /** * 自定义一个Array ...
- DES加密解密算法C++实现
DES加密算法并不难,是由一些简单的变换得来的,难的是要有足够的耐心.蒟蒻并不想说自己用了多久才把代码写好的. 代码: 我真的太难了QAQ #include<iostream> using ...
- 箭头函数的this指向问题-一看就懂
OK,对于箭头函数的this 用一句话概括:箭头函数中的this指向的是定义时的this,而不是执行时的this. 如果上面这句话听的是懂非懂或者完全不懂的,没关系,下面会有案例讲解. 举个栗子 来看 ...