目录
1、Redis 入门
1.1、Redis 简介
1.2、下载与安装
1.2.1、下载
1.2.2、Linux 安装 Redis
1.2.3、Windows 安装 Redis
1.3、服务启动与停止
1.3.1、Linux 启动与停止
1.3.2、Linux 设置服务后台运行
1.3.3、Windows 启动与停止服务
1.3.4、Linux 开启密码校验
1.3.5、Linux 开启远程连接
2、Redis 数据类型
2.1、介绍
2.2、字符串 String 操作命令
2.3、哈希 hash 操作命令
2.4、列表 list 操作命令
2.5、集合 set 操作命令
2.6、有序集合 sorted set 操作命令
2.7、通用命令
3、在 Java 中操作 Redis
3.1、介绍
3.2、Jedis
3.3、Spring Data Redis
3.3.1、依赖
3.3.2、RedisTemplate 类
3.3.3、application.yml
3.3.4、设置 RedisTemplate 的序列化器
3.3.5、测试
Redis 是一个基于内存的 key - value 结构数据库。
官网:Redis
The open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker.
开源内存数据存储,被数百万开发人员用作数据库、缓存、流引擎和消息代理。
Redis 是用 C 语言开发的一个开源的高性能键值对 (key-value) 数据库,官方提供的数据是可以达到 100000+ 的 QPS(每秒内查询次数)。它存储的 value 类型比较丰富,也被称为结构化的 NoSql 数据库。
NoSql (Not Only sQL),不仅仅是 SQL,泛指非关系型数据库。NoSql 数据库并不是要取代关系型数据库,而是关系型数据库的补充。
关系型数据库(RDBMS):MySQL、Oracl、DB2、SQLServer
非关系型数据库(NoSQL):Redis、Mongo db、MemCached
Redis 应用场景:缓存、任务队列、消息队列、分布式锁
Redis 安装包分为 Windows 版和 Linux 版
解压:
tar -zxvf redis-4.0.0.tar.gz -C /usr/local
安装gcc:
yum install gcc-c++
进入redis目录后执行:
[root@localhost redis-4.0.0]# make
进入src目录后进行安装:
[root@localhost src]# make install
Redis 的 windows 版属于绿色软件,直接解压即可使用,解压后目录结构如下:
进入 src 目录后执行
./redis-server
进入 根目录 下修改 redis.conf 文件
输入 /dae 查找,然后将 daemonize 改为 yes,即后台运行
在根目录以 redis.conf 作为配置文件启动后台运行
[root@localhost redis-4.0.0]# src/redis-server ./redis.conf
25973:C 10 Sep 12:52:39.424 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
25973:C 10 Sep 12:52:39.424 # Redis version=4.0.0, bits=64, commit=00000000, modified=0, pid=25973, just started
25973:C 10 Sep 12:52:39.424 # Configuration loaded
双击 redis-server.exe 启动
修改 redis-conf,将 requirepass 这行的注解去掉,并指定密码
将原进程杀掉再启动
[root@localhost redis-4.0.0]# ps -ef | grep redis
root 25974 1 0 12:52 ? 00:00:01 src/redis-server 127.0.0.1:6379
root 35559 27196 0 11:02 pts/2 00:00:00 ./redis-cli
root 70343 1289 0 13:12 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis-4.0.0]# kill -9 25974
[root@localhost redis-4.0.0]# ps -ef | grep redis
root 35559 27196 0 11:02 pts/2 00:00:00 ./redis-cli
root 71526 1289 0 13:12 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis-4.0.0]# kill -9 35559
[root@localhost redis-4.0.0]# ps -ef | grep redis
root 72270 1289 0 13:13 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis-4.0.0]# src/redis-server ./redis.conf
74266:C 10 Sep 13:13:58.154 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
74266:C 10 Sep 13:13:58.154 # Redis version=4.0.0, bits=64, commit=00000000, modified=0, pid=74266, just started
74266:C 10 Sep 13:13:58.154 # Configuration loaded
[root@localhost redis-4.0.0]# src/redis-cli -h localhost -p 6379
localhost:6379> keys *
(error) NOAUTH Authentication required.
localhost:6379> auth 密码
OK
设置登陆时同时进行认证
[root@localhost redis-4.0.0]# src/redis-cli -h localhost -p 6379 -a 密码
localhost:6379> keys *
(empty list or set)
再没有开启远程连接前,再 Windows 下连接 Linux 的 Redis
PS D:\redis\Redis-x64-3.2.100> ./redis-cli.exe -h 192.168.44.128 -p 6379 -a 密码
Could not connect to Redis at 192.168.44.128:6379: 由于目标计算机积极拒绝,无法连接。
Could not connect to Redis at 192.168.44.128:6379: 由于目标计算机积极拒绝,无法连接。
需要再 Linux 下修改 redis.conf 配置文件,将下图改行注释
修改完后要重启服务
[root@localhost redis-4.0.0]# ps -ef | grep redis
root 2650 1 0 10:25 ? 00:00:00 src/redis-server 127.0.0.1:6379
root 16456 1290 0 10:37 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis-4.0.0]# kill -9 2650
[root@localhost redis-4.0.0]# ps -ef | grep redis
root 16785 1290 0 10:37 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis-4.0.0]# src/redis-server ./redis.conf
17142:C 11 Sep 10:38:10.099 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
17142:C 11 Sep 10:38:10.099 # Redis version=4.0.0, bits=64, commit=00000000, modified=0, pid=17142, just started
17142:C 11 Sep 10:38:10.099 # Configuration loaded
然后设置防火墙
[root@localhost redis-4.0.0]# firewall-cmd --zone=public --add-port=6379/tcp --permanent
success
[root@localhost redis-4.0.0]# firewall-cmd --reload
success
在 Windows 下再次连接 Linux 的 Redis
PS D:\redis\Redis-x64-3.2.100> ./redis-cli.exe -h 192.168.44.128 -p 6379 -a 密码
192.168.44.128:6379> keys *
(empty list or set)
中文文档:Redis 教程_redis教程
更多命令:Redis 字符串(String)_redis教程
127.0.0.1:6379> set name xiaoming
OK
127.0.0.1:6379> get name
"xiaoming"
127.0.0.1:6379> get age
(nil)
127.0.0.1:6379> set age 20
OK
127.0.0.1:6379> get age
"20"
127.0.0.1:6379> set age 30
OK
127.0.0.1:6379> get age
"30"
127.0.0.1:6379> setex city 10 beijing
OK
127.0.0.1:6379> get city
"beijing"
127.0.0.1:6379> get city
(nil)
127.0.0.1:6379> setnx k1 v1
(integer) 1
127.0.0.1:6379> setnx k1 v2
(integer) 0
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379>
127.0.0.1:6379> hset 001 name xiaoming
(integer) 1
127.0.0.1:6379> hset 001 age 20
(integer) 1
127.0.0.1:6379> hget 001 name
"xiaoming"
127.0.0.1:6379> hget 001 age
"20"
127.0.0.1:6379> hdel 001 age
(integer) 1
127.0.0.1:6379> hget 001 age
(nil)
127.0.0.1:6379> hkeys 001
1) "name"
127.0.0.1:6379> hset 001 age 30
(integer) 1
127.0.0.1:6379> hkeys 001
1) "name"
2) "age"
127.0.0.1:6379> hvals 001
1) "xiaoming"
2) "30"
127.0.0.1:6379> hgetall 001
1) "name"
2) "xiaoming"
3) "age"
4) "30"
127.0.0.1:6379> lpush list a b c
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> lpush list zhang
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "zhang"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> lpush list a
(integer) 5
127.0.0.1:6379> lrange list 0 -1
1) "a"
2) "zhang"
3) "c"
4) "b"
5) "a"
127.0.0.1:6379> rpop list
"a"
127.0.0.1:6379> lrange list 0 -1
1) "a"
2) "zhang"
3) "c"
4) "b"
127.0.0.1:6379> llen list
(integer) 4
127.0.0.1:6379> brpop list 10
1) "list"
2) "b"
127.0.0.1:6379> brpop list 10
1) "list"
2) "c"
127.0.0.1:6379> brpop list 10
1) "list"
2) "zhang"
127.0.0.1:6379> brpop list 10
1) "list"
2) "a"
127.0.0.1:6379> lrange list 0 -1
(empty list or set)
127.0.0.1:6379> brpop list 10
(nil)
(10.04s)
127.0.0.1:6379>
127.0.0.1:6379> sadd set a b c d
(integer) 4
127.0.0.1:6379> smembers set
1) "c"
2) "b"
3) "a"
4) "d"
127.0.0.1:6379> sadd set a d
(integer) 0
127.0.0.1:6379> smembers set
1) "a"
2) "d"
3) "b"
4) "c"
127.0.0.1:6379> scard set
(integer) 4
127.0.0.1:6379> sadd s a d s o
(integer) 4
127.0.0.1:6379> smembers s
1) "a"
2) "o"
3) "s"
4) "d"
127.0.0.1:6379> sinter set s
1) "a"
2) "d"
127.0.0.1:6379> sunion set s
1) "b"
2) "c"
3) "a"
4) "d"
5) "s"
6) "o"
127.0.0.1:6379> sdiff set s
1) "c"
2) "b"
127.0.0.1:6379> sdiff s set
1) "s"
2) "o"
127.0.0.1:6379> srem s a b c d
(integer) 2
127.0.0.1:6379> smembers s
1) "o"
2) "s"
127.0.0.1:6379>
127.0.0.1:6379> zadd sset 10.0 a 9.0 b
(integer) 2
127.0.0.1:6379> zrange sset 0 -1
1) "b"
2) "a"
127.0.0.1:6379> zadd sset 9.5 c
(integer) 1
127.0.0.1:6379> zrange sset 0 -1
1) "b"
2) "c"
3) "a"
127.0.0.1:6379> zrange sset 0 -1 withscores
1) "b"
2) "9"
3) "c"
4) "9.5"
5) "a"
6) "10"
127.0.0.1:6379> zincrby sset 20 b
"29"
127.0.0.1:6379> zrange sset 0 -1
1) "c"
2) "a"
3) "b"
127.0.0.1:6379> zrem sset b
(integer) 1
127.0.0.1:6379> zrange sset 0 -1
1) "c"
2) "a"
127.0.0.1:6379>
127.0.0.1:6379> keys *
1) "001"
2) "sset"
3) "name"
4) "k1"
5) "set"
6) "age"
7) "s"
127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> exists aaa
(integer) 0
127.0.0.1:6379> type name
string
127.0.0.1:6379> type set
set
127.0.0.1:6379> type sset
zset
127.0.0.1:6379> ttl name
(integer) -1
127.0.0.1:6379> setex test 100 zhang
OK
127.0.0.1:6379> ttl test
(integer) 97
127.0.0.1:6379> del test
(integer) 1
127.0.0.1:6379>
Maven 坐标:
redis.clients
jedis
2.8.0
注意,运行前要先启动 Redis
使用:
@Test
public void testRedis(){
//1 获取连接,设置服务器ip及端口号
Jedis jedis = new Jedis("localhost",6379);
//2 执行具体的操作
jedis.set("username","xiaoming");
String value = jedis.get("username");
System.out.println(value);
jedis.hset("myhash","addr","bj");
String hValue = jedis.hget("myhash", "addr");
System.out.println(hValue);
Set keys = jedis.keys("*");
for (String key : keys) {
System.out.println(key);
}
jedis.del("username","myhash");
//3 关闭连接
jedis.close();
}
Maven 坐标:
org.springframework.boot
spring-boot-starter-data-redis
spring:
application:
name: springdataredis_demo
#Redis相关配置
redis:
host: localhost # 连接ip
port: 6379 # 端口
#password: 123456 #密码
database: 0 #操作的是0号数据库
jedis:
#Redis连接池配置
pool:
max-active: 8 #最大连接数
max-wait: 1ms #连接池最大阻塞等待时间
max-idle: 4 #连接池中的最大空闲连接
min-idle: 0 #连接池中的最小空闲连接
RedisTemplate 默认的Key序列化器为:JdkSerializationRedisSerializer,在执行下面这行代码后
redisTemplate.opsForValue().set("city123","beijing");
在数据库中的键如下
127.0.0.1:6379> keys * 1) "\xac\xed\x00\x05t\x00\acity123"
为了避免这种情况,需要设置其序列化器
一般来说 Value 无需更换系列化器,因为从代码获取时会自动反序列化
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate
@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringDataRedisTest {
@Autowired
private RedisTemplate redisTemplate;
/**
* 操作String类型数据
*/
@Test
public void testString(){
redisTemplate.opsForValue().set("city123","beijing");
String value = (String) redisTemplate.opsForValue().get("city123");
System.out.println(value);
// 设置超时时间
redisTemplate.opsForValue().set("key1","value1",10l, TimeUnit.SECONDS);
Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("city1234", "nanjing");
System.out.println(aBoolean);
redisTemplate.delete("city123");
redisTemplate.delete("city1234");
redisTemplate.delete("key1");
}
/**
* 操作Hash类型数据
*/
@Test
public void testHash(){
HashOperations hashOperations = redisTemplate.opsForHash();
//存值
hashOperations.put("002","name","xiaoming");
hashOperations.put("002","age","20");
hashOperations.put("002","address","bj");
//取值
String age = (String) hashOperations.get("002", "age");
System.out.println(age);
//获得hash结构中的所有字段
Set keys = hashOperations.keys("002");
for (Object key : keys) {
System.out.println(key);
}
//获得hash结构中的所有值
List values = hashOperations.values("002");
for (Object value : values) {
System.out.println(value);
}
redisTemplate.delete("002");
}
/**
* 操作List类型的数据
*/
@Test
public void testList(){
ListOperations listOperations = redisTemplate.opsForList();
//存值
listOperations.leftPush("mylist","a");
listOperations.leftPushAll("mylist","b","c","d");
//取值
List mylist = listOperations.range("mylist", 0, -1);
for (String value : mylist) {
System.out.println(value);
}
//获得列表长度 llen
Long size = listOperations.size("mylist");
int lSize = size.intValue();
for (int i = 0; i < lSize; i++) {
//出队列
String element = (String) listOperations.rightPop("mylist");
System.out.println(element);
}
redisTemplate.delete("mylist");
}
/**
* 操作Set类型的数据
*/
@Test
public void testSet(){
SetOperations setOperations = redisTemplate.opsForSet();
//存值
setOperations.add("myset","a","b","c","a");
//取值
Set myset = setOperations.members("myset");
for (String o : myset) {
System.out.println(o);
}
//删除成员
setOperations.remove("myset","a","b");
//取值
myset = setOperations.members("myset");
for (String o : myset) {
System.out.println(o);
}
redisTemplate.delete("myset");
}
/**
* 操作ZSet类型的数据
*/
@Test
public void testZset(){
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
//存值
zSetOperations.add("myZset","a",10.0);
zSetOperations.add("myZset","b",11.0);
zSetOperations.add("myZset","c",12.0);
zSetOperations.add("myZset","a",13.0);
//取值
Set myZset = zSetOperations.range("myZset", 0, -1);
for (String s : myZset) {
System.out.println(s);
}
System.out.println("------------------------------");
//修改分数
zSetOperations.incrementScore("myZset","b",20.0);
//取值
myZset = zSetOperations.range("myZset", 0, -1);
for (String s : myZset) {
System.out.println(s);
}
System.out.println("------------------------------");
//删除成员
zSetOperations.remove("myZset","a","b");
//取值
myZset = zSetOperations.range("myZset", 0, -1);
for (String s : myZset) {
System.out.println(s);
}
redisTemplate.delete("myZset");
}
/**
* 通用操作,针对不同的数据类型都可以操作
*/
@Test
public void testCommon(){
//获取Redis中所有的key
Set keys = redisTemplate.keys("*");
for (String key : keys) {
System.out.println(key);
}
//判断某个key是否存在
Boolean itcast = redisTemplate.hasKey("itcast");
System.out.println(itcast);
//删除指定key
redisTemplate.delete("myZset");
//获取指定key对应的value的数据类型
DataType dataType = redisTemplate.type("myset");
System.out.println(dataType.name());
}
}