Redis使用详解

本文详细介绍了Redis的Linux安装步骤,包括下载、解压、编译、配置及启动,以及如何设置和验证密码。此外,还讲解了在SpringBoot项目中配置Redis的全过程,包括添加依赖、配置数据源、编写配置文件、工具类以及测试连接。
摘要由CSDN通过智能技术生成

Redis是一个开源的支持网络可基于内存亦可持久化的日志型Key-Value数据库,并提供多种语言的API。它的值(value)可以是字符串(String)、哈希(Hash)、列表(list)、集合(sets)和有序集合(sorted sets)等类型。

一、Redis安装(以linux安装为例)

1.使用linux wget下载安装包

wget http://download.redis.io/releases/redis-3.0.0.tar.gz

2.将安装包拷贝到安装目录下如/usr/local

cp redis-3.0.0.rar.gz /usr/local

3.解压源码

tar -zxvf redis-3.0.0.tar.gz 

4.进入解压后的目录进行编译

cd /usr/local/redis-3.0.0

5.安装到指定目录如/usr/local/redis

make PREFIX=/usr/local/redis install

6.进入源码目录,里面有一份配置文件 redis.conf,然后将其拷贝到安装路径下

cp /usr/local/redis-3.0.0/redis.conf  /usr/local/redis

7.修改redis.conf配置文件, daemonize yes 以后端模式启动

vim /usr/local/redis/redis.conf

8.启动redis

cd /usr/local/redis
./bin/redis-server ./redis.conf

9.连接redis,查看现有redis密码

redis-cli
127.0.0.1:6379> config get requirepass

10.设置redis密码,成功后会返回“ok”字样

config set requirepass ****(****为你要设置的密码)

11.以密码登录redis

redis-cli -h 127.0.0.1 -p 6379 -a ****

二、SpringBoot配置使用redis

1.pom.xml添加redis依赖

 <!-- redis缓存 -->
 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>

2.application.properties添加数据源

#Redis 配置信息
#Redis数据库分片索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=

3.配置RedisConfig配置文件

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerial
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值