Home > Java > javaTutorial > body text

java: What does Redis do?

怪我咯
Release: 2017-06-26 11:45:14
Original
4561 people have browsed it

Overview

1.Redis

Redis is used A memory-based noSQL database written in C language that supports persistence and high-performance key-value. It mainly stores data with large access volume, frequent changes, and low security requirements.

2.NoSQL

Non-relational database. A relational database is a database built based on a relational model, which reflects the relationships between entities in the real world.

3.BSD protocol

The BSD open source protocol is a protocol that gives users great freedom. Users can modify the source code and publish modifications code after.

4. Message middleware

Middleware that supports and ensures synchronization or asynchronous sending and receiving of messages between distributed applications.

5. Message queue

# In order to effectively control the sending and receiving process of messages, the data structure for storing messages is built into the message middleware. Use queue, first in first out.

6.SOA

Service-Oriented Architecture, service-oriented architecture, divides the application into several coarse-grained, loosely coupled Application modules are then connected through neutral

interfaces.

Second key naming

key01::key02: Multiple layers, adjacent layers are separated by:

Three common operations

  • redis-server.exe redis-windows-conf: Start the server.

  • redis-cli.exe -h 127.0.0.1 -p 6379: Connect to a redis service through IP and port.

  • keys *: View all keys in the current database.

  • config get *: Get all configuration information.

  • #help command: View the meaning of the operation.

  • #help @string: View all string operations.

  • rename key01 key02: Rename the key.

  • #type key: Get the data type.

  • del key: Delete key.

  • flushdb: Clear the current database.

  • flushall: Clear all databases.

Four strings

1. Add

  • set key value: assignment.

  • mset key01 value01 key02 value02: Assign values ​​to multiple keys, atomic, succeed at the same time, and fail at the same time.

  • set key value nx: Can only be set when the key does not exist.

  • #set key value xx: Can only be set when the key exists.

  • set key value ex seonds: Set the timeout of the key.

  • getset key value: Get the value first, then assign it.

2. Delete

del key: Delete key.

3. Change

  • incr key: increase by 1.

  • incrby key increment: Increase the specified amount.

  • incrbyfloat key increment: Increase the amount specified by float type data.

  • decr key: minus 1.

  • decrby key decrement: Decrease the specified amount.

4. Check

  • get key: value.

  • strlen key: Get the string length.

  • exists key: returns 1 if it exists, 0 if it does not exist.

5. Index

Starting from the beginning, the index starts from 0, 1,2....

Starting from the end, the index starts from -1, -2. -3...

  • setrange key index value: Replace starting from the index position.

  • getrange key begin end: Get the value of the specified index range.

  • getrange key 0 -1: Get all.

Five LinkedList

1. LinkedList, a doubly linked list, has low query efficiency and high addition and deletion efficiency.

2. Add

  • LPUSH key value01 value02: Push elements from the left.

  • RPUSH key value02 value02: Push the element from the right.

3. Delete

  • LPOP KEY: pop up an element from the left, quite To remove the element from the list.

  • RPOP KEY: Pop up an element from the right.

  • ltrim key start end: Delete elements outside the specified range.

lrem key count value: Delete count elements with value in the linked list.

  • count>0: Delete from the left.

  • count<0: Delete from the right.

  • count=0: Delete all.

4. Change

LINSERT key before/after oldValue newValue: Insert elements before and after the specified element, if specified If the element does not exist, no operation is performed. If there are multiple specified elements, only the first element will be operated.

RPOPLPUSH key01 key02: Pop an element from the right side of key01 and push it to the left side of key02.

5. Check

llen key: The length is the number of elements in the list.

6. Index

From left to right, starting from 0; from right to left, starting from -1.

  • LRANGE key start end: Get the elements in the specified index range.

  • LRANGE key 0 -1: Get all elements.

  • LINDEX key index: Get the value of the specified index position.

  • LSET key index value: Set the value of the specified index position.

六hash

