理解Redis的持久性的两种方式,RDB,AOF.重点内容。
Redis本质是数据库。那么redis如何用java代码实现数据库的连接呢?
回顾上次课将的一些语句语法。
Redis的持久性的两种方式。
第一部分。让数据能存放的时间,更加持久。那么利用的是。Redis缓存的一个文件。redis.windows.conf
思考一个问题为什么要学Redis持久性?写看一个案例。
上面的图是Redis的服务器启动页面。
思考一下问题,当我关闭了服务器,数据还在吗?
答案是不在。如果你用自己一天的数据存放的数据用上万个,突然断电。一切清零,你的心情如何?
Redis的持久性,的两种方式,RDB,AOF.重点内容。
接下来进入RDB与AOF学习。首先打开E:\Redis-x64-3.2.100,文件下的redis.windows.conf文件。
# # Save the DB on disk: # # save <seconds> <changes> # # Will save the DB if both the given number of seconds and the given # number of write operations against the DB occurred. # # In the example below the behaviour will be to save: # after 900 sec (15 min) if at least 1 key changed # after 300 sec (5 min) if at least 10 keys changed # after 60 sec if at least 10000 keys changed # # Note: you can disable saving completely by commenting out all "save" lines. # # It is also possible to remove all the previously configured save # points by adding a save directive with a single empty string argument # like in the following example: # # save "" save 900 1 save 300 10 save 10000 6
上面是RDB文件的学习过程。
# The name of the append only file (default: "appendonly.aof") appendfilename "appendonly.aof" # The fsync() call tells the Operating System to actually write data on disk # instead of waiting for more data in the output buffer. Some OS will really flush # data on disk, some other OS will just try to do it ASAP. # # Redis supports three different modes: # # no: don't fsync, just let the OS flush the data when it wants. Faster. # always: fsync after every write to the append only log. Slow, Safest. # everysec: fsync only one time every second. Compromise. # # The default is "everysec", as that's usually the right compromise between # speed and data safety. It's up to you to understand if you can relax this to # "no" that will let the operating system flush the output buffer when # it wants, for better performances (but if you can live with the idea of # some data loss consider the default persistence mode that's snapshotting), # or on the contrary, use "always" that's very slow but a bit safer than # everysec. # # More details please check the following article: # http://antirez.com/post/redis-persistence-demystified.html # # If unsure, use "everysec". # appendfsync always appendfsync everysec # appendfsync no
上面是AOF的详细信息。
Microsoft Windows [版本 10.0.19044.1348] (c) Microsoft Corporation。保留所有权利。 C:\Users\MZFAITHDREAM> E: E:\>cd E:\Redis-x64-3.2.100 E:\Redis-x64-3.2.100> redis-server.exe redis.windows.conf [13268] 17 Dec 14:34:38.295 # Creating Server TCP listening socket 127.0.0.1:6379: bind: No error E:\Redis-x64-3.2.100>redis-server.exe redis.windows.conf _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.2.100 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 14768 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [14768] 17 Dec 14:34:54.574 # Server started, Redis version 3.2.100 [14768] 17 Dec 14:34:54.575 * The server is now ready to accept connections on port 6379
观察上面的代码请看出你的问题。
导入架包开始实操
package com.redis.test; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.tomcat.jni.OS; import org.junit.Test; import redis.clients.jedis.Jedis; /** * 五种数据的增删改查 String Hash Map List * @author MZFAITHDREAM * */ public class RedisTest {}
1.String 类型
@Test public void test1() { /** * 操作redis数据库的增删改查String */ Jedis jedis =new Jedis("localhost",6379); System.out.println("从redis数据库中获取名称"+jedis.get("name")); System.out.println("从redis数据库中获取名称"+jedis.get("sex")); System.out.println("从redis数据库中获取名称"+jedis.get("number")); jedis.set("phone", "1234556"); System.out.println("增加成功1"); long q =jedis.del("name"); if(q>0) { System.out.println("删除成功"); } jedis.close(); }
2.hash类型
@Test public void test2() { Jedis jedis =new Jedis("localhost",6379); String name=jedis.hget("student", "name"); String age=jedis.hget("student", "age"); String sex=jedis.hget("student", "sex"); System.out.println(name+age+sex); long l= jedis.hset("student", "adress", "江西省"); if(l>0) { System.out.println("删除成功"); } //增加数据 String adress=jedis.hget("student", "江西省"); System.out.println(adress); jedis.hdel("student", "name"); //查询数据 Map<String ,String> map= jedis.hgetAll("student"); map.forEach((key,value)->System.out.println(key+"="+value)); jedis.close(); }
3.list类型
//list的集合 @Test public void test3() { //操作list的集合 Jedis jedis =new Jedis("localhost",6379); //从left增加数据 jedis.lpush("mylist", "张三","李四","万物","hellow"); jedis.rpush("Amylist", "B张三","C李四","D万物","Ehellow"); List<String> olList =jedis.lrange("myslist", 0, -1); olList.forEach(s->System.out.println(s)); String s=jedis.lpop("mylist"); System.out.println("删除一条数据"+s); String s1=jedis.rpop("mylist"); System.out.println("删除一条数据"+s1); jedis.close(); }
4.set类型
//Set集合 @Test public void test4() { Jedis jedis =new Jedis("localhost",6379); long l= jedis.sadd("myset","256","3373","373"); if(l>0) { System.out.println("增加成功"); }else { System.out.println("元素不能重复"); } Set<String> oSet =jedis.smembers("myset"); oSet.forEach(x->System.out.println(x)); jedis.close(); }
5.sortedset类型
/** * mysoet */ @Test public void test5() { Jedis jedis =new Jedis("localhost",6379); jedis.zadd("mysort", 34,"王五"); jedis.zadd("mysort", 56, "小康"); jedis.zadd("mysort", 14,"四小五"); jedis.zadd("mysort", 66, "小二康"); Set <String>oSet =jedis.zrange("mysort", 0,-1); oSet.forEach(s->System.out.println(s)); jedis.close(); jedis.zrem("mysort", "小二康");
五种数据的案例运行结果