Nosql and Redis

1.NoSQL

a) Refers to some non-relational databases. High read and write performance, storage will expire, and data will be stored in memory

b) Classification

ii. Mainly used for caching

2.Redis

a) High-performance open source Philippine relational database, data is stored in memory or disk

b) Redis is stored in the form of key-value, which is different from traditional relational databases. It does not necessarily follow some basic requirements of traditional databases. For example, it does not follow SQL standards, transactions, table structures, etc. Redis is strictly not a database, it should be a collection of data structured storage methods. Data structure: array, list, set, map and other redis provide some operation methods, we can use these methods to store strings and organize them into various types of database structures (string, list, set, map, etc.).

c) Advantages of Redis database

i. Data storage: stored in memory, also supports persistence.-Fast access speed, strong concurrency, high data security
Also Support cluster (master-slave synchronization) (support high concurrency, massive data), data can be synchronized from the master server to any number of slave servers, and the slave server can be the master server associated with other slave servers.

ii. Types of storage values ​​it supports

  1. String,list,set,sorted set,hash
  2. Support cluster (support master-slave synchronization )
  3. Support persistence

d) Usage scenarios

i. Caching: For frequently queried data, put it in Redis. It will improve query efficiency and reduce database pressure

ii. ii. Counter reference: number of WeChat likes

p

P Real-time prevention p>e) Redis server installation

i. Start the Redis service

  1. Start by double-clicking redis-service.exe in the installation directory
  2. < li>Cmd start:

ii. Redis client operation Redis

  1. Operation for value as string (String)

a) Set a single key value: set

b) Set multiple key values: mset

  1. Operation on the key

< p>a) Keys: Get a list of all keys

b) Del key: Delete key value

c) expire key xx //Set the expiration time of the key (expires in xx seconds) )

d) ttl key: view the expiration time of the key

e) flushall: clear all data in the Redis database

f) flushdb: clear the current database Data, Redis defaults to 16 databases 0,1….15

  1. For lis Operation of t collection
    How does rdis implement stack (FILO) and queue (FIFO)???
    List controls the same side to enter, and the same side to exit is the stack
    List control while entering and exiting is the queue
  2. Operation on Set collection
  3. Operation on SortedSet (ordered set)
  4. Operation on hash type
  5. Redis set password

a) Set password through commands
CONFIG SET The command can dynamically adjust the configuration of the Redis server without restarting, and it will become invalid after restarting

CONFIG SET requirepass 123456 //Set the password to 123456

CONFIG SET requirepass “” //Clear the password

AUTH 123456 //Enter the password for authentication

b) Modify the Redis configuration file to set the password
Add a line of code to the configuration file redis.widows.conf

requirepass 123456

Set the password 123456 to the configuration file, and load the file when redis starts to enable the password

3. Java operates the Redis server and uses the Jedis client

a) Jedis simple operation
Import jedis package
@Test
public void test()throws Exception{< br> //Create a connection to the Redis server
String host =“127.0.0.1”;
int port = 6379;
int timeout = 1000;//Timeout, 1 second timeout
Jedis jedis = < strong>new
Jedis(h ost,port,timeout);
//Password authentication
jedis.auth(“admin”);
//Perform operation , Set the key and value values.
jedis.set(“yhptest”,“yhptest dbl!”);
System.out.println(jedis.get(“yhptest”));
//Close connection
jedis.close();
}

b) Connection pool configuration

