Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the callback event triggered by php snooping redis key failure

Shulou Source: shulou.com Published: 2022-06-03 05:25:26 10月12日 Update

What this article shares with you is what is the callback event triggered by the failure of php snooping redis key. The editor thinks it is very practical, so I share it with you. I hope you can learn something after reading this article. Let's take a look at it with the editor.

Solution to order timeout and activity expiration: php snooping redis key failure triggers callback events

Redis version 2.8.0 is available, key space message (Redis Keyspace Notifications), with version 2.0.0 after the SUBSCRIBE can complete the operation of this scheduled task, the timing unit is seconds.

1. Let's subscribe to the channel named redisChat first.

two。 Now, let's reopen a redis client, then post the message on the same channel redisChat, and the subscriber can receive the message.

The message received is as follows:

Redis configuration for 3.Key expiration events

Here you need to configure the parameter "Ex" for notify-keyspace-events. X represents an expired event. After notify-keyspace-events "Ex" saves the configuration, restart the Redis service to make the configuration take effect.

PHP redis implements subscription key space notification

The redis instantiation class:

Redis.class.php

/ / Redis2class Redis2 {private $redis; public function _ construct ($host = '127.0.0.1, $port = 6379) {$this- > redis = new Redis (); $this- > redis- > connect ($host, $port);} public function setex ($key, $time, $val) {return $this- > redis- > setex ($key, $time, $val) } public function set ($key, $val) {return $this- > redis- > set ($key, $val);} public function get ($key) {return $this- > redis- > get ($key);} public function expire ($key = null, $time = 0) {return $this- > redis- > expire ($key, $time);} public function psubscribe ($patterns = array (), $callback) {$this- > redis- > psubscribe ($patterns, $callback) } public function setOption () {$this- > redis- > setOption (\ Redis::OPT_READ_TIMEOUT,-1);}}

Subscriptions to expired events:

Psubscribe.php

Require_once'. / Redis.class.php';$redis = new\ Redis2 (); / / solve the Redis client subscription timeout $redis- > setOption (); $redis- > psubscribe (array ('_ keyevent@0__:expired'), 'keyCallback'); / / callback function, where the processing logic function keyCallback ($redis, $pattern, $chan, $msg) {echo "Pattern: $pattern\ n"; echo "Channel: $chan\ n" Echo "Payl oad: $msg\ n\ n"; / / keyCallback is the callback function after the subscription event. The business processing logic is written here. / / for example, if the goods mentioned above are automatically withdrawn without payment, you can automatically withdraw orders according to the order id.

Set expiration events:

Index.php

Require_once'. / Redis.class.php';$redis = new\ Redis2 (); $order_id = 123 leading events-> setex ('order_id',10,$order_id)

Execute psubscribe.php in command line mode first

Access index.php in a browser

This is what the callback event triggered by the failure of php snooping redis key is. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Events subscriptions messages configuration monitoring channels functions customers clients more versions knowledge space articles orders logic processing utility business representatives Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi Microsoft Shulou Tech Info vpn