Skip to content

Commit 33c9c43

Browse files
authored
Merge pull request #629 from Geolim4/master
Implemented #627
2 parents 4aafbfe + 84a2f27 commit 33c9c43

File tree

5 files changed

+137
-2
lines changed

5 files changed

+137
-2
lines changed

lib/Phpfastcache/Drivers/Predis/Config.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class Config extends ConfigurationOption
3737
*/
3838
protected $predisClient;
3939

40+
/**
41+
* @var string
42+
*/
43+
protected $optPrefix = '';
44+
4045
/**
4146
* @return string
4247
*/
@@ -139,4 +144,24 @@ public function setPredisClient(\Predis\Client $predisClient = null): Config
139144
$this->predisClient = $predisClient;
140145
return $this;
141146
}
147+
148+
/**
149+
* @return string
150+
* @since 7.0.2
151+
*/
152+
public function getOptPrefix(): string
153+
{
154+
return $this->optPrefix;
155+
}
156+
157+
/**
158+
* @param string $optPrefix
159+
* @return Config
160+
* @since 7.0.2
161+
*/
162+
public function setOptPrefix(string $optPrefix): Config
163+
{
164+
$this->optPrefix = trim($optPrefix);
165+
return $this;
166+
}
142167
}

lib/Phpfastcache/Drivers/Predis/Driver.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,19 @@ protected function driverConnect(): bool
7373
return true;
7474
}
7575

76+
$options = [];
77+
78+
if($this->getConfig()->getOptPrefix()){
79+
$options['prefix'] = $this->getConfig()->getOptPrefix();
80+
}
81+
7682
if (!empty($this->getConfig()->getPath())) {
7783
$this->instance = new PredisClient([
7884
'scheme' => 'unix',
7985
'path' => $this->getConfig()->getPath(),
80-
]);
86+
], $options);
8187
} else {
82-
$this->instance = new PredisClient($this->getConfig()->getPredisConfigArray());
88+
$this->instance = new PredisClient($this->getConfig()->getPredisConfigArray(), $options);
8389
}
8490

8591
try {

lib/Phpfastcache/Drivers/Redis/Config.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ class Config extends ConfigurationOption
4343
*/
4444
protected $redisClient;
4545

46+
/**
47+
* @var string
48+
*/
49+
protected $optPrefix = '';
50+
4651
/**
4752
* @return string
4853
*/
@@ -150,4 +155,24 @@ public function setRedisClient(RedisClient $redisClient = null): Config
150155
$this->redisClient = $redisClient;
151156
return $this;
152157
}
158+
159+
/**
160+
* @return string
161+
* @since 7.0.2
162+
*/
163+
public function getOptPrefix(): string
164+
{
165+
return $this->optPrefix;
166+
}
167+
168+
/**
169+
* @param string $optPrefix
170+
* @return Config
171+
* @since 7.0.2
172+
*/
173+
public function setOptPrefix(string $optPrefix): Config
174+
{
175+
$this->optPrefix = trim($optPrefix);
176+
return $this;
177+
}
153178
}

lib/Phpfastcache/Drivers/Redis/Driver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ protected function driverConnect(): bool
8181
return false;
8282
}
8383

84+
if($this->getConfig()->getOptPrefix()){
85+
$this->instance->setOption(RedisClient::OPT_PREFIX, $this->getConfig()->getOptPrefix());
86+
}
87+
8488
if (!$this->getConfig()->getPath()) {
8589
if ($this->getConfig()->getPassword() && !$this->instance->auth($this->getConfig()->getPassword())) {
8690
return false;

tests/issues/Github-627.test.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/**
4+
* @author Khoa Bui (khoaofgod) <[email protected]> https://www.phpfastcache.com
5+
* @author Georges.L (Geolim4) <[email protected]>
6+
*/
7+
8+
use Phpfastcache\CacheManager;
9+
use Phpfastcache\Helper\TestHelper;
10+
use Phpfastcache\Drivers\Redis\Config as RedisConfig;
11+
use Phpfastcache\Drivers\Predis\Config as PredisConfig;
12+
13+
14+
chdir(__DIR__);
15+
require_once __DIR__ . '/../../vendor/autoload.php';
16+
$testHelper = new TestHelper('Github issue #627 - Redis/Predis "prefix" option');
17+
$redisInstance = CacheManager::getInstance('Redis', new RedisConfig(['optPrefix' => uniqid('pfc', true) . '_']));
18+
$predisInstance = CacheManager::getInstance('Predis', new PredisConfig(['optPrefix' => uniqid('pfc', true) . '_']));
19+
20+
$testHelper->printInfoText('Testing Redis 1/2');
21+
22+
/**
23+
* Clear the cache to avoid
24+
* unexpected results
25+
*/
26+
$redisInstance->clear();
27+
28+
$cacheKey = uniqid('ck', true);
29+
$string = uniqid('pfc', true);
30+
$testHelper->printText('Preparing test item...');
31+
32+
/**
33+
* Setup the cache item
34+
*/
35+
$cacheItem = $redisInstance->getItem($cacheKey);
36+
$cacheItem->set($string);
37+
$redisInstance->save($cacheItem);
38+
unset($cacheItem);
39+
$redisInstance->detachAllItems();
40+
41+
if($redisInstance->getItem($cacheKey)->isHit()){
42+
$testHelper->printPassText('The cache item has been found in cache');
43+
}else{
44+
$testHelper->printFailText('The cache item was not found in cache');
45+
}
46+
47+
$testHelper->printInfoText('Testing Predis 2/2');
48+
49+
/**
50+
* Clear the cache to avoid
51+
* unexpected results
52+
*/
53+
$predisInstance->clear();
54+
55+
$cacheKey = uniqid('ck', true);
56+
$string = uniqid('pfc', true);
57+
$testHelper->printText('Preparing test item...');
58+
59+
/**
60+
* Setup the cache item
61+
*/
62+
$cacheItem = $predisInstance->getItem($cacheKey);
63+
$cacheItem->set($string);
64+
$predisInstance->save($cacheItem);
65+
unset($cacheItem);
66+
$predisInstance->detachAllItems();
67+
68+
if($predisInstance->getItem($cacheKey)->isHit()){
69+
$testHelper->printPassText('The cache item has been found in cache');
70+
}else{
71+
$testHelper->printFailText('The cache item was not found in cache');
72+
}
73+
74+
75+
$testHelper->terminateTest();

0 commit comments

Comments
 (0)