The key value space stores not only the key value, but also other information, such as timeout Time, so the key takes up a lot of space. You should reduce the number of keys and store related data in the same key. This generates a data type hash, which is equivalent to a HashMap.

In hash, the timeout duration can only be set on the key, not the field.

1. Add

  • hset key field value: Assign a value to a field in key.

  • hmset key field01 value01 field02 value02: Assign values ​​to multiple fields at the same time.

2. Delete

  • hdel key field: Delete the specified field in key.

3. Modify

  • ## hincrby key filed increment: Add an integer field.

  • hincrbyfloat field increment: Increase the value of a floating point field.

4. Check

  • ## hget key field: Get the value of the specified field in key .

  • hmget key field01 field02: Get the values ​​of multiple fields.

  • hgetall key: Get all fields and their corresponding values.

  • hkeys key: Get all field names.

  • # kvals key: Get the values ​​of all fields.

  • hexists key field: Determine whether the specified field exists in the key.

  • # hlen key: Get the number of fields in the key.

Seven set

Unordered, non-repeatable. The so-called disorder means that the same collection is queried at different times, and the elements in the same sorting position are different.

1. Add

  • sadd key value01 vlaue02: Add element.

2. Delete

  • srem key value01 value02: Delete element.

  • # spop key: Randomly remove an element.

3. Check

  • smembers key: Get all elements

  • srandmember key: Returns an element randomly.

  • scard key: Returns the number of elements.

  • sismember key value: Determine whether the value exists.

4. Intersection and union operation

The intersection and union operation will not delete the elements in the operated set .

  • sdiff key01 key02: Find the difference set and delete the elements of key02 from key01. It is just an operation and will not delete the elements in key01.

  • sdiffstore destination key01 key02: Store the difference set in the specified collection.

  • suinon key01 key02: Find the union.

  • sninonstore destination key01 key02: Store the union result in the specified collection.

  • Sinter key01 key02: Find the intersection.

  • Sinterstore destination key01 key02: Get the intersection and store it in the specified collection.

八sortedset

Ordered set, the reason for ordering, each element is associated with a score , sorted according to score.

1. Add

  • zadd key score01 value01 score02 value02.

2. Delete

  • zrem key value: Delete the specified element.

  • zremrangebyrank key start end: Delete elements in the specified sort range.

  • zremrangebyscore key min max: Delete elements in the specified score range.

3. Change

  • zincrby key increment value: Modify the score.

  • zrank key value: Get the sorting, from small to large, starting from 0.

  • zrevrank key value: Get the sorting, from large to small, starting from 0.

