Skip to content

Commit 488182a

Browse files
committed
merging
2 parents 7e334b2 + 755de6b commit 488182a

4 files changed

Lines changed: 165 additions & 12 deletions

File tree

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ services:
2020
- memcached
2121
- mysql
2222
- postgresql
23+
- redis-server
2324

2425
script:
2526
- php vendor/bin/phpunit -v
@@ -31,6 +32,7 @@ before_install:
3132

3233
before_script:
3334
- echo 'extension = memcached.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
35+
- echo 'extension = redis.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
3436
- composer install --prefer-source
3537

3638
after_success:

system/Cache/Handlers/RedisHandler.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ class RedisHandler implements CacheInterface
7070

7171
//--------------------------------------------------------------------
7272

73-
public function __construct($config)
73+
public function __construct(array $config)
7474
{
75-
$this->prefix = $config->prefix ?: '';
75+
$this->prefix = $config['prefix'] ?? '';
7676

77-
if (isset($config->redis))
77+
if ( ! empty($config))
7878
{
79-
$this->config = array_merge($this->config, $config->redis);
79+
$this->config = array_merge($this->config, $config);
8080
}
8181
}
8282

@@ -297,10 +297,12 @@ public function getMetaData(string $key)
297297

298298
if ($value !== FALSE)
299299
{
300-
return array(
301-
'expire' => time() + $this->redis->ttl($key),
302-
'data' => $value
303-
);
300+
$time = time();
301+
return [
302+
'expire' => $time + $this->redis->ttl($key),
303+
'mtime' => $time,
304+
'data' => $value
305+
];
304306
}
305307

306308
return FALSE;

system/Router/RouteCollection.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,10 +1117,7 @@ protected function create(string $from, $to, array $options = null)
11171117
$from = trim($from, '/');
11181118
}
11191119

1120-
if (is_null($options))
1121-
{
1122-
$options = $this->currentOptions;
1123-
}
1120+
$options = array_merge((array)$this->currentOptions, (array)$options);
11241121

11251122
// Hostname limiting?
11261123
if (isset($options['hostname']) && ! empty($options['hostname']))
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php namespace CodeIgniter\Cache\Handlers;
2+
3+
/**
4+
* CodeIgniter
5+
*
6+
* An open source application development framework for PHP
7+
*
8+
* This content is released under the MIT License (MIT)
9+
*
10+
* Copyright (c) 2014-2017 British Columbia Institute of Technology
11+
*
12+
* Permission is hereby granted, free of charge, to any person obtaining a copy
13+
* of this software and associated documentation files (the "Software"), to deal
14+
* in the Software without restriction, including without limitation the rights
15+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
* copies of the Software, and to permit persons to whom the Software is
17+
* furnished to do so, subject to the following conditions:
18+
*
19+
* The above copyright notice and this permission notice shall be included in
20+
* all copies or substantial portions of the Software.
21+
*
22+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28+
* THE SOFTWARE.
29+
*
30+
* @package CodeIgniter
31+
* @author CodeIgniter Dev Team
32+
* @copyright 2014-2017 British Columbia Institute of Technology (https://bcit.ca/)
33+
* @license https://opensource.org/licenses/MIT MIT License
34+
* @link https://codeigniter.com
35+
* @since Version 3.0.0
36+
* @filesource
37+
*/
38+
class RedisHandlerTest extends \CIUnitTestCase
39+
{
40+
private $redisHandler;
41+
private static $key1 = 'key1';
42+
private static $key2 = 'key2';
43+
private static $key3 = 'key3';
44+
private static function getKeyArray()
45+
{
46+
return [
47+
self::$key1, self::$key2, self::$key3
48+
];
49+
}
50+
51+
private static $dummy = 'dymmy';
52+
private $config;
53+
54+
public function setUp()
55+
{
56+
$this->config = new \Config\Cache();
57+
58+
$this->redisHandler = new RedisHandler($this->config->redis);
59+
if (!$this->redisHandler->isSupported()) {
60+
$this->markTestSkipped('Not support redis');
61+
}
62+
63+
$this->redisHandler->initialize();
64+
}
65+
66+
public function tearDown()
67+
{
68+
foreach (self::getKeyArray() as $key) {
69+
$this->redisHandler->delete($key);
70+
}
71+
}
72+
73+
public function testNew()
74+
{
75+
$this->assertInstanceOf(RedisHandler::class, $this->redisHandler);
76+
}
77+
78+
public function testDestruct()
79+
{
80+
$this->redisHandler = new RedisHandler($this->config->redis);
81+
$this->redisHandler->initialize();
82+
83+
$this->assertInstanceOf(RedisHandler::class, $this->redisHandler);
84+
}
85+
86+
87+
public function testGet()
88+
{
89+
$this->redisHandler->save(self::$key1, 'value', 1);
90+
91+
$this->assertSame('value', $this->redisHandler->get(self::$key1));
92+
$this->assertFalse($this->redisHandler->get(self::$dummy));
93+
94+
\CodeIgniter\CLI\CLI::wait(2);
95+
$this->assertFalse($this->redisHandler->get(self::$key1));
96+
}
97+
98+
public function testSave()
99+
{
100+
$this->assertTrue($this->redisHandler->save(self::$key1, 'value'));
101+
}
102+
103+
public function testDelete()
104+
{
105+
$this->redisHandler->save(self::$key1, 'value');
106+
107+
$this->assertTrue($this->redisHandler->delete(self::$key1));
108+
$this->assertFalse($this->redisHandler->delete(self::$dummy));
109+
}
110+
111+
//FIXME: I don't like all Hash logic very much. It's wasting memory.
112+
//public function testIncrement()
113+
//{
114+
//}
115+
116+
//public function testDecrement()
117+
//{
118+
//}
119+
120+
public function testClean()
121+
{
122+
$this->redisHandler->save(self::$key1, 1);
123+
$this->redisHandler->save(self::$key2, 'value');
124+
125+
$this->assertTrue($this->redisHandler->clean());
126+
}
127+
128+
public function testGetCacheInfo()
129+
{
130+
$this->redisHandler->save(self::$key1, 'value');
131+
132+
$this->assertInternalType('array', $this->redisHandler->getCacheInfo());
133+
}
134+
135+
public function testGetMetaData()
136+
{
137+
$time = time();
138+
$this->redisHandler->save(self::$key1, 'value');
139+
140+
$this->assertFalse($this->redisHandler->getMetaData(self::$dummy));
141+
142+
$actual = $this->redisHandler->getMetaData(self::$key1);
143+
$this->assertLessThanOrEqual(60, $actual['expire'] - $time);
144+
$this->assertLessThanOrEqual(0, $actual['mtime'] - $time);
145+
$this->assertSame('value', $actual['data']);
146+
}
147+
148+
public function testIsSupported()
149+
{
150+
$this->assertTrue($this->redisHandler->isSupported());
151+
}
152+
}

0 commit comments

Comments
 (0)