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.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.lang.reflec