4. Check

  • ## zscore key value01: Get the score.

  • zcard key: Get the number of elements in the collection.

  • zrange key start end [with scores]: Get the value of the specified index range.

  • ## zrangebyscore key min max [limit offset count]: Gets the value of the score in the specified interval. Limit is used to intercept count data from the specified offset.

    You can add ( before min to indicate an open interval, and only min can be loaded.

  • zcount key min max: Get the number of elements in the specified score range.

5. Union

zunionstore destination numKeys key[key...] weights weight aggregate max/min/sum
Copy after login
  • destination: Union The collection in which the set results are stored.

  • numKey: The number of collections participating in the operation.

  • weights: Weights, that is, the percentage of scores participating in the calculation, are specified for each set separately.

  • aggregate: integration strategy.

  • zinterstore destination numKeys key[key...] weights weight aggregate max/min/sum: intersection operation.

9 Rdis persistence

1. What is persistence?

The process of saving data to the hard disk is called persistence.

2.Redis persistence method

  • RDB: Redis DB, save all data to dump in binary form .rdb file, enabled by default.

  • AOF: AppendOnlyFile, saves modifications to the database to a file, closed by default.

3.RDB

RDB persistence will generate a dump.rdb file, overwriting the original file. RDB method

  • The user issues a save operation: blocking the server.

  • #The user issues a bgsave operation: it is executed in the background and will not block the server. The principle is to create a sub-thread to generate persistent files.

  • #Configure in the configuration file and automatically persist when specified conditions are met. This is a common method. Configuration conditions: save , only if at least the specified number of write operations occur within the specified time, the bgsave operation will be executed to generate the rdb file.

RDB persistence method saves all the data in the database to the hard disk every time, consuming system resources and cannot be executed frequently. In order to ensure that the RDB interval is within For data security, AOF persistence can be used in parallel.

4.AOF

⑴The execution process of the system writing the data in the memory to the file: first write the data into the buffer, and then the buffer After the area is full, the contents of the buffer are written to the file.

⑵Append the modification operations to the database to the file, with the operations performed first coming first.

⑶Several methods of AOP

  • #always: Every time a modification operation is performed, the file is written immediately.

  • everysec: Write modification operations to the file once every 1 second, default value.

  • #no: Determined by the system, the file will be written after the buffer is full.

⑷Rewriting of AOP files

In order to avoid the AOP file being too large, you can rewrite the AOP file and add multiple operations are merged into one operation.
Rewriting method:
The first method, BGREWRITEAOP: is used to send this operation and rewrite the AOP file.
The second method is to set configuration conditions and automatically rewrite when the conditions are met. The following are the rewriting conditions:

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
Copy after login

Rewrite when the AOP file size reaches a certain value.

Ten clusters

1. A Redis service can have multiple replicas of the service. This Redis service is called the master service, and other replicas The product is called slave service.

2. The master service will synchronize its data to the slave service.

3. The master service can both read and write, and the slave service can only read.

4. There are two ways to set a service as a slave service:

  • The customer issues slaveof masterip masterport .

  • #Configure in the configuration file: slaveof masterip masterport.

5. You can cancel the slave setting operation in the configuration file, or you can cancel it by the customer: slaveof no one

6. Problems with master-slave replication
Only one master server performs write operations. If the master server fails, the write operation cannot be performed.

Eleven sentinal sentinel mechanism

1. What is the sentinel mechanism?

Configure a listener for the master server. This listener is called a sentry. When the master server fails, the sentinel automatically promotes a slave server to the master server. , thereby ensuring uninterrupted execution of the server.

Twelve Twemproxy Cluster

1. In master-slave replication, the write task is still undertaken by one node, and the write pressure is not resolved. Therefore, the Twemproxy cluster mechanism was born. The user sends a request to the proxy, and the proxy allocates the write tasks to the nodes in the server pool.

2. Basic principles of Twenproxy

Each server in the server pool has a receiving interval. After the client sends a request to the proxy, the proxy Get the hashcode of the key. Whichever

range the value falls in, which server is called.

3. Disadvantages

There is a central object, the proxy. If there is a problem with the proxy, the entire Redis service cannot be used. The solution is Decentralization allows a certain part to

failure and the whole can still run.

13 Native cluster mode

1. What is native cluster mode?

Multiple Redis master servers form a cluster. Each Redis master server is called a node. The nodes communicate with each other. The nodes can have their own

slaves. server to share the reading pressure.

2. One of the purposes of communication between nodes

Each node can communicate with all other nodes and act as a sentinel role to monitor other nodes. The operating status of the node. When a node fails

Integrated information from multiple other nodes. If it is judged that the node cannot work normally, promote a slave server of the node to the node.

Fourteen Jedis

1. To access the Redis server in java, you need to import the shelf package jedis.jar.

2. Jedis is a client tool in java language that connects to the Redis server.

3. Jedis basically retains the command line method of accessing the Redis server.

4. Just as it is best to establish a connection pool to connect to a relational database, it is also best to establish a connection pool to connect to the Rdis database. The classes used when establishing a connection pool

JedisPoolConfig/JedisPool.

5. A single Redis server is under great pressure, so you can build a Redis server cluster and use the class HostAndPort/JedisCluster when building a cluster.

The above is the detailed content of java: What does Redis do?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template