Nextrek
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Pipeline;
use Iterator;
use SplQueue;
use Predis\ResponseErrorInterface;
use Predis\ResponseObjectInterface;
use Predis\ServerException;
use Predis\Command\CommandInterface;
use Predis\Connection\ConnectionInterface;
use Predis\Connection\ReplicationConnectionInterface;
/**
* Implements the standard pipeline executor strategy used
* to write a list of commands and read their replies over
* a connection to Redis.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class StandardExecutor implements PipelineExecutorInterface
{
protected $exceptions;
/**
* @param bool $exceptions Specifies if the executor should throw exceptions on server errors.
*/
public function __construct($exceptions = true)
{
$this->exceptions = (bool) $exceptions;
}
/**
* Allows the pipeline executor to perform operations on the
* connection before starting to execute the commands stored
* in the pipeline.
*
* @param ConnectionInterface $connection Connection instance.
*/
protected function checkConnection(ConnectionInterface $connection)
{
if ($connection instanceof ReplicationConnectionInterface) {
$connection->switchTo('master');
}
}
/**
* Handles a response object.
*
* @param ConnectionInterface $connection
* @param CommandInterface $command
* @param ResponseObjectInterface $response
* @return mixed
*/
protected function onResponseObject(ConnectionInterface $connection, CommandInterface $command, ResponseObjectInterface $response)
{
if ($response instanceof ResponseErrorInterface) {
return $this->onResponseError($connection, $response);
}
if ($response instanceof Iterator) {
return $command->parseResponse(iterator_to_array($response));
}
return $response;
}
/**
* Handles -ERR responses returned by Redis.
*
* @param ConnectionInterface $connection The connection that returned the error.
* @param ResponseErrorInterface $response The error response instance.
* @return ResponseErrorInterface
*/
protected function onResponseError(ConnectionInterface $connection, ResponseErrorInterface $response)
{
if (!$this->exceptions) {
return $response;
}
// Force disconnection to prevent protocol desynchronization.
$connection->disconnect();
$message = $response->getMessage();
throw new ServerException($message);
}
/**
* {@inheritdoc}
*/
public function execute(ConnectionInterface $connection, SplQueue $commands)
{
$this->checkConnection($connection);
foreach ($commands as $command) {
$connection->writeCommand($command);
}
$values = array();
while (!$commands->isEmpty()) {
$command = $commands->dequeue();
$response = $connection->readResponse($command);
if ($response instanceof ResponseObjectInterface) {
$values[] = $this->onResponseObject($connection, $command, $response);
} else {
$values[] = $command->parseResponse($response);
}
}
return $values;
}
}
|
Revision |
Author |
Commited |
Message |
1464
|
MOliva
|
Tue 13 Oct, 2020 11:16:56 +0000 |
|