I. Simple operation of Redis database through jedis connection pool
/Idea: If you need to set a lot of values ​​for an object after creating it, it is better to create it first, configure the object and finish the configuration, and then create it through the configuration object
//1 Create jedispool configuration object
//2 Do configuration-four
//3 Create jedispool
//4 Get connection through jedispool
//5 Perform operation
// 6 Release connection< br> // 7 Destroy the connection pool-if it is a real project, it should be a singleton managed by spring
@Test
public void test()throws Exception{
//1 Create jedispool configuration object
JedisPoolConfig config = new < /strong>JedisPoolConfig();
//2 Do configuration-four
config.setMaxIdle(2);
config.setMaxTotal(10 );
config.se tMaxWaitMillis(1*1000); //Create connection timeout
config.setTestOnBorrow(true);//Getting the connection is to test whether the connection is unblocked
//3 Create jedispool
//1*1000 Get the connection timeout
JedisPool pool = new JedisPool(config,
“127.0.0.1”,6379,1*1000,“admin”);
//4 Get connections through jedispool
Jedis jedis = pool.getResource();
//5 Perform operations< /em>
jedis.set(“jedispooltest”,“dbldblddzt…..”);
System. out.println(jedis.get(“jedispooltest”));
// 6 Release the connection
jedis.close(); //The bottom layer is compatible. If it is a connection pool operation, it is released. If it is The connection operation is to close
// 7 Destroy the connection pool-if it is a real project, it should be a singleton managed by spring
pool.destroy();
}
Summary : First create the Jedis connection pool configuration object, and then create the connection pool. When creating the connection pool, pass in the connection pool configuration object, the ip address stored by redis, the Redis port number, set the connection timeout time and the user operating Redis, and the connection pool will get it. jedis object, jedis object operation server. Close connection pool and jedis object

Operation c) Jedis data structure

i. Key value of the operation

ii. String operations

iii. List Operation

iv. Set operation

Des v. Hash operation

And asc method sorting

  • String letters are sorted by calling the alpha method.
  • Redis persistence configuration
  • a) Two ways of persistence

    I. Load the RDB and AOF. You can configure it by modifying the Redis conf configuration file

    Determine whether the F server is activated ii. Persistence, load the AOF file when it is turned on, and load the RDB file when it is not opened

    iii. RDB mode

    1. RDB mode can be used at a specified time interval The point-in-time snapshot of the data set generated in the internal, the RDB mode is turned on by default,
    2. How to turn off the RDB persistence method that is turned on by default: In the Redis.conf file, a time snapshot of the data set will be generated at the specified time. A note, if there is a sudden power failure in the middle, it cannot be persisted to the disk
      save “”

      # save 900 1 //At least one change to the storage within a period of 900 seconds Sync once

      # save xxx

      # save 60 10000

    iv. AOF mode

    1. AO F Persistently records all write operation commands executed by the server, and restores the data set by re-executing these commands when the server starts, and this mode is turned off by default.
    2. How to turn on AOF

    a) appendonly yes //yes on, no off

    # appendfsync always // every time a new command is written Execute fsync synchronization once at any time

    #Here we enable everysec

    appendfsync everysec //fsync synchronization once per second

    # appendfsync no //Never fsync( Hand it over to the operating system for processing, it may take a long time to execute fsync), its parameters, please see the redis.conf configuration file for detailed explanation

    Summary: How does Redis save data.

    < ol>

  • In order to consider efficiency, redis saves data in the content. And considers data security, and also does data persistence. If the preservation strategy is met, the data in the memory will be saved to the data rdb file, and it is too late to save that part The data is stored in the aof update log. When loading, make a union of the two data.
  • Redis elimination strategy: select the appropriate data to eliminate
  • volatile-lru: From a data set with an expiration time set (server.db[i] .expires) select the least recently used data to eliminate

    volatile-ttl: Select the data to be expired from the data set (server.db[i].expires) for which the expiration time has been set.

    volatile-random: From a data set with an expiration time set (server.db[i].expires) arbitrarily select data elimination

    allkeys-lru: From the data set (server.db[i].dict) Select the least recently used data to eliminate

    allkeys-random: arbitrarily select data elimination from the data set (server.db[i].dict)< /p>

    no-enviction (eviction): prohibits eviction of data

    After redis determines to evict a key-value pair, it will delete the data and change the message Publish to local (AOF persistence) and slave (master-slave connection).

    Leave a Comment

    Your email address will not be published.