This repository has been archived by the owner on Nov 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from elricho/master
Add Redis as caching option.
- Loading branch information
Showing
5 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
/* | ||
** Copyright (c) Richard Uren 2017 - 2017 <[email protected]> | ||
** All Rights Reserved | ||
** | ||
** -- | ||
** | ||
** LICENSE: Redistribution and use in source and binary forms, with or | ||
** without modification, are permitted provided that the following | ||
** conditions are met: Redistributions of source code must retain the | ||
** above copyright notice, this list of conditions and the following | ||
** disclaimer. Redistributions in binary form must reproduce the above | ||
** copyright notice, this list of conditions and the following disclaimer | ||
** in the documentation and/or other materials provided with the | ||
** distribution. | ||
** | ||
** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | ||
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN | ||
** NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
** BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | ||
** OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | ||
** TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
namespace HandsetDetection\Cache; | ||
|
||
use HandsetDetection\Cache\CacheInterface; | ||
|
||
class Redis implements CacheInterface { | ||
|
||
protected $redis; | ||
private $name = 'redis'; | ||
|
||
public function __construct($config=array()) { | ||
if (! class_exists("\Predis\Client")) { | ||
throw new \RuntimeException('Redis functions not available. Is the Predis module installed ?'); | ||
} | ||
|
||
$options = @$config['cache']['redis']; | ||
|
||
// Create handle | ||
$this->redis = empty($options) ? new \Predis\Client() : new \Predis\Client($options); | ||
} | ||
|
||
/** Get key */ | ||
public function get($key) { | ||
$data = $this->redis->get($key); | ||
if ($data === false) | ||
return null; | ||
if (empty($data)) | ||
return null; | ||
return unserialize($data); | ||
} | ||
|
||
/** Set key */ | ||
public function set($key, $data, $ttl) { | ||
$code = $this->redis->set($key, serialize($data)); | ||
if (! $code) | ||
return null; | ||
if ($ttl) { | ||
return ($this->redis->expire($key, $ttl) == 1) ? true : null; | ||
} | ||
return true; | ||
} | ||
|
||
/** Delete key */ | ||
public function del($key) { | ||
return ($this->redis->del($key) == 1) ? true : null; | ||
} | ||
|
||
/** Flush Cache */ | ||
public function flush() { | ||
if (! $this->redis->flushdb()) | ||
return null; | ||
return true; | ||
} | ||
|
||
/** Return cache name **/ | ||
public function getName() { | ||
return $this->name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
class RedisTest extends PHPUnit_Framework_TestCase { | ||
var $volumeTest = 10000; | ||
var $testData = array( | ||
'roses' => 'red', | ||
'fish' => 'blue', | ||
'sugar' => 'sweet', | ||
'number' => 4 | ||
); | ||
|
||
function setup() { | ||
if (! class_exists('\Predis\Client')) { | ||
$this->markTestSkipped('Predis class not available. Please install via composer.'); | ||
} | ||
} | ||
|
||
function testBasic() { | ||
$config = array( | ||
'cache' => array( | ||
'redis' => array( | ||
'scheme' => 'tcp', | ||
'host' => '127.0.0.1', | ||
'port' => 6379 | ||
) | ||
) | ||
); | ||
|
||
$cache = new HandsetDetection\HDCache($config); | ||
$now = time(); | ||
|
||
// Test Write & Read | ||
$cache->write($now, $this->testData); | ||
$reply = $cache->read($now); | ||
$this->assertEquals($this->testData, $reply); | ||
|
||
// Test Flush | ||
$reply = $cache->purge(); | ||
$this->assertTrue($reply); | ||
$reply = $cache->read($now); | ||
$this->assertNull($reply); | ||
} | ||
|
||
function testVolume() { | ||
$config = array( | ||
'cache' => array( | ||
'redis' => array( | ||
'scheme' => 'tcp', | ||
'host' => '127.0.0.1', | ||
'port' => 6379 | ||
) | ||
) | ||
); | ||
|
||
$cache = new HandsetDetection\HDCache($config); | ||
$now = time(); | ||
|
||
for($i=0; $i < $this->volumeTest; $i++) { | ||
$key = 'test'.$now.$i; | ||
|
||
// Write | ||
$reply = $cache->write($key, $this->testData); | ||
$this->assertTrue($reply); | ||
|
||
// Read | ||
$reply = $cache->read($key); | ||
$this->assertEquals($this->testData, $reply); | ||
|
||
// Delete | ||
$reply = $cache->delete($key); | ||
$this->assertTrue($reply); | ||
|
||
// Read | ||
$reply = $cache->read($key); | ||
$this->assertNull($reply); | ||
} | ||
$end = time(); | ||
$cache->purge(); | ||
} | ||
|
||
function testGetName() { | ||
$config = array( | ||
'cache' => array( | ||
'redis' => array( | ||
'scheme' => 'tcp', | ||
'host' => '127.0.0.1', | ||
'port' => 6379 | ||
) | ||
) | ||
); | ||
|
||
$cache = new HandsetDetection\HDCache($config); | ||
$this->assertEquals('redis', $cache->getName()); | ||
} | ||
} |