栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Redis缓存技术学习内容第二课。@1回顾上次课将的一些语句语法。@2理解Redis的持久性,的两种方式,RDB,AOF.重点内容。@3 Redis本质是数据库。那么redis如何用,java代码去编

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Redis缓存技术学习内容第二课。@1回顾上次课将的一些语句语法。@2理解Redis的持久性,的两种方式,RDB,AOF.重点内容。@3 Redis本质是数据库。那么redis如何用,java代码去编

@1回顾上次课将的一些语句语法。 @2理解Redis的持久性,的两种方式, 。 @3 Redis本质是数据库。那么redis如何用,java代码去编译。
这些疑问我一个个的去解释。
第一部分。让数据能存放的时间,更加持久。那么利用的是。Redis缓存的一个文件。redis.windows.conf

 思考一个问题为什么要学Redis持久性?写看一个案例。

 上面的图是Redis的服务器启动页面。

 上面是一种数据类型。的存放和拿出数据的方式。 思考一个问题,当我关闭了服务器,数据还在吗?

 答案是不在。如果你用自己一天的数据存放的数据用上万个,突然断电。一切清零,你的心情如何?
为了解决上面的问题。所以有了我们接下来要学的,RDB,AOF. 什么是RDB? 什么是AOF? 总之:这两个名称,是来解决。上面的问题的?一个优秀的开发者,都在不断的思考这各自中问题。
接下来进入RDB与AOF学习。首先打开E:Redis-x64-3.2.100,文件下的redis.windows.conf文件。

#
# Save the DB on disk:
#
#   save  
#
#   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文件的学习过程。 接下来我用AOF来操作一遍。
# 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的详细信息。 主要是让你学会操作。 首先打开redis服务器,将你修改的文件信息,进行运行。 win+r 输入cmd 按下 enter  看操作。

Microsoft Windows [版本 10.0.19044.1348]
(c) Microsoft Corporation。保留所有权利。

C:UsersMZFAITHDREAM> 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
 观察上面的代码请看出你的问题。


进入用java语言获得redis中的数据。

 

 导入架包
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;


public class RedisTest {
	@Test
	public void test1() {
		
		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();
	}
		
	
		@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 map=	jedis.hgetAll("student");
	map.forEach((key,value)->System.out.println(key+"="+value));
	jedis.close();

		}
	
	
	//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 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();

	}
	
	//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 oSet =jedis.smembers("myset");
		oSet.forEach(x->System.out.println(x));
		jedis.close();

	}
	
	
	@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 oSet =jedis.zrange("mysort", 0,-1);
	oSet.forEach(s->System.out.println(s));
	jedis.close();
	jedis.zrem("mysort", "小二康");
		
	
		
		
		
	}
	
		}

	
	


运行结果

 Redis第一课https://blog.csdn.net/qq_56248592/article/details/121939650

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/671323.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号