Skip to content

Commit

Permalink
Merge pull request #2 from kodus/1.0.0
Browse files Browse the repository at this point in the history
Updated implementation to use simple-cache 1.0
  • Loading branch information
boan-jfm committed Feb 7, 2017
2 parents a47f559 + 1750d2f commit 644d7f6
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 19 deletions.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
"license": "MIT",
"minimum-stability": "dev",
"prefer-stable": true,
"provide": {
"psr/simple-cache-implementation": "1.0"
},
"require": {
"php": ">= 5.6",
"psr/simple-cache": "^0.2"
"psr/simple-cache": "^1"
},
"require-dev": {
"codeception/codeception": "^2"
"codeception/codeception": "^2",
"mindplay/simple-cache": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
53 changes: 41 additions & 12 deletions src/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
use DateInterval;
use FilesystemIterator;
use Generator;
use InvalidArgumentException;
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\CounterInterface;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Traversable;

/**
* This is a simple, file-based cache implementation, which is bootstrapped by
Expand All @@ -19,7 +18,7 @@
*
* @link https://github.com/matthiasmullie/scrapbook/
*/
class FileCache implements CacheInterface, CounterInterface
class FileCache implements CacheInterface
{
// TODO garbage collection

Expand Down Expand Up @@ -136,34 +135,49 @@ public function set($key, $value, $ttl = null)

public function delete($key)
{
@unlink($this->getPath($key));
return @unlink($this->getPath($key));
}

public function clear()
{
$success = true;

$paths = $this->listPaths();

foreach ($paths as $path) {
@unlink($path);
if (! unlink($path)) {
$success = false;
}
}

return $success;
}

public function getMultiple($keys)
public function getMultiple($keys, $default = null)
{
if (! is_array($keys) && ! $keys instanceof Traversable) {
throw new InvalidArgumentException("keys must be either of type array or Traversable");
}

$values = [];

foreach ($keys as $key) {
$values[$key] = $this->get($key);
$values[$key] = $this->get($key) ?: $default;
}

return $values;
}

public function setMultiple($items, $ttl = null)
public function setMultiple($values, $ttl = null)
{
if (! is_array($values) && ! $values instanceof Traversable) {
throw new InvalidArgumentException("keys must be either of type array or Traversable");
}

$ok = true;

foreach ($items as $key => $value) {
foreach ($values as $key => $value) {
$this->validateKey($key);
$ok = $this->set($key, $value, $ttl) && $ok;
}

Expand All @@ -172,7 +186,12 @@ public function setMultiple($items, $ttl = null)

public function deleteMultiple($keys)
{
if (! is_array($keys) && ! $keys instanceof Traversable) {
throw new InvalidArgumentException("keys must be either of type array or Traversable");
}

foreach ($keys as $key) {
$this->validateKey($key);
$this->delete($key);
}
}
Expand Down Expand Up @@ -247,9 +266,7 @@ public function cleanExpired()
*/
protected function getPath($key)
{
if (preg_match(self::PSR16_RESERVED, $key, $match) === 1) {
throw new InvalidArgumentException("invalid character in key: {$match[0]}");
}
$this->validateKey($key);

$hash = hash("sha256", $key);

Expand Down Expand Up @@ -290,4 +307,16 @@ protected function listPaths()
yield $path;
}
}

/**
* @param string $key
*
* @throws InvalidArgumentException
*/
protected function validateKey($key)
{
if (preg_match(self::PSR16_RESERVED, $key, $match) === 1) {
throw new InvalidArgumentException("invalid character in key: {$match[0]}");
}
}
}
8 changes: 8 additions & 0 deletions src/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Kodus\Cache;

class InvalidArgumentException extends \InvalidArgumentException implements \Psr\SimpleCache\InvalidArgumentException
{

}
48 changes: 43 additions & 5 deletions tests/integration/FileCacheCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Codeception\Util\FileSystem;
use DateInterval;
use IntegrationTester;
use Kodus\Cache\InvalidArgumentException;
use Kodus\Cache\Test\TestableFileCache;

class FileCacheCest
Expand All @@ -28,7 +29,7 @@ public function _before()
$this->cache = new TestableFileCache($path, self::DEFAULT_EXPIRATION);

assert(file_exists($path));

assert(is_writable($path));
}

Expand All @@ -45,10 +46,23 @@ public function setGetAndDelete(IntegrationTester $I)
$I->assertSame("value1", $this->cache->get("key1"));
$I->assertSame("value2", $this->cache->get("key2"));

$this->cache->delete("key1");
$I->assertTrue($this->cache->delete("key1"));
$I->assertFalse($this->cache->delete("key1"));

$I->assertSame(null, $this->cache->get("key1"));
$I->assertSame("value2", $this->cache->get("key2"));

$I->expectException(InvalidArgumentException::class, function () {
$this->cache->set("key@", "value1");
});

$I->expectException(InvalidArgumentException::class, function () {
$this->cache->get("key@");
});

$I->expectException(InvalidArgumentException::class, function () {
$this->cache->delete("key@");
});
}

public function getNonExisting(IntegrationTester $I)
Expand Down Expand Up @@ -117,7 +131,7 @@ public function clear(IntegrationTester $I)
$this->cache->set("key1", "value1");
$this->cache->set("key2", "value2");

$this->cache->clear();
$I->assertTrue($this->cache->clear());

// check to confirm everything"s been wiped out:

Expand Down Expand Up @@ -145,9 +159,25 @@ public function testGetAndSetMultiple(IntegrationTester $I)
{
$this->cache->setMultiple(["key1" => "value1", "key2" => "value2"]);

$results = $this->cache->getMultiple(["key1", "key2", "key3"]);
$results = $this->cache->getMultiple(["key1", "key2", "key3"], false);

$I->assertSame(["key1" => "value1", "key2" => "value2", "key3" => false], $results);

$I->assertSame(["key1" => "value1", "key2" => "value2", "key3" => null], $results);
$I->expectException(InvalidArgumentException::class, function () {
$this->cache->getMultiple("Invalid type");
});

$I->expectException(InvalidArgumentException::class, function () {
$this->cache->setMultiple("Invalid type");
});

$I->expectException(InvalidArgumentException::class, function () {
$this->cache->setMultiple(["Invalid key@" => "value1"]);
});

$I->expectException(InvalidArgumentException::class, function () {
$this->cache->getMultiple(["Invalid key@"]);
});
}

public function testDeleteMultiple(IntegrationTester $I)
Expand All @@ -159,6 +189,14 @@ public function testDeleteMultiple(IntegrationTester $I)
$I->assertSame(["key1" => null, "key2" => null], $this->cache->getMultiple(["key1", "key2"]));

$I->assertSame("value3", $this->cache->get("key3"));

$I->expectException(InvalidArgumentException::class, function () {
$this->cache->deleteMultiple("Invalid type");
});

$I->expectException(InvalidArgumentException::class, function () {
$this->cache->deleteMultiple(["Invalid key@"]);
});
}

public function testHas(IntegrationTester $I)
Expand Down

0 comments on commit 644d7f6

Please sign in to comment.