Skip to content

Commit ef30f5c

Browse files
author
Michiel Vancoillie
committed
Add unit testing + fix memcache expiry (zero = unlimited TTL)
1 parent febf143 commit ef30f5c

File tree

7 files changed

+117
-42
lines changed

7 files changed

+117
-42
lines changed

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: php
2+
services:
3+
- memcache
4+
php:
5+
- "5.4"
6+
- "5.3"
7+
before_script:
8+
- chmod 777 tests/travis/memcache-setup.sh
9+
- ./tests/travis/memcache-setup.sh
10+
- composer install
11+
12+
script: phpunit tests

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
cache
2-
=====
1+
# tdt/cache
32

4-
This repository holds a wrapper around a caching system. You can use NoCache if no caching system is installed, or MemCache. This allows the user to provide caching in his code, and switch to other
3+
[![Build Status](https://travis-ci.org/tdt/cache.png)](https://travis-ci.org/tdt/cache)
4+
5+
This repository holds a wrapper around a caching system. You can use NoCache if no caching system is installed, or MemCache. This allows the user to provide caching in his code, and switch to other
56
caching systems later on if necessary.
67

78
# Usage

src/tdt/cache/Cache.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* The caching class. Depending on the globals given in the config it will be able to set and get a variable
44
*
55
* Usage of an implementation:
6+
*
67
* $c = Cache::getInstance();
78
* $element = $c->get($id);
89
* if(is_null($element)){
@@ -13,8 +14,9 @@
1314
* @package tdt\cache
1415
* @copyright (C) 2011,2013 by iRail vzw/asbl, OKFN Belgium vzw/asbl
1516
* @license AGPLv3
16-
* @author Jan Vansteenlandt <[email protected]>
17-
* @author Pieter Colpaert <[email protected]>
17+
* @author Jan Vansteenlandt <[email protected]>
18+
* @author Michiel Vancoillie <[email protected]>
19+
* @author Pieter Colpaert <[email protected]>
1820
*/
1921

2022
namespace tdt\cache;
@@ -24,9 +26,9 @@
2426
abstract class Cache{
2527
private static $instance;
2628
protected $config = array();
27-
28-
29-
protected function __construct(array $config){
29+
30+
31+
protected function __construct(array $config){
3032
$this->config = $config;
3133
}
3234

@@ -35,27 +37,31 @@ protected function __construct(array $config){
3537
* system = name of the caching system i.e. MemCache, NoCache,...
3638
* host = the host on which the caching system runs
3739
* port = the port on which to connect to the host
38-
*
40+
*
3941
* If NoCache is used, no host or port is necessary.
4042
*/
41-
42-
public static function getInstance(array $config){
43-
if(!isset(self::$instance)){
43+
44+
public static function getInstance(array $config){
45+
if(!isset(self::$instance)){
4446
if(isset($config["system"])){
45-
$cacheclass = 'tdt\\cache\\TDT' . $config["system"];
46-
if(class_exists($cacheclass)){
47+
$cacheclass = 'tdt\\cache\\' . $config["system"];
48+
if(class_exists($cacheclass)){
4749
self::$instance = new $cacheclass($config);
4850
}else{
49-
throw new TDTException(551,array("tdt\cache\ ".$config["system"]));
51+
throw new TDTException(551, array("tdt\\cache\\".$config["system"]));
5052
}
5153
}else{
52-
throw new TDTException(500,array("The system was not set in the configuration"));
53-
}
54-
54+
throw new TDTException(500, array("The cache system was not set in the configuration"));
55+
}
56+
5557
}
5658
return self::$instance;
57-
}
58-
59+
}
60+
61+
public static function destroy(){
62+
self::$instance = null;
63+
}
64+
5965
abstract public function set($key, $value, $TTL = 60);
6066
abstract public function get($key);
6167
abstract public function delete($key);

src/tdt/cache/TDTMemCache.php renamed to src/tdt/cache/MemCache.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
/**
44
* MemCache implementation of Cache
5-
*
5+
*
66
* @package The-Datatank/aspects/caching
77
* @copyright (C) 2011,2013 by iRail vzw/asbl
88
* @license AGPLv3
9-
* @author Jan Vansteenlandt <[email protected]>
10-
* @author Pieter Colpaert <[email protected]>
9+
* @author Jan Vansteenlandt <[email protected]>
10+
* @author Michiel Vancoillie <[email protected]>
11+
* @author Pieter Colpaert <[email protected]>
1112
*/
1213

1314
namespace tdt\cache;
@@ -16,7 +17,7 @@
1617
use Monolog\Logger;
1718
use Monolog\Handler\StreamHandler;
1819

19-
class TDTMemCache extends Cache {
20+
class MemCache extends Cache {
2021

2122
private $memcache;
2223

@@ -29,21 +30,23 @@ protected function __construct($config) {
2930
* In memcache, the old but stable implementation in PHP of memcached the persistent connection works like charm
3031
* In memcached however there is a severe bug which leads to a memory leak. If you'd take over code from this class to implement memcached, DON'T use the persistent connect!
3132
*/
32-
33+
3334
if(!isset($this->config["host"])){
34-
throw new TDTException(500, array("No host has been given to cache to."));
35+
// the default host for memcached localhost
36+
$this->config["host"] = "localhost";
3537
}
36-
38+
3739
if(!isset($this->config["port"])){
38-
$this->config["port"] = 11211; // the default port for memcached is 11211
40+
// the default port for memcached is 11211
41+
$this->config["port"] = 11211;
3942
}
40-
43+
4144
if (!$this->memcache->pconnect($this->config["host"], $this->config["port"])) {
4245
if (isset($this->config["log_dir"])) {
4346
$log_dir = rtrim($this->config["log_dir"], "/");
4447
$log = new Logger('cache');
4548
$log->pushHandler(new StreamHandler($log_dir . "/log_". date('Y-m-d') . ".txt", Logger::CRITICAL));
46-
$log->addCritical("Could not connect to memcached.", $this->config);
49+
$log->addCritical("Could not connect to memcached.", $this->config);
4750
}else{
4851
/*
4952
* if we have no log directory, it's no use to throw a TDTException
@@ -54,9 +57,7 @@ protected function __construct($config) {
5457
}
5558

5659
public function set($key, $value, $timeout = 60) {
57-
if ($timeout > 0) {
58-
$this->memcache->set($key, $value, FALSE, $timeout); //the true flag will compress the value using zlib
59-
}
60+
$this->memcache->set($key, $value, FALSE, $timeout); //the true flag will compress the value using zlib
6061
}
6162

6263
public function get($key) {
@@ -67,7 +68,7 @@ public function get($key) {
6768
}
6869

6970
public function delete($key) {
70-
$this->memcache->delete($key, 0);
71+
$this->memcache->delete($key);
7172
}
7273

7374
}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
<?php
22
/**
33
* Dummy class - when no cache could be installed on the system (e.g. cheap hosts)
4-
*
4+
*
55
* @package The-Datatank/aspects/caching
66
* @copyright (C) 2011 by iRail vzw/asbl
77
* @license AGPLv3
8-
* @author Jan Vansteenlandt <[email protected]>
9-
* @author Pieter Colpaert <[email protected]>
8+
* @author Jan Vansteenlandt <[email protected]>
9+
* @author Michiel Vancoillie <[email protected]>
10+
* @author Pieter Colpaert <[email protected]>
1011
*/
12+
1113
namespace tdt\cache;
1214

13-
class TDTNoCache extends Cache{
15+
class NoCache extends Cache{
1416
protected function __construct(){
15-
17+
1618
}
1719

1820
public function set($key,$value, $timeout=60){
19-
//do nothing
21+
// do nothing
2022
}
2123

2224
public function get($key){
2325
return null;
2426
}
2527

2628
public function delete($key){
27-
//do nothing
29+
// do nothing
2830
}
29-
31+
3032
}

tests/CacheTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* Quick test of the caching systems
5+
*
6+
* @copyright (C) 2011 by iRail vzw/asbl
7+
* @license AGPLv3
8+
* @author Michiel Vancoillie
9+
*/
10+
11+
require "vendor/autoload.php";
12+
13+
use tdt\cache\MemCache;
14+
use tdt\cache\NoCache;
15+
use tdt\cache\Cache;
16+
17+
class CacheTest extends \PHPUnit_Framework_TestCase{
18+
19+
public function testCache(){
20+
// Test nocache
21+
$nocache = $this->_createCacheInstance("NoCache");
22+
$nocache->set("key","value", 0);
23+
$this->assertNull($nocache->get("key"), "NoCache shouldn't be caching!");
24+
Cache::destroy();
25+
26+
// Test memcached
27+
$memcache = $this->_createCacheInstance("MemCache");
28+
$memcache->set("key","value", 0);
29+
$this->assertEquals("value", $memcache->get("key"), "MemCache didn't cache!");
30+
$memcache->set("key2","value2", 2);
31+
$this->assertEquals("value2", $memcache->get("key2"), "MemCache didn't cache expiring value!");
32+
sleep(1);
33+
$this->assertEquals("value2", $memcache->get("key2"), "MemCache didn't cache expiring value (1s)!");
34+
sleep(1);
35+
$this->assertNull($memcache->get("key2"), "Memcache didn't expire 'key2'.");
36+
$memcache->delete("key");
37+
$this->assertNull($memcache->get("key"), "Memcache didn't delete 'key'.");
38+
Cache::destroy();
39+
}
40+
41+
private function _createCacheInstance($type){
42+
$config = array("system" => $type);
43+
$cache = Cache::getInstance($config);
44+
$this->assertInstanceOf('tdt\cache\Cache', $cache, "Could not construct $type instance");
45+
46+
return $cache;
47+
}
48+
49+
}

tests/travis/memcache-setup.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
printf "yes\n" | pecl install memcache
4+
echo "extension=memcache.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

0 commit comments

Comments
 (0)