8000 transactions, pub/sub sections, toc · jrtkcoder/phpredis@0cf7c88 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0cf7c88

Browse files
author
Tit Petric
committed
transactions, pub/sub sections, toc
1 parent 328f599 commit 0cf7c88

File tree

1 file changed

+123
-108
lines changed

1 file changed

+123
-108
lines changed

README.markdown

Lines changed: 123 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ You can send comments, patches, questions [here on github](https://github.com/ni
2222
* [Lists](#lists)
2323
* [Sets](#sets)
2424
* [Sorted sets](#sorted-sets)
25-
* Pub/sub
26-
* Transactions
25+
* [Pub/sub](#pubsub)
26+
* [Transactions](#transactions)
2727
* [Scripting](#scripting)
2828

2929
-----
@@ -623,112 +623,6 @@ $redis->delete('key1', 'key2'); /* return 2 */
623623
$redis->delete(array('key3', 'key4')); /* return 2 */
624624
~~~
625625

626-
### multi, exec, discard.
627-
-----
628-
_**Description**_: Enter and exit transactional mode.
629-
630-
##### *Parameters*
631-
(optional) `Redis::MULTI` or `Redis::PIPELINE`. Defaults to `Redis::MULTI`. A `Redis::MULTI` block of commands runs as a single transaction; a `Redis::PIPELINE` block is simply transmitted faster to the server, but without any guarantee of atomicity. `discard` cancels a transaction.
632-
633-
##### *Return value*
634-
`multi()` returns the Redis instance and enters multi-mode. Once in multi-mode, all subsequent method calls return the same object until `exec()` is called.
635-
636-
##### *Example*
637-
~~~
638-
$ret = $redis->multi()
639-
->set('key1', 'val1')
640-
->get('key1')
641-
->set('key2', 'val2')
642-
->get('key2')
643-
->exec();
644-
645-
/*
646-
$ret == array(
647-
0 => TRUE,
648-
1 => 'val1',
649-
2 => TRUE,
650-
3 => 'val2');
651-
*/
652-
~~~
653-
654-
### watch, unwatch
655-
-----
656-
_**Description**_: Watches a key for modifications by another client. If the key is modified between `WATCH` and `EXEC`, the MULTI/EXEC transaction will fail (return `FALSE`). `unwatch` cancels all the watching of all keys by this client.
657-
658-
##### *Parameters*
659-
*keys*: a list of keys
660-
661-
##### *Example*
662-
~~~
663-
$redis->watch('x');
664-
/* long code here during the execution of which other clients could well modify `x` */
665-
$ret = $redis->multi()
666-
->incr('x')
667-
->exec();
668-
/*
669-
$ret = FALSE if x has been modified between the call to WATCH and the call to EXEC.
670-
*/
671-
~~~
672-
673-
### subscribe
674-
-----
675-
_**Description**_: Subscribe to channels. Warning: this function will probably change in the future.
676-
677-
##### *Parameters*
678-
*channels*: an array of channels to subscribe to
679-
*callback*: either a string or an array($instance, 'method_name'). The callback function receives 3 parameters: the redis instance, the channel name, and the message.
680-
681-
##### *Example*
682-
~~~
683-
function f($redis, $chan, $msg) {
684-
switch($chan) {
685-
case 'chan-1':
686-
...
687-
break;
688-
689-
case 'chan-2':
690-
...
691-
break;
692-
693-
case 'chan-2':
694-
...
695-
break;
696-
}
697-
}
698-
699-
$redis->subscribe(array('chan-1', 'chan-2', 'chan-3'), 'f'); // subscribe to 3 chans
700-
~~~
701-
702-
### psubscribe
703-
-----
704-
_**Description**_: Subscribe to channels by pattern
705-
706-
##### *Parameters*
707-
*patterns*: An array of patterns to match
708-
*callback*: Either a string or an array with an object and method. The callback will get four arguments ($redis, $pattern, $channel, $message)
709-
710-
##### *Example*
711-
~~~
712-
function psubscribe($redis, $pattern, $chan, $msg) {
713-
echo "Pattern: $pattern\n";
714-
echo "Channel: $chan\n";
715-
echo "Payload: $msg\n";
716-
}
717-
~~~
718-
719-
### publish
720-
-----
721-
_**Description**_: Publish messages to channels. Warning: this function will probably change in the future.
722-
723-
##### *Parameters*
724-
*channel*: a channel to publish to
725-
*messsage*: string
726-
727-
##### *Example*
728-
~~~
729-
$redis->publish('chan-1', 'hello, world!'); // send message.
730-
~~~
731-
732626

733627
### exists
734628
-----
@@ -2704,6 +2598,127 @@ $redis->zUnion('ko2', array('k1', 'k2'), array(1, 1)); /* 4, 'ko2' => array('val
27042598
$redis->zUnion('ko3', array('k1', 'k2'), array(5, 1)); /* 4, 'ko3' => array('val0', 'val2', 'val3', 'val1') */
27052599
~~~
27062600

2601+
## Pub/sub
2602+
2603+
* [psubscribe](#psubscribe) - Subscribe to channels by pattern
2604+
* [publish](#publish) - Post a message to a channel
2605+
* [subscribe](#subscribe) - Subscribe to channels
2606+
2607+
### psubscribe
2608+
-----
2609+
_**Description**_: Subscribe to channels by pattern
2610+
2611+
##### *Parameters*
2612+
*patterns*: An array of patterns to match
2613+
*callback*: Either a string or an array with an object and method. The callback will get four arguments ($redis, $pattern, $channel, $message)
2614+
2615+
##### *Example*
2616+
~~~
2617+
function psubscribe($redis, $pattern, $chan, $msg) {
2618+
echo "Pattern: $pattern\n";
2619+
echo "Channel: $chan\n";
2620+
echo "Payload: $msg\n";
2621+
}
2622+
~~~
2623+
2624+
### publish
2625+
-----
2626+
_**Description**_: Publish messages to channels. Warning: this function will probably change in the future.
2627+
2628+
##### *Parameters*
2629+
*channel*: a channel to publish to
2630+
*messsage*: string
2631+
2632+
##### *Example*
2633+
~~~
2634+
$redis->publish('chan-1', 'hello, world!'); // send message.
2635+
~~~
2636+
2637+
### subscribe
2638+
-----
2639+
_**Description**_: Subscribe to channels. Warning: this function will probably change in the future.
2640+
2641+
##### *Parameters*
2642+
*channels*: an array of channels to subscribe to
2643+
*callback*: either a string or an array($instance, 'method_name'). The callback function receives 3 parameters: the redis instance, the channel name, and the message.
2644+
2645+
##### *Example*
2646+
~~~
2647+
function f($redis, $chan, $msg) {
2648+
switch($chan) {
2649+
case 'chan-1':
2650+
...
2651+
break;
2652+
2653+
case 'chan-2':
2654+
...
2655+
break;
2656+
2657+
case 'chan-2':
2658+
...
2659+
break;
2660+
}
2661+
}
2662+
2663+
$redis->subscribe(array('chan-1', 'chan-2', 'chan-3'), 'f'); // subscribe to 3 chans
2664+
~~~
2665+
2666+
2667+
## Transactions
2668+
2669+
1. [multi, exec, discard](#multi-exec-discard) - Enter and exit transactional mode
2670+
2. [watch, unwatch](#watch-unwatch) - Watches a key for modifications by another client.
2671+
2672+
### multi, exec, discard.
2673+
-----
2674+
_**Description**_: Enter and exit transactional mode.
2675+
2676+
##### *Parameters*
2677+
(optional) `Redis::MULTI` or `Redis::PIPELINE`. Defaults to `Redis::MULTI`. A `Redis::MULTI` block of commands runs as a single transaction; a `Redis::PIPELINE` block is simply transmitted faster to the server, but without any guarantee of atomicity. `discard` cancels a transaction.
2678+
2679+
##### *Return value*
2680+
`multi()` returns the Redis instance and enters multi-mode. Once in multi-mode, all subsequent method calls return the same object until `exec()` is called.
2681+
2682+
##### *Example*
2683+
~~~
2684+
$ret = $redis->multi()
2685+
->set('key1', 'val1')
2686+
->get('key1')
2687+
->set('key2', 'val2')
2688+
->get('key2')
2689+
->exec();
2690+
2691+
/*
2692+
$ret == array(
2693+
0 => TRUE,
2694+
1 => 'val1',
2695+
2 => TRUE,
2696+
3 => 'val2');
2697+
*/
2698+
~~~
2699+
2700+
### watch, unwatch
2701+
-----
2702+
_**Description**_: Watches a key for modifications by another client.
2703+
2704+
If the key is modified between `WATCH` and `EXEC`, the MULTI/EXEC transaction will fail (return `FALSE`). `unwatch` cancels all the watching of all keys by this client.
2705+
2706+
##### *Parameters*
2707+
*keys*: a list of keys
2708+
2709+
##### *Example*
2710+
~~~
2711+
$redis->watch('x');
2712+
/* long code here during the execution of which other clients could well modify `x` */
2713+
$ret = $redis->multi()
2714+
->incr('x')
2715+
->exec();
2716+
/*
2717+
$ret = FALSE if x has been modified between the call to WATCH and the call to EXEC.
2718+
*/
2719+
~~~
2720+
2721+
27072722

27082723
## Scripting
27092724

0 commit comments

Comments
 (0)
0