Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Redis Note arrangement (3): advanced Operations and Advanced parts

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

[TOC]

Redis publish subscription Redis publish subscription (pub/sub) is a mode of message communication: the sender (pub) sends messages and the subscriber (sub) receives messages. Redis clients can subscribe to any number of channels. The following figure shows the relationship between the channel channel1 and the three clients that subscribe to the channel, client1,client2,client5. When a new message is sent to channel channel1 through the PUBLISH command, the message is sent to the three clients that subscribe to it:

The relevant operation commands are as follows:

Command to describe PSUBSCRIBE pattern [pattern...] Subscribe to one or more channels that match a given pattern PUSBSUB subcommand [argument [argument...]] View subscription and publish system status PUBLISH channel message sends messages to the specified channel PUNSUBSCRIBE [pattern [pattern...]] Unsubscribe from all channels of a given mode SUBSCRIBE channel [channel...] Subscribe to information for a given channel or channels UNSUBSCRIBE [channel [channel...]] To unsubscribe from a given channel

Examples are as follows:

The subscription channel created is named redisChatlocalhost:6379 > SUBSCRIBE redisChat1) "subscribe" 2) redisChat reopens a new redis client, then posts messages on the same channel redisChat twice, and subscribers can receive the messages. 127.0.0.1 6379 > PUBLISH redisChat "jack is handsome boy" at this point you can quickly see the message on the subscriber side. Redis transactions Redis transactions can execute more than one command at a time with two important guarantees: a transaction is a separate isolated operation: all commands in a transaction are serialized and executed sequentially. In the course of execution, the transaction will not be interrupted by command requests sent by other clients. A transaction is an atomic operation: either all or none of the commands in the transaction are executed. A transaction goes through three stages from start to execution: start the transaction. Order to join the team. Execute the transaction.

The relevant operation commands are as follows:

Command description DISCARD cancels the transaction, abandons all commands within the transaction block EXEC executes commands within all transaction blocks MULTI marks the beginning of a transaction block UNWATCH cancels the WATCH command to monitor WATCH key for all key [key.] Monitor one or more key. If these key are changed by other commands before the transaction is executed, the transaction will be interrupted.

Examples are as follows:

Uplooking01:7001 > get name "xpleaf" uplooking01:7001 > MULTIOKuplooking01:7001 > get nameQUEUEDuplooking01:7001 > set name yyhQUEUEDuplooking01:7001 > get nameQUEUEDuplooking01:7001 > EXEC1) "xpleaf" 2) OK3) "yyh" Redis commands summarize the common commands of Redis are mainly divided into two aspects, one is key-value-related commands, the other is server-related commands 1, The key-value-related command keys * takes out all the current key exists name to check whether the redis has name this key del name deletes the key name expire confirm 100 setting confirm this key100 second expires ttl confirm gets the valid duration of the confirm this key select 0 to 0 database redis default database is 0,15, a total of 16 databases move confirm 1 moves the key in the current database to another database Persist confirm removes the expiration time of the key confirm randomkey randomly returns a key rename key2 key3 in the database renaming key2 to key3 type key2 returns the data type of key 2. The server-related command ping PONG returns the response whether the connection was successful or not. Echo prints some content on the command line: database quit / exit with the number of select 0per15 exit client dbsize returns the number of all key in the current database info returns the relevant information of redis config get dir/* real-time transfer storage received request flushdb delete the currently selected database Delete database Redis security from all databases in all key flushall in

We can set the password parameters through the redis configuration file, so that the client needs password authentication to connect to the redis service, which makes your redis service more secure.

We can see if password authentication is set with the following command:

Uplooking01:7001 > config get requirepass1) "requirepass" 2) ""

By default, the requirepass parameter is empty, which means that you can connect to the redis service without password authentication. If you set a password, password authentication is required for the client to connect to the redis service. Otherwise, the command cannot be executed, and the authentication can be completed in two ways:

You can specify a password when you connect: redis-cli-h uplooking03-a uplooking can also connect to the terminal and then authenticate: auth uplookingRedis pipeline and performance test Redis is a TCP service based on client-server model and request / response protocol. This means that usually a request follows the following steps: the client sends a query request to the server and listens for scoket returns, usually in blocking mode, waiting for the server to respond. The server processes the command and returns the result to the client. Redis pipeline technology can be in the server end response, the client can continue to send requests to the server, and finally read the corresponding of all servers at once.

Here is the Java code for the test:

Package com.uplooking.bigdata;import com.uplooking.bigdata.common.util.redis.JedisUtil;import org.junit.Test;import redis.clients.jedis.Jedis;import redis.clients.jedis.Pipeline;/** * comparison of performance tests with and without pipes * / public class PipelineTest {@ Test public void testPipeline () {int count = 10000 / / Mark the start time long start = System.currentTimeMillis () when the pipe operation is not used; / / perform the operation withoutPipeline (count) without the pipe operation; / / mark the end time long end = System.currentTimeMillis () when the pipe operation is not used / / output System.out.println ("withoutPipeline:" + (end-start)) when operating without pipes; / / Mark the start time of operations using pipes start = System.currentTimeMillis (); / / use pipes to perform operations usePipeline (count) / / Mark the end time when using the pipe operation end = System.currentTimeMillis (); / / output the time consumed when using the pipe operation System.out.println ("usePipeline:" + (end-start));} private void withoutPipeline (int count) {JedisUtil.getJedis (); Jedis jedis = JedisUtil.getJedis (); for (int I = 0; I < count) ) {jedis.incr ("testKey1");} cleanUp (jedis);} private void usePipeline (int count) {Jedis jedis = JedisUtil.getJedis (); Pipeline pl = jedis.pipelined (); for (int I = 0; I < count; iTunes +) {pl.incr ("testKey1");} pl.sync (); cleanUp (jedis) } public void cleanUp (Jedis jedis) {JedisUtil.returnJedis (jedis);}}

JedisUtil can look at the previous code, which is not given here.

The output is as follows:

WithoutPipeline: 1935usePipeline: 60

There is still a significant gap in the test results, so there are obvious advantages to using pipeline for multiple operations.

Redis performance test: read and write ability View Redis performance test is achieved by executing multiple commands at the same time. Syntax redis-benchmark [option] [option-value] instance the following instances simultaneously execute 10000 requests to test performance: 1), redis-benchmark-n 100000 2), redis-benchmark-h localhost-p 6379-t set,lpush-n 100000-Q

The common command options are as follows:

Option description default value-h specify server hostname 127.0.0.1Meip specify server port 6379Meis specify server socket-c specify number of concurrent connections 50-n specify number of requests 10000-d specify data size of set/ get value in bytes 2-k1=keep alive 0=reconnect1-rset/get/incr use random key,sadd use random value-P pipe transfer request 1Muq force exit redis. Show only query/sec values-csv outputs the-l production loop in CSV format, permanent execution of the test-t only runs the comma-separated list of test commands-IIdle mode. Open only N idle connections and wait for the full test case [uplooking@uplooking01 ~] $redis-benchmark-h uplooking01-p 6379-n 100000-c 20 = PING_INLINE = 100000 requests completed in 1.29 seconds 20 parallel clients 3 bytes payload keep alive: 199.96%

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report