The mental journey of learning front-end web ------ non-relational database redis


insert image description here

I. Concept

  1. Concept: redis is a high-performance NOSQL series non-relational database

  2. Comparison of relational and non-relational databases (NOSQL):
    a. Advantages:
      1) Cost: The nosql database is simple and easy to deploy, and it is basically an open source software. It does not need to spend a lot of money to purchase and use it like using oracle, and it is cheaper than relational databases.
      2) Query speed: nosql database stores data in cache, relational database stores data in hard disk, and the natural query speed is far less than that of nosql database.
      3) Format of stored data: The storage format of nosql is key, value form, document form, image form, etc., so it can store various formats such as basic types and objects or collections, while the database only supports basic types.
      4) Scalability: Relational databases have the limitation of multi-table query mechanisms like join, which makes it difficult to expand.
    b. Disadvantages:
      1) The tools and materials for maintenance are limited, because nosql is a new technology and cannot be compared with the technology of relational databases for more than 10 years.
      2) It does not provide support for sql. If it does not support industry standards such as sql, it will result in a certain cost for users to learn and use.
      3) Does not provide relational database transaction processing.
    c. Advantages of non-relational databases:
      1) Performance NOSQL is based on key-value pairs, which can be imagined as the correspondence between primary keys and values ​​in the table, and does not need to be parsed by the SQL layer, so the performance is very high.
      2) Scalability is also based on key-value pairs, and there is no coupling between data, so it is very easy to scale horizontally.
    d. Advantages of relational databases:
      1) Complex queries can easily use SQL statements to do very complex data queries between one table and multiple tables.
      2) Transaction support enables the realization of data access requirements with high security performance. For these two types of databases, each other's strengths are their weaknesses, and vice versa.

3. The key-value data types supported by Redis are as follows:

  1. string type string
  2. Hash type hash: map format
  3. List type list: linkedlist format. Support for repeating elements
  4. Collection type set: Duplicate elements are not allowed
  5. Sorted collection type sortedset: duplicate elements are not allowed, and the elements have an order

2. Download and install

1. 官网:https://redis.io
2. 中文网:http://www.redis.net.cn/
3. 解压直接可以使用:
	* redis.windows.conf:配置文件
	* redis-cli.exe:redis的客户端
	* redis-server.exe:redis服务器端

3. Operation command

  1. The data structure of redis:
  • Redis stores data in the format of key and value, where the key is a string and the value has 5 different data structures

1. String type string

1. 存储: set key value
	127.0.0.1:6379> set username zhangsan
	OK
2. 获取: get key
	127.0.0.1:6379> get username
	"zhangsan"
3. 删除: del key
	127.0.0.1:6379> del age
	(integer) 1

2. Hash type hash

1. 存储: hset key field value
	127.0.0.1:6379> hset myhash username lisi
	(integer) 1
	127.0.0.1:6379> hset myhash password 123
	(integer) 1
2. 获取: 
	* hget key field: 获取指定的field对应的值
		127.0.0.1:6379> hget myhash username
		"lisi"
	* hgetall key:获取所有的field和value
		127.0.0.1:6379> hgetall myhash
		1) "username"
		2) "lisi"
		3) "password"
		4) "123"
			
3. 删除: hdel key field
	127.0.0.1:6379> hdel myhash username
	(integer) 1

3. List type list: You can add an element to the head (left) or tail (right) of the list

1. 添加:
	1. lpush key value: 将元素加入列表左表

	2. rpush key value:将元素加入列表右边	
		127.0.0.1:6379> lpush myList a
		(integer) 1
		127.0.0.1:6379> lpush myList b
		(integer) 2
		127.0.0.1:6379> rpush myList c
		(integer) 3
2. 获取:
	* lrange key start end :范围获取
		127.0.0.1:6379> lrange myList 0 -1
		1) "b"
		2) "a"
		3) "c"
3. 删除:
	* lpop key: 删除列表最左边的元素,并将元素返回
	* rpop key: 删除列表最右边的元素,并将元素返回

4. Collection type set: Duplicate elements are not allowed

1. 存储:sadd key value
	127.0.0.1:6379> sadd myset a
	(integer) 1
	127.0.0.1:6379> sadd myset a
	(integer) 0
2. 获取:smembers key:获取set集合中所有元素
	127.0.0.1:6379> smembers myset
	1) "a"
3. 删除:srem key value:删除set集合中的某个元素	
	127.0.0.1:6379> srem myset a
	(integer) 1

5. Sorted set type sortedset: Duplicate elements are not allowed, and the elements are in order. Each element is associated with a double-type score. Redis sorts the members of the set from small to large through scores.

