Skip to content

Latest commit

 

History

History
78 lines (62 loc) · 3.03 KB

File metadata and controls

78 lines (62 loc) · 3.03 KB

Valkey PHP - Transactions

Command Description Supported Tested Class/Trait Method
multi Enter and exit transactional mode. Transactions multi
exec Enter and exit transactional mode. Transactions exec
discard Enter and exit transactional mode. Transactions discard
watch Enter and exit transactional mode. Transactions watch
unwatch Enter and exit transactional mode. Transactions unwatch
  • DISCARD Discards a transaction.
  • EXEC Executes all commands in a transaction.
  • MULTI Starts a transaction.
  • UNWATCH Forgets about watched keys of a transaction.
  • WATCH Monitors changes to keys to determine the execution of a transaction.

Usage

$valkey = new Valkey();
$valkey->connect('127.0.0.1', 6379);
$ret = $valkey->multi()
    ->set('key1', 'val1')
    ->get('key1')
    ->set('key2', 'val2')
    ->get('key2')
    ->exec();
  1. multi, exec, discard - Enter and exit transactional mode
  2. watch, unwatch - Watches a key for modifications by another client.

multi, exec, discard.


Description: Enter and exit transactional mode.

Parameters

(optional) Valkey::MULTI or Valkey::PIPELINE. Defaults to Valkey::MULTI. A Valkey::MULTI block of commands runs as a single transaction; a Valkey::PIPELINE block is simply transmitted faster to the server, but without any guarantee of atomicity. discard cancels a transaction.

Return value

multi() returns the Valkey instance and enters multi-mode. Once in multi-mode, all subsequent method calls return the same object until exec() is called.

Example
$ret = $valkey->multi()
    ->set('key1', 'val1')
    ->get('key1')
    ->set('key2', 'val2')
    ->get('key2')
    ->exec();

/*
$ret == [0 => TRUE, 1 => 'val1', 2 => TRUE, 3 => 'val2'];
*/

watch, unwatch


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.

Parameters

keys: string for one key or array for a list of keys

Example
$valkey->watch('x'); // or for a list of keys: $valkey->watch(['x','another key']);
/* long code here during the execution of which other clients could well modify `x` */
$ret = $valkey->multi()
    ->incr('x')
    ->exec();
/*
$ret = FALSE if x has been modified between the call to WATCH and the call to EXEC.
*/