diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..47f36f6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +vendor \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..5ddad42 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1 @@ +# Changelog \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ef28f1d --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "rngr/cache", + "type": "library", + "description": "Simple Cache library", + "keywords": ["directus", "cache", "rngr"], + "homepage": "http://getdirectus.com", + "license": "MIT", + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "~3.7.0" + }, + "autoload": { + "psr-4": { + "RNGR\\Cache\\": "src/" + } + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..6f94f22 --- /dev/null +++ b/composer.lock @@ -0,0 +1,432 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "c49e1479e6ad3e5867af9cbfa28c3d7a", + "content-hash": "93cd3f7e4f797a76b6f86064d178cb6a", + "packages": [], + "packages-dev": [ + { + "name": "phpunit/php-code-coverage", + "version": "1.2.18", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-code-coverage.git", + "reference": "fe2466802556d3fe4e4d1d58ffd3ccfd0a19be0b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/fe2466802556d3fe4e4d1d58ffd3ccfd0a19be0b", + "reference": "fe2466802556d3fe4e4d1d58ffd3ccfd0a19be0b", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "phpunit/php-file-iterator": ">=1.3.0@stable", + "phpunit/php-text-template": ">=1.2.0@stable", + "phpunit/php-token-stream": ">=1.1.3,<1.3.0" + }, + "require-dev": { + "phpunit/phpunit": "3.7.*@dev" + }, + "suggest": { + "ext-dom": "*", + "ext-xdebug": ">=2.0.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2.x-dev" + } + }, + "autoload": { + "classmap": [ + "PHP/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "include-path": [ + "" + ], + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", + "homepage": "https://github.com/sebastianbergmann/php-code-coverage", + "keywords": [ + "coverage", + "testing", + "xunit" + ], + "time": "2014-09-02 10:13:14" + }, + { + "name": "phpunit/php-file-iterator", + "version": "1.4.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-file-iterator.git", + "reference": "6150bf2c35d3fc379e50c7602b75caceaa39dbf0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/6150bf2c35d3fc379e50c7602b75caceaa39dbf0", + "reference": "6150bf2c35d3fc379e50c7602b75caceaa39dbf0", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "FilterIterator implementation that filters files based on a list of suffixes.", + "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", + "keywords": [ + "filesystem", + "iterator" + ], + "time": "2015-06-21 13:08:43" + }, + { + "name": "phpunit/php-text-template", + "version": "1.2.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-text-template.git", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Simple template engine.", + "homepage": "https://github.com/sebastianbergmann/php-text-template/", + "keywords": [ + "template" + ], + "time": "2015-06-21 13:50:34" + }, + { + "name": "phpunit/php-timer", + "version": "1.0.7", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-timer.git", + "reference": "3e82f4e9fc92665fafd9157568e4dcb01d014e5b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/3e82f4e9fc92665fafd9157568e4dcb01d014e5b", + "reference": "3e82f4e9fc92665fafd9157568e4dcb01d014e5b", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Utility class for timing", + "homepage": "https://github.com/sebastianbergmann/php-timer/", + "keywords": [ + "timer" + ], + "time": "2015-06-21 08:01:12" + }, + { + "name": "phpunit/php-token-stream", + "version": "1.2.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-token-stream.git", + "reference": "ad4e1e23ae01b483c16f600ff1bebec184588e32" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/ad4e1e23ae01b483c16f600ff1bebec184588e32", + "reference": "ad4e1e23ae01b483c16f600ff1bebec184588e32", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2-dev" + } + }, + "autoload": { + "classmap": [ + "PHP/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "include-path": [ + "" + ], + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Wrapper around PHP's tokenizer extension.", + "homepage": "https://github.com/sebastianbergmann/php-token-stream/", + "keywords": [ + "tokenizer" + ], + "time": "2014-03-03 05:10:30" + }, + { + "name": "phpunit/phpunit", + "version": "3.7.38", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit.git", + "reference": "38709dc22d519a3d1be46849868aa2ddf822bcf6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/38709dc22d519a3d1be46849868aa2ddf822bcf6", + "reference": "38709dc22d519a3d1be46849868aa2ddf822bcf6", + "shasum": "" + }, + "require": { + "ext-ctype": "*", + "ext-dom": "*", + "ext-json": "*", + "ext-pcre": "*", + "ext-reflection": "*", + "ext-spl": "*", + "php": ">=5.3.3", + "phpunit/php-code-coverage": "~1.2", + "phpunit/php-file-iterator": "~1.3", + "phpunit/php-text-template": "~1.1", + "phpunit/php-timer": "~1.0", + "phpunit/phpunit-mock-objects": "~1.2", + "symfony/yaml": "~2.0" + }, + "require-dev": { + "pear-pear.php.net/pear": "1.9.4" + }, + "suggest": { + "phpunit/php-invoker": "~1.1" + }, + "bin": [ + "composer/bin/phpunit" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.7.x-dev" + } + }, + "autoload": { + "classmap": [ + "PHPUnit/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "include-path": [ + "", + "../../symfony/yaml/" + ], + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "The PHP Unit Testing framework.", + "homepage": "http://www.phpunit.de/", + "keywords": [ + "phpunit", + "testing", + "xunit" + ], + "time": "2014-10-17 09:04:17" + }, + { + "name": "phpunit/phpunit-mock-objects", + "version": "1.2.3", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", + "reference": "5794e3c5c5ba0fb037b11d8151add2a07fa82875" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/5794e3c5c5ba0fb037b11d8151add2a07fa82875", + "reference": "5794e3c5c5ba0fb037b11d8151add2a07fa82875", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "phpunit/php-text-template": ">=1.1.1@stable" + }, + "suggest": { + "ext-soap": "*" + }, + "type": "library", + "autoload": { + "classmap": [ + "PHPUnit/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "include-path": [ + "" + ], + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Mock Object library for PHPUnit", + "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", + "keywords": [ + "mock", + "xunit" + ], + "time": "2013-01-13 10:24:48" + }, + { + "name": "symfony/yaml", + "version": "v2.8.3", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "2a4ee40acb880c56f29fb1b8886e7ffe94f3b995" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/2a4ee40acb880c56f29fb1b8886e7ffe94f3b995", + "reference": "2a4ee40acb880c56f29fb1b8886e7ffe94f3b995", + "shasum": "" + }, + "require": { + "php": ">=5.3.9" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.8-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Yaml Component", + "homepage": "https://symfony.com", + "time": "2016-02-23 07:41:20" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.5.0" + }, + "platform-dev": [] +} diff --git a/phpunit.php b/phpunit.php new file mode 100644 index 0000000..8c9d32f --- /dev/null +++ b/phpunit.php @@ -0,0 +1,2 @@ + + + + + ./tests + + + \ No newline at end of file diff --git a/src/ArrayStorage.php b/src/ArrayStorage.php new file mode 100644 index 0000000..512d3f6 --- /dev/null +++ b/src/ArrayStorage.php @@ -0,0 +1,57 @@ +storage)) { + return $this->storage[$key]; + } + } + + /** + * @inheritDoc + */ + public function put($key, $value, $seconds) + { + $this->storage[$key] = $value; + } + + /** + * @inheritDoc + */ + public function delete($key) + { + unset($this->storage[$key]); + + return true; + } + + /** + * @inheritDoc + */ + public function flush() + { + $this->storage = []; + } + + /** + * @inheritDoc + */ + public function getPrefix() + { + return ''; + } +} diff --git a/src/RedisExtensionStorage.php b/src/RedisExtensionStorage.php new file mode 100644 index 0000000..12800ef --- /dev/null +++ b/src/RedisExtensionStorage.php @@ -0,0 +1,77 @@ +redis, func_get_args()); + } + + /** + * @inheritDoc + */ + public function read($key) + { + $value = $this->redis->get($key); + if ($value) { + return is_numeric($value) ? $value : unserialize($value); + } + } + + /** + * @inheritDoc + */ + public function put($key, $value, $seconds) + { + $this->redis->set($key, is_numeric($value) ? $value : serialize($value), $seconds); + } + + /** + * @inheritDoc + */ + public function delete($key) + { + $this->redis->delete($key); + + return true; + } + + /** + * @inheritDoc + */ + public function flush() + { + $this->redis->flushDB(); + } + + /** + * @inheritDoc + */ + public function getPrefix() + { + return ''; + } + + public function setPrefix($prefix) + { + $this->prefix = !empty($prefix) ? $prefix.':' : ''; + } +} diff --git a/src/Repository.php b/src/Repository.php new file mode 100644 index 0000000..5bb338c --- /dev/null +++ b/src/Repository.php @@ -0,0 +1,97 @@ +storage = $storage; + } + + /** + * @inheritDoc + */ + public function has($key) + { + return !is_null($this->storage->read($key)); + } + + /** + * @inheritDoc + */ + public function read($key, $default = null) + { + $value = $this->storage->read($key); + + if (is_null($value)) { + $value = $default; + } + + return $value; + } + + /** + * @inheritDoc + */ + public function readAndDelete($key, $default = null) + { + $value = $this->read($key, $default); + + $this->delete($key); + + return $value; + } + + /** + * @inheritDoc + */ + public function put($key, $value, $seconds) + { + $this->storage->put($key, $value, $seconds); + } + + /** + * @inheritDoc + */ + public function readOrPut($key, $seconds, Closure $callback) + { + $value = $this->read($key); + if (!is_null($value)) { + return $value; + } + + $this->put($key, $value = $callback(), $seconds); + + return $value; + } + + /** + * @inheritDoc + */ + public function delete($key) + { + return $this->storage->delete($key); + } + + /** + * Pass missing methods to the store. + * + * @param string $method + * @param array $parameters + * @return mixed + */ + public function __call($method, $parameters) + { + return call_user_func_array([$this->storage, $method], $parameters); + } +} diff --git a/src/RepositoryInterface.php b/src/RepositoryInterface.php new file mode 100644 index 0000000..fe84984 --- /dev/null +++ b/src/RepositoryInterface.php @@ -0,0 +1,62 @@ +put('name', 'John', 0); + $this->assertSame('John', $storage->read('name')); + + $storage->delete('name'); + $this->assertNull($storage->read('name')); + + $storage->put('name', 'Samuel', 0); + $this->assertSame('Samuel', $storage->read('name')); + + $storage->flush(); + $this->assertNull($storage->read('name')); + } +} diff --git a/tests/RepositoryTest.php b/tests/RepositoryTest.php new file mode 100644 index 0000000..1ecef01 --- /dev/null +++ b/tests/RepositoryTest.php @@ -0,0 +1,38 @@ +put('name', 'Bob', 0); + $this->assertSame('Bob', $repository->read('name')); + + $repository->delete('name'); + $this->assertNull($repository->read('name')); + $this->assertSame('Joe', $repository->read('name', 'Joe')); + + $repository->put('name', 'Daisy', 0); + $this->assertSame('Daisy', $repository->read('name')); + + $repository->flush(); + $this->assertNull($repository->read('name')); + + $repository->put('name', 'O.B', 0); + $this->assertSame('O.B', $repository->read('name')); + + $this->assertSame('O.B', $repository->readAndDelete('name')); + $this->assertNull($repository->read('name')); + + $repository->readOrPut('name', 0, function() { + return 'Oswaldo'; + }); + + $this->assertSame('Oswaldo', $repository->read('name')); + } +}