From b562c85b9a06d26bbe5521d3bbdeb83786aaf976 Mon Sep 17 00:00:00 2001 From: sgiehl Date: Mon, 1 Dec 2014 23:14:29 +0100 Subject: [PATCH] refs #5290 - use doctrine-cache for caching --- Cache/CacheFile.php | 125 ------------------------------ Cache/CacheInterface.php | 38 --------- Cache/CacheMemcache.php | 50 ------------ Cache/CacheStatic.php | 47 ----------- Cache/StaticCache.php | 78 +++++++++++++++++++ DeviceDetector.php | 14 ++-- Parser/ParserAbstract.php | 20 ++--- README.md | 4 +- Tests/Cache/CacheFileTest.php | 61 --------------- Tests/Cache/CacheMemcacheTest.php | 35 --------- Tests/Cache/CacheStaticTest.php | 38 --------- Tests/Cache/StaticCacheTest.php | 39 ++++++++++ Tests/DeviceDetectorTest.php | 8 +- composer.json | 7 +- 14 files changed, 144 insertions(+), 420 deletions(-) delete mode 100644 Cache/CacheFile.php delete mode 100644 Cache/CacheInterface.php delete mode 100644 Cache/CacheMemcache.php delete mode 100644 Cache/CacheStatic.php create mode 100644 Cache/StaticCache.php delete mode 100644 Tests/Cache/CacheFileTest.php delete mode 100644 Tests/Cache/CacheMemcacheTest.php delete mode 100644 Tests/Cache/CacheStaticTest.php create mode 100644 Tests/Cache/StaticCacheTest.php diff --git a/Cache/CacheFile.php b/Cache/CacheFile.php deleted file mode 100644 index 5d259d2921..0000000000 --- a/Cache/CacheFile.php +++ /dev/null @@ -1,125 +0,0 @@ -cachePath = realpath($directory); - } - - public function set($key, $value) - { - parent::set($key, $value); - - if (empty($key)) { - return false; - } - if (!is_writable($this->cachePath)) { - return false; - } - - $id = $this->getCacheFileName($key); - - if (is_object($value)) { - throw new \Exception('You cannot use the CacheFile to cache an object, only arrays, strings and numbers.'); - } - - $cache_literal = "<" . "?php\n"; - $cache_literal .= "$" . "content = " . var_export($value, true) . ";\n"; - $cache_literal .= "$" . "cache_complete = true;\n"; - $cache_literal .= "?" . ">"; - - // Write cache to a temp file, then rename it, overwriting the old cache - // On *nix systems this should guarantee atomicity - $tmp_filename = tempnam($this->cachePath, 'tmp_'); - @chmod($tmp_filename, 0640); - if ($fp = @fopen($tmp_filename, 'wb')) { - @fwrite($fp, $cache_literal, strlen($cache_literal)); - @fclose($fp); - - if (!@rename($tmp_filename, $id)) { - // On some systems rename() doesn't overwrite destination - // @codeCoverageIgnoreStart - @unlink($id); - if (!@rename($tmp_filename, $id)) { - // Make sure that no temporary file is left over - // if the destination is not writable - @unlink($tmp_filename); - return false; - } - } - // @codeCoverageIgnoreEnd - - // invalidate opcache for file if opcache is active - $this->opCacheInvalidate($id); - - return true; - } - return false; - - } - - public function get($key) - { - $value = parent::get($key); - - if (is_null($value)) { - $cache_complete = false; - $content = ''; - - // We are assuming that most of the time cache will exists - $cacheFilePath = $this->getCacheFileName($key); - - $ok = @include($cacheFilePath); - - if ($ok && $cache_complete == true) { - - // as key was missing in "parent" cache, set it again - parent::set($key, $content); - return $content; - } - } - - return $value; - } - - protected function getCacheFileName($id) - { - return sprintf('%s/%s.php', $this->cachePath, $id); - } - - /** - * @codeCoverageIgnore - */ - protected function opCacheInvalidate($filepath) - { - if (function_exists('opcache_invalidate') - && is_file($filepath) - ) { - @opcache_invalidate($filepath, $force = true); - } - } -} diff --git a/Cache/CacheInterface.php b/Cache/CacheInterface.php deleted file mode 100644 index 1db787f63b..0000000000 --- a/Cache/CacheInterface.php +++ /dev/null @@ -1,38 +0,0 @@ -connect($server, $port) or die ("Could not connect"); - } - } - - public function set($key, $value) - { - self::$memcache->set($key, $value); - } - - public function get($key) - { - return self::$memcache->get($key); - } - - public static function reset() - { - self::$memcache->flush(); - } -} \ No newline at end of file diff --git a/Cache/CacheStatic.php b/Cache/CacheStatic.php deleted file mode 100644 index 6927bcadb6..0000000000 --- a/Cache/CacheStatic.php +++ /dev/null @@ -1,47 +0,0 @@ -doContains($id) ? self::$staticCache[$id] : false; + } + + /** + * {@inheritdoc} + */ + protected function doContains($id) + { + return isset(self::$staticCache[$id]) || array_key_exists($id, self::$staticCache); + } + + /** + * {@inheritdoc} + */ + protected function doSave($id, $data, $lifeTime = 0) + { + self::$staticCache[$id] = $data; + return true; + } + + /** + * {@inheritdoc} + */ + protected function doDelete($id) + { + unset(self::$staticCache[$id]); + return true; + } + + /** + * {@inheritdoc} + */ + protected function doGetStats() + { + return null; + } + + /** + * {@inheritdoc} + */ + protected function doFlush() + { + self::$staticCache = array(); + return true; + } +} \ No newline at end of file diff --git a/DeviceDetector.php b/DeviceDetector.php index ddf840f654..9f8323149b 100644 --- a/DeviceDetector.php +++ b/DeviceDetector.php @@ -8,12 +8,12 @@ namespace DeviceDetector; -use DeviceDetector\Cache\CacheInterface; -use DeviceDetector\Cache\CacheStatic; +use DeviceDetector\Cache\StaticCache; use DeviceDetector\Parser\Bot; use DeviceDetector\Parser\OperatingSystem; use DeviceDetector\Parser\Client\ClientParserAbstract; use DeviceDetector\Parser\Device\DeviceParserAbstract; +use \Doctrine\Common\Cache\Cache; use \Spyc; class DeviceDetector @@ -92,7 +92,7 @@ class DeviceDetector /** * Holds the cache class used for caching the parsed yml-Files - * @var CacheInterface + * @var \Doctrine\Common\Cache\CacheProvider */ protected $cache = null; @@ -567,11 +567,9 @@ static public function getInfoFromUserAgent($ua) /** * Sets the Cache class * - * Note: The given class needs to have a 'get' and 'set' method to be used - * * @param $cache */ - public function setCache(CacheInterface $cache) + public function setCache(Cache $cache) { $this->cache = $cache; } @@ -579,7 +577,7 @@ public function setCache(CacheInterface $cache) /** * Returns Cache object * - * @return CacheInterface + * @return \Doctrine\Common\Cache\CacheProvider */ public function getCache() { @@ -587,6 +585,6 @@ public function getCache() return $this->cache; } - return new CacheStatic(); + return new StaticCache(); } } diff --git a/Parser/ParserAbstract.php b/Parser/ParserAbstract.php index 2f7bc58de6..54b6373cd4 100644 --- a/Parser/ParserAbstract.php +++ b/Parser/ParserAbstract.php @@ -7,9 +7,9 @@ */ namespace DeviceDetector\Parser; -use DeviceDetector\Cache\CacheInterface; -use DeviceDetector\Cache\CacheStatic; +use DeviceDetector\Cache\StaticCache; use DeviceDetector\DeviceDetector; +use Doctrine\Common\Cache\Cache; use \Spyc; /** @@ -87,7 +87,7 @@ abstract class ParserAbstract const VERSION_TRUNCATION_NONE = null; /** - * @var CacheInterface + * @var \Doctrine\Common\Cache\Cache */ protected $cache; @@ -143,10 +143,10 @@ protected function getRegexes() if (empty($this->regexList)) { $cacheKey = 'DeviceDetector-'.DeviceDetector::VERSION.'regexes-'.$this->getName(); $cacheKey = preg_replace('/([^a-z0-9_-]+)/i', '', $cacheKey); - $this->regexList = $this->getCache()->get($cacheKey); + $this->regexList = $this->getCache()->fetch($cacheKey); if (empty($this->regexList)) { $this->regexList = Spyc::YAMLLoad(dirname(__DIR__).DIRECTORY_SEPARATOR.$this->fixtureFile); - $this->getCache()->set($cacheKey, $this->regexList); + $this->getCache()->save($cacheKey, $this->regexList); } } return $this->regexList; @@ -232,7 +232,7 @@ protected function preMatchOverall() $cacheKey = preg_replace('/([^a-z0-9_-]+)/i', '', $cacheKey); if (empty($overAllMatch)) { - $overAllMatch = $this->getCache()->get($cacheKey); + $overAllMatch = $this->getCache()->fetch($cacheKey); } if (empty($overAllMatch)) { @@ -244,7 +244,7 @@ protected function preMatchOverall() return $val2['regex']; } }); - $this->getCache()->set($cacheKey, $overAllMatch); + $this->getCache()->save($cacheKey, $overAllMatch); } return $this->matchUserAgent($overAllMatch); @@ -257,7 +257,7 @@ protected function preMatchOverall() * * @param $cache */ - public function setCache(CacheInterface $cache) + public function setCache(Cache $cache) { $this->cache = $cache; } @@ -265,7 +265,7 @@ public function setCache(CacheInterface $cache) /** * Returns Cache object * - * @return CacheInterface + * @return \Doctrine\Common\Cache\CacheProvider */ public function getCache() { @@ -273,6 +273,6 @@ public function getCache() return $this->cache; } - return new CacheStatic(); + return new StaticCache(); } } \ No newline at end of file diff --git a/README.md b/README.md index d5b6eac47f..55509653df 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ require_once 'vendor/autoload.php'; use DeviceDetector\DeviceDetector; use DeviceDetector\Parser\Device\DeviceParserAbstract; -use DeviceDetector\Cache\CacheFile; +use Doctrine\Common\Cache\PhpFileCache; // OPTIONAL: Set version truncation to none, so full versions will be returned // By default only minor versions will be returned (e.g. X.Y) @@ -25,7 +25,7 @@ $dd = new DeviceDetector($userAgent); // OPTIONAL: Set caching method // By default static cache is used, which works best within one php process // To cache across requests use caching in files or memcache -$dd->setCache(new CacheFile('./tmp/')); +$dd->setCache(new PhpFileCache('./tmp/')); // OPTIONAL: If called, getBot() will only return true if a bot was detected (speeds up detection a bit) $dd->discardBotInformation(); diff --git a/Tests/Cache/CacheFileTest.php b/Tests/Cache/CacheFileTest.php deleted file mode 100644 index 2cf46e5adc..0000000000 --- a/Tests/Cache/CacheFileTest.php +++ /dev/null @@ -1,61 +0,0 @@ -markTestSkipped(); - } - CacheStatic::reset(); - } - - public function testSetNotPresent() - { - $cache = new CacheFile(sys_get_temp_dir()); - $this->assertNull($cache->get('NotExistingKey')); - } - - public function testSetAndGet() - { - $cache = new CacheFile(sys_get_temp_dir()); - $this->assertTrue($cache->set('key', 'value')); - CacheStatic::reset(); - $this->assertEquals('value', $cache->get('key')); - $this->assertTrue($cache->set('key', 'value2')); - CacheStatic::reset(); - $this->assertEquals('value2', $cache->get('key')); - } - - public function testSetInvalidPath() - { - $cache = new CacheFile('invalidDir'); - $this->assertFalse($cache->set('key', 'value')); - } - - public function testSetInvalidKey() - { - $cache = new CacheFile(sys_get_temp_dir()); - $this->assertFalse($cache->set('', 'value')); - } - - /** - * @expectedException \Exception - */ - public function testSetInvalidObject() - { - $cache = new CacheFile(sys_get_temp_dir()); - $this->assertFalse($cache->set('key', new \stdClass())); - } - -} diff --git a/Tests/Cache/CacheMemcacheTest.php b/Tests/Cache/CacheMemcacheTest.php deleted file mode 100644 index e0b6e2377a..0000000000 --- a/Tests/Cache/CacheMemcacheTest.php +++ /dev/null @@ -1,35 +0,0 @@ -assertFalse($cache->get('NotExistingKey')); - } - - public function testSetAndGet() - { - $cache = new CacheMemcache(); - $cache->set('key', 'value'); - $this->assertEquals('value', $cache->get('key')); - $cache->set('key', 'value2'); - $this->assertEquals('value2', $cache->get('key')); - } - -} diff --git a/Tests/Cache/CacheStaticTest.php b/Tests/Cache/CacheStaticTest.php deleted file mode 100644 index 083cb24e41..0000000000 --- a/Tests/Cache/CacheStaticTest.php +++ /dev/null @@ -1,38 +0,0 @@ -assertNull($cache->get('NotExistingKey')); - } - - public function testSetAndGet() - { - $cache = new CacheStatic(); - $cache->set('key', 'value'); - $this->assertEquals('value', $cache->get('key')); - $cache->set('key', 'value2'); - $this->assertEquals('value2', $cache->get('key')); - - CacheStatic::reset(); - - $this->assertNull($cache->get('key')); - } - -} diff --git a/Tests/Cache/StaticCacheTest.php b/Tests/Cache/StaticCacheTest.php new file mode 100644 index 0000000000..535b7454d4 --- /dev/null +++ b/Tests/Cache/StaticCacheTest.php @@ -0,0 +1,39 @@ +flushAll(); + } + + public function testSetNotPresent() + { + $cache = new StaticCache(); + $this->assertFalse($cache->fetch('NotExistingKey')); + } + + public function testSetAndGet() + { + $cache = new StaticCache(); + $cache->save('key', 'value'); + $this->assertEquals('value', $cache->fetch('key')); + $cache->save('key', 'value2'); + $this->assertEquals('value2', $cache->fetch('key')); + + $cache->flushAll(); + + $this->assertFalse($cache->fetch('key')); + } + +} diff --git a/Tests/DeviceDetectorTest.php b/Tests/DeviceDetectorTest.php index 6f298a62da..b0a360b47a 100644 --- a/Tests/DeviceDetectorTest.php +++ b/Tests/DeviceDetectorTest.php @@ -7,10 +7,10 @@ */ namespace DeviceDetector\Tests; -use DeviceDetector\Cache\CacheMemcache; use DeviceDetector\DeviceDetector; use DeviceDetector\Parser\Device\DeviceParserAbstract; use DeviceDetector\Parser\ParserAbstract; +use Doctrine\Common\Cache\MemcacheCache; use \Spyc; class DeviceDetectorTest extends \PHPUnit_Framework_TestCase @@ -36,8 +36,10 @@ public function testAddDeviceParserInvalid() public function testCacheSetAndGet() { $dd = new DeviceDetector(); - $dd->setCache(new CacheMemcache()); - $this->assertInstanceOf('DeviceDetector\\Cache\\CacheMemcache', $dd->getCache()); + $memcacheServer = new \Memcache(); + $memcacheServer->connect('localhost', 11211); + $dd->setCache(new MemcacheCache($memcacheServer)); + $this->assertInstanceOf('Doctrine\\Common\\Cache\\MemcacheCache', $dd->getCache()); } public function testParseEmptyUA() diff --git a/composer.json b/composer.json index f519f33126..75ad248a9f 100644 --- a/composer.json +++ b/composer.json @@ -22,10 +22,11 @@ "psr-4": { "DeviceDetector\\": "" } }, "require": { - "php": ">=5.3.1", - "mustangostang/spyc": "*" + "php": ">=5.3.2", + "mustangostang/spyc": "*", + "doctrine/cache": "~1.2" }, "require-dev": { - "phpunit/phpunit": "4.0.*" + "phpunit/phpunit": "4.1.*" } }