Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
dantodev committed Apr 14, 2016
1 parent dab19c9 commit 3e188f0
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 8 deletions.
68 changes: 63 additions & 5 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ class Cache
/** @var CacheElement[] */
private $_cache = [];

/**
* Cache constructor.
* @param $path
* @param int $default_lifetime
* @param bool $default_refresh
*/
public function __construct($path, $default_lifetime = 60, $default_refresh = false)
{
$this->_path = $path;
$this->_default_lifetime = $default_lifetime;
$this->_default_refresh = $default_refresh;
}

/**
* @param $key
* @return bool
*/
public function has($key)
{
if (!is_null($element = $this->getOrFetchCacheElement($key))) {
Expand All @@ -29,6 +39,11 @@ public function has($key)
return false;
}

/**
* @param $key
* @param null $default_value
* @return null|string
*/
public function get($key, $default_value = null)
{
if (!is_null($element = $this->getOrFetchCacheElement($key))) {
Expand All @@ -41,20 +56,31 @@ public function get($key, $default_value = null)
return $default_value;
}

/**
* @param $key
* @param $value
* @param int|null $lifetime
* @param bool|null $refresh
* @return $this
*/
public function set($key, $value, $lifetime = null, $refresh = null)
{
if (is_null($element = $this->getOrFetchCacheElement($key))) {
$this->_cache[$key] = $this->newCacheElement($key, $value, $lifetime, $refresh);
} else {
$element->update(
$value,
$lifetime ?: $this->_default_lifetime,
$lifetime ?: $element->getLifetime(),
$refresh ?: $this->_default_refresh
);
}
return $this;
}

/**
* @param $key
* @return $this
*/
public function forget($key)
{
if (!is_null($element = $this->getOrFetchCacheElement($key))) {
Expand All @@ -64,6 +90,13 @@ public function forget($key)
return $this;
}

/**
* @param $key
* @param \Closure $call
* @param int|null $lifetime
* @param bool|null $refresh
* @return $this
*/
public function remember($key, \Closure $call, $lifetime = null, $refresh = null)
{
if (!$this->has($key)) {
Expand All @@ -72,21 +105,46 @@ public function remember($key, \Closure $call, $lifetime = null, $refresh = null
return $this;
}

public function flush()
/**
* @param bool $keep_files
* @return $this
*/
public function flush($keep_files = false)
{
array_map("unlink", glob($this->_path . "/cache_*.json"));
if (!$keep_files) {
array_map("unlink", glob($this->path() . DIRECTORY_SEPARATOR . "cache_*.json"));
}
$this->_cache = [];
return $this;
}

/**
* @return $this
*/
public function writeCache()
{
foreach ($this->_cache as $element) {
$element->writeToFs();
}
return $this;
}

/**
* @return string
*/
private function path()
{
return rtrim($this->_path, DIRECTORY_SEPARATOR);
}

/**
* @param $key
* @return string
*/
public function getPathForKey($key)
{
return $this->_path . "/cache_" . md5($key) . ".json";

return $this->path() . DIRECTORY_SEPARATOR . "cache_" . md5($key) . ".json";
}

/**
Expand Down Expand Up @@ -119,7 +177,7 @@ private function fetchCacheElement($key)
{
$path = $this->getPathForKey($key);
if (is_file($path)) {
$cache = json_decode(file_get_contents($path));
$cache = json_decode(file_get_contents($path), true);
$element = new CacheElement(
$this,
$key,
Expand Down
13 changes: 12 additions & 1 deletion src/CacheElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CacheElement
private $_lifetime;
private $_start;
private $_refresh;
private $_is_written;
private $_modified;

/**
Expand All @@ -28,6 +29,7 @@ public function __construct(Cache $cache_instance, $key, $value, $lifetime = 60,
$this->_lifetime = $lifetime;
$this->_start = $start ?: time();
$this->_refresh = $refresh;
$this->_is_written = $start !== null;
$this->_modified = $start === null;
}

Expand Down Expand Up @@ -56,6 +58,11 @@ public function getValue()
return $this->_value;
}

public function getLifeTime()
{
return $this->_lifetime;
}

/**
*
*/
Expand All @@ -69,6 +76,7 @@ public function writeToFs()
"refresh" => $this->_refresh,
]));
}
$this->_is_written = true;
$this->_modified = false;
}

Expand All @@ -77,7 +85,10 @@ public function writeToFs()
*/
public function removeFromFs()
{
return unlink($this->path());
if ($this->_is_written) {
return unlink($this->path());
}
return true;
}

/**
Expand Down
52 changes: 50 additions & 2 deletions test/FileCacheTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
<?php namespace Dtkahl\SimpleConfig;
<?php namespace Dtkahl\FileCache;

class FileCacheTest extends \PHPUnit_Framework_TestCase
{

public function test()
{
var_dump(time());
$cache = new Cache(__DIR__."/test_path/");

// get if not set
$this->assertNull($cache->get("foo"));
$this->assertEquals("default", $cache->get("foo", "default"));

// test has = false
$this->assertFalse($cache->has("foo"));

// set & get if set
$cache->set("foo", "bar");
$this->assertEquals("bar", $cache->get("foo"));

// test has = true
$this->assertTrue($cache->has("foo"));

//test if cache has been written
$cache->writeCache()->flush(true);
$this->assertEquals("bar", $cache->get("foo"));

// test remove
$cache->forget("foo");
$this->assertNull($cache->get("foo"));

// test remember
$cache->remember("foo2", function () {
return "bar2";
});
$cache->remember("foo2", function () {
return "this will never be set";
});
$this->assertEquals("bar2", $cache->get("foo2"));

// test timeout
$cache->set("foo3", "bar3", 1);
sleep(2);
$this->assertNull($cache->get("foo3"));

// test refresh
$cache->set("foo4", "bar4", 4);
sleep(2);
$this->assertEquals("bar4", $cache->get("foo4"));
sleep(2);
$this->assertEquals("bar4", $cache->get("foo4"));
sleep(5);
$this->assertNull($cache->get("foo4"));

// test flush
$cache->flush();
}

}
3 changes: 3 additions & 0 deletions test/test_path/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FileCacheTest will cache to this directory.

This file is only present to commit this dir :P

0 comments on commit 3e188f0

Please sign in to comment.