Skip to content

Commit e02d59a

Browse files
author
Pavle Lee
committed
Better compatibility with yii2-redis
1 parent 9720d48 commit e02d59a

14 files changed

+236
-104
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

src/ActiveQuery.php

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/ActiveRecord.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Command/ClientList.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* User: Pavle Lee <523260513@qq.com>
4+
* Date: 2017/8/26
5+
* Time: 16:29
6+
*/
7+
8+
namespace pavle\yii\redis\Command;
9+
10+
11+
class ClientList extends \Predis\Command\ServerClient
12+
{
13+
/**
14+
* @inheritDoc
15+
*/
16+
public function getArguments()
17+
{
18+
return ['LIST'];
19+
}
20+
21+
22+
/**
23+
* Parses the response to CLIENT LIST and returns a structured list.
24+
*
25+
* @param string $data Response buffer.
26+
*
27+
* @return string
28+
*/
29+
protected function parseClientList($data)
30+
{
31+
return $data;
32+
}
33+
}

src/Command/ClientSetName.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* User: Pavle Lee <523260513@qq.com>
4+
* Date: 2017/8/26
5+
* Time: 16:29
6+
*/
7+
8+
namespace pavle\yii\redis\Command;
9+
10+
11+
class ClientSetName extends \Predis\Command\ServerClient
12+
{
13+
/**
14+
* @inheritDoc
15+
*/
16+
public function getArguments()
17+
{
18+
return array_merge(['SETNAME'], parent::getArguments());
19+
}
20+
21+
22+
/**
23+
* Parses the response to CLIENT LIST and returns a structured list.
24+
*
25+
* @param string $data Response buffer.
26+
*
27+
* @return string
28+
*/
29+
protected function parseClientList($data)
30+
{
31+
return $data;
32+
}
33+
}

src/Command/HashGetAll.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* User: Pavle Lee <523260513@qq.com>
4+
* Date: 2017/8/26
5+
* Time: 16:11
6+
*/
7+
8+
namespace pavle\yii\redis\Command;
9+
10+
11+
use Predis\Command\Command;
12+
13+
class HashGetAll extends Command
14+
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
public function getId()
19+
{
20+
return 'HGETALL';
21+
}
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function parseResponse($data)
27+
{
28+
return $data;
29+
}
30+
}

src/Connection.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@
77

88
namespace pavle\yii\redis;
99

10+
use pavle\yii\redis\Profile\RedisVersion200;
11+
use pavle\yii\redis\Profile\RedisVersion220;
12+
use pavle\yii\redis\Profile\RedisVersion240;
13+
use pavle\yii\redis\Profile\RedisVersion260;
14+
use pavle\yii\redis\Profile\RedisVersion280;
15+
use pavle\yii\redis\Profile\RedisVersion300;
16+
use pavle\yii\redis\Profile\RedisVersion320;
1017
use Predis\Client;
18+
use Predis\Profile\Factory;
1119
use yii\db\Exception;
1220
use yii\helpers\VarDumper;
1321

@@ -28,6 +36,24 @@ class Connection extends \yii\redis\Connection
2836
*/
2937
private $_socket = false;
3038

39+
/**
40+
* @inheritDoc
41+
*/
42+
public function init()
43+
{
44+
parent::init();
45+
46+
Factory::define('2.0', RedisVersion200::class);
47+
Factory::define('2.2', RedisVersion220::class);
48+
Factory::define('2.4', RedisVersion240::class);
49+
Factory::define('2.6', RedisVersion260::class);
50+
Factory::define('2.8', RedisVersion280::class);
51+
Factory::define('3.0', RedisVersion300::class);
52+
Factory::define('3.2', RedisVersion320::class);
53+
Factory::define('dev', 'Predis\Profile\RedisUnstable');
54+
Factory::define('default', RedisVersion320::class);
55+
}
56+
3157
/**
3258
* Returns a value indicating whether the DB connection is established.
3359
* @return bool whether the DB connection is established
@@ -113,7 +139,7 @@ public function executeCommand($name, $params = [])
113139
{
114140
$this->open();
115141

116-
\Yii::trace("Executing Redis Command: {$name}", __METHOD__);
142+
\Yii::trace("Executing Redis Command: {$name} " . join(' ', $params), __METHOD__);
117143

118144
return $this->_socket->executeCommand(
119145
$this->_socket->createCommand($name, $params)

src/Profile/RedisVersion200.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace pavle\yii\redis\Profile;
4+
5+
6+
class RedisVersion200 extends \Predis\Profile\RedisVersion200
7+
{
8+
public function getSupportedCommands()
9+
{
10+
return array_merge(parent::getSupportedCommands(), [
11+
'HGETALL' => 'pavle\yii\redis\Command\HashGetAll',
12+
'CLIENT LIST' => 'pavle\yii\redis\Command\ClientList',
13+
'CLIENT SETNAME' => 'pavle\yii\redis\Command\ClientSetName',
14+
]);
15+
}
16+
}

src/Profile/RedisVersion220.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace pavle\yii\redis\Profile;
4+
5+
6+
class RedisVersion220 extends \Predis\Profile\RedisVersion220
7+
{
8+
public function getSupportedCommands()
9+
{
10+
return array_merge(parent::getSupportedCommands(), [
11+
'HGETALL' => 'pavle\yii\redis\Command\HashGetAll',
12+
'CLIENT LIST' => 'pavle\yii\redis\Command\ClientList',
13+
'CLIENT SETNAME' => 'pavle\yii\redis\Command\ClientSetName',
14+
]);
15+
}
16+
}

src/Profile/RedisVersion240.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace pavle\yii\redis\Profile;
4+
5+
6+
class RedisVersion240 extends \Predis\Profile\RedisVersion240
7+
{
8+
public function getSupportedCommands()
9+
{
10+
return array_merge(parent::getSupportedCommands(), [
11+
'HGETALL' => 'pavle\yii\redis\Command\HashGetAll',
12+
'CLIENT LIST' => 'pavle\yii\redis\Command\ClientList',
13+
'CLIENT SETNAME' => 'pavle\yii\redis\Command\ClientSetName',
14+
]);
15+
}
16+
}

0 commit comments

Comments
 (0)