1. 存储:zadd key score value
	127.0.0.1:6379> zadd mysort 60 zhangsan
	(integer) 1
	127.0.0.1:6379> zadd mysort 50 lisi
	(integer) 1
	127.0.0.1:6379> zadd mysort 80 wangwu
	(integer) 1
2. 获取:zrange key start end [withscores]
	127.0.0.1:6379> zrange mysort 0 -1
	1) "lisi"
	2) "zhangsan"
	3) "wangwu"

	127.0.0.1:6379> zrange mysort 0 -1 withscores
	1) "zhangsan"
	2) "60"
	3) "wangwu"
	4) "80"
	5) "lisi"
	6) "500"
3. 删除:zrem key value
	127.0.0.1:6379> zrem mysort lisi
	(integer) 1

General order
1. keys * : 查询所有的键
2. type key : 获取键对应的value的类型
3. del key:删除指定的key value

4. Endurance

1. RDB:默认方式,不需要进行配置,默认就使用这种机制
	* 在一定的间隔时间中,检测key的变化情况,然后持久化数据
	1. 编辑redis.windwos.conf文件
		#   after 900 sec (15 min) if at least 1 key changed
		save 900 1
		#   after 300 sec (5 min) if at least 10 keys changed
		save 300 10
		#   after 60 sec if at least 10000 keys changed
		save 60 10000
		
	2. 重新启动redis服务器,并指定配置文件名称
		D:\JavaWeb2018\day23_redis\资料\redis\windows-64\redis-2.8.9>redis-server.exe redis.windows.conf	
		
2. AOF:日志记录的方式,可以记录每一条命令的操作。可以每一次命令操作后,持久化数据
	1. 编辑redis.windwos.conf文件
		appendonly no(关闭aof) --> appendonly yes (开启aof)
		# appendfsync always : 每一次操作都进行持久化
		appendfsync everysec : 每隔一秒进行一次持久化
		# appendfsync no	 : 不进行持久化

5. Java client operation

  • Usage steps:
    1. Download the jar package of jedis
    2. Use
//1. 获取连接
    Jedis jedis = new Jedis("localhost",6379);
//2. 操作
   	jedis.set("username","zhangsan");
//3. 关闭连接
    jedis.close();

String String class:

 //1. 获取连接
Jedis jedis = new Jedis();//如果使用空参构造,默认值 "localhost",6379端口
//2. 操作
 //存储
jedis.set("username","zhangsan");
//获取
String username = jedis.get("username");
System.out.println(username);
	
//可以使用setex()方法存储可以指定过期时间的 key value
jedis.setex("activecode",20,"hehe");//将activecode:hehe键值对存入redis,并且20秒后自动删除该键值对

//3. 关闭连接
jedis.close();

List type list:

/*
lpush / rpush
lpop / rpop
lrange start end : 范围获取
*/
//1. 获取连接
Jedis jedis = new Jedis();//如果使用空参构造,默认值 "localhost",6379端口
//2. 操作
// list 存储
jedis.lpush("mylist","a","b","c");//从左边存
jedis.rpush("mylist","a","b","c");//从右边存
	
// list 范围获取
List<String> mylist = jedis.lrange("mylist", 0, -1);
System.out.println(mylist);
	        
// list 弹出
String element1 = jedis.lpop("mylist");//c
System.out.println(element1);
	
String element2 = jedis.rpop("mylist");//c
System.out.println(element2);
	
// list 范围获取
List<String> mylist2 = jedis.lrange("mylist", 0, -1);
System.out.println(mylist2);
	
//3. 关闭连接
jedis.close();

== jedis connection pool: JedisPool==

		   //1.创建Jedis连接池对象
		 JedisPool jedisPool = new JedisPool(config,"localhost",6379);
		
		   //2.获取连接
		 Jedis jedis = jedisPool.getResource();
		  //3. 使用
		 jedis.set("hehe","heihei");
		  //4. 关闭 归还到连接池中
		 jedis.close();
	
	//* 连接池工具类
public class JedisPoolUtils {
    
    

private static JedisPool jedisPool;
			
static{
    
    
		//读取配置文件
		InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
		//创建Properties对象
		Properties pro = new Properties();
		//关联文件
		try {
    
    
			 pro.load(is);
			} catch (IOException e) {
    
    
			e.printStackTrace();
			        }
		//获取数据,设置到JedisPoolConfig中
		JedisPoolConfig config = new JedisPoolConfig();
	    config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
	    config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
			
		//初始化JedisPool
		jedisPool = new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port")));}/**
​				     * 获取连接方法
​				     */public static Jedis getJedis(){
    
    return jedisPool.getResource();}}

insert image description here

Guess you like

Origin blog.csdn.net/S_yyuan/article/details/123702195