PHP code example of webpatser / laravel-resp3-cache
1. Go to this page and download the library: Download webpatser/laravel-resp3-cache library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
Cache::put('{user:42}.profile', $profile);
Cache::put('{user:42}.cart', $cart);
Cache::many(['{user:42}.profile', '{user:42}.cart']); // one round trip
use Illuminate\Support\Facades\Redis;
Redis::subscribe(['user-events'], function (string $message, string $channel) {
echo "[$channel] $message\n";
});
Redis::psubscribe(['user.*'], function (string $message, string $channel, string $pattern) {
// ...
});
use Illuminate\Support\Facades\Redis;
// Subscriber: every channel must hash to the same slot. Use {hash tag}
// syntax to colocate them, exactly like multi-key MGET / MSET.
Redis::connection()->ssubscribe(
['orders.{user42}.created', 'orders.{user42}.updated'],
function (string $message, string $channel) {
// ...
},
);
// Publisher: route via SPUBLISH (not PUBLISH) so the message lands on
// the same shard as the subscribers.
Redis::connection()->command('SPUBLISH', ['orders.{user42}.created', $payload]);