Skip to content

Commit 3e188f0

Browse files
committed
dev
1 parent dab19c9 commit 3e188f0

File tree

4 files changed

+128
-8
lines changed

4 files changed

+128
-8
lines changed

src/Cache.php

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@ class Cache
1010
/** @var CacheElement[] */
1111
private $_cache = [];
1212

13+
/**
14+
* Cache constructor.
15+
* @param $path
16+
* @param int $default_lifetime
17+
* @param bool $default_refresh
18+
*/
1319
public function __construct($path, $default_lifetime = 60, $default_refresh = false)
1420
{
1521
$this->_path = $path;
1622
$this->_default_lifetime = $default_lifetime;
1723
$this->_default_refresh = $default_refresh;
1824
}
1925

26+
/**
27+
* @param $key
28+
* @return bool
29+
*/
2030
public function has($key)
2131
{
2232
if (!is_null($element = $this->getOrFetchCacheElement($key))) {
@@ -29,6 +39,11 @@ public function has($key)
2939
return false;
3040
}
3141

42+
/**
43+
* @param $key
44+
* @param null $default_value
45+
* @return null|string
46+
*/
3247
public function get($key, $default_value = null)
3348
{
3449
if (!is_null($element = $this->getOrFetchCacheElement($key))) {
@@ -41,20 +56,31 @@ public function get($key, $default_value = null)
4156
return $default_value;
4257
}
4358

59+
/**
60+
* @param $key
61+
* @param $value
62+
* @param int|null $lifetime
63+
* @param bool|null $refresh
64+
* @return $this
65+
*/
4466
public function set($key, $value, $lifetime = null, $refresh = null)
4567
{
4668
if (is_null($element = $this->getOrFetchCacheElement($key))) {
4769
$this->_cache[$key] = $this->newCacheElement($key, $value, $lifetime, $refresh);
4870
} else {
4971
$element->update(
5072
$value,
51-
$lifetime ?: $this->_default_lifetime,
73+
$lifetime ?: $element->getLifetime(),
5274
$refresh ?: $this->_default_refresh
5375
);
5476
}
5577
return $this;
5678
}
5779

80+
/**
81+
* @param $key
82+
* @return $this
83+
*/
5884
public function forget($key)
5985
{
6086
if (!is_null($element = $this->getOrFetchCacheElement($key))) {
@@ -64,6 +90,13 @@ public function forget($key)
6490
return $this;
6591
}
6692

93+
/**
94+
* @param $key
95+
* @param \Closure $call
96+
* @param int|null $lifetime
97+
* @param bool|null $refresh
98+
* @return $this
99+
*/
67100
public function remember($key, \Closure $call, $lifetime = null, $refresh = null)
68101
{
69102
if (!$this->has($key)) {
@@ -72,21 +105,46 @@ public function remember($key, \Closure $call, $lifetime = null, $refresh = null
72105
return $this;
73106
}
74107

75-
public function flush()
108+
/**
109+
* @param bool $keep_files
110+
* @return $this
111+
*/
112+
public function flush($keep_files = false)
76113
{
77-
array_map("unlink", glob($this->_path . "/cache_*.json"));
114+
if (!$keep_files) {
115+
array_map("unlink", glob($this->path() . DIRECTORY_SEPARATOR . "cache_*.json"));
116+
}
117+
$this->_cache = [];
118+
return $this;
78119
}
79120

121+
/**
122+
* @return $this
123+
*/
80124
public function writeCache()
81125
{
82126
foreach ($this->_cache as $element) {
83127
$element->writeToFs();
84128
}
129+
return $this;
85130
}
86131

132+
/**
133+
* @return string
134+
*/
135+
private function path()
136+
{
137+
return rtrim($this->_path, DIRECTORY_SEPARATOR);
138+
}
139+
140+
/**
141+
* @param $key
142+
* @return string
143+
*/
87144
public function getPathForKey($key)
88145
{
89-
return $this->_path . "/cache_" . md5($key) . ".json";
146+
147+
return $this->path() . DIRECTORY_SEPARATOR . "cache_" . md5($key) . ".json";
90148
}
91149

92150
/**
@@ -119,7 +177,7 @@ private function fetchCacheElement($key)
119177
{
120178
$path = $this->getPathForKey($key);
121179
if (is_file($path)) {
122-
$cache = json_decode(file_get_contents($path));
180+
$cache = json_decode(file_get_contents($path), true);
123181
$element = new CacheElement(
124182
$this,
125183
$key,

src/CacheElement.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class CacheElement
99
private $_lifetime;
1010
private $_start;
1111
private $_refresh;
12+
private $_is_written;
1213
private $_modified;
1314

1415
/**
@@ -28,6 +29,7 @@ public function __construct(Cache $cache_instance, $key, $value, $lifetime = 60,
2829
$this->_lifetime = $lifetime;
2930
$this->_start = $start ?: time();
3031
$this->_refresh = $refresh;
32+
$this->_is_written = $start !== null;
3133
$this->_modified = $start === null;
3234
}
3335

@@ -56,6 +58,11 @@ public function getValue()
5658
return $this->_value;
5759
}
5860

61+
public function getLifeTime()
62+
{
63+
return $this->_lifetime;
64+
}
65+
5966
/**
6067
*
6168
*/
@@ -69,6 +76,7 @@ public function writeToFs()
6976
"refresh" => $this->_refresh,
7077
]));
7178
}
79+
$this->_is_written = true;
7280
$this->_modified = false;
7381
}
7482

@@ -77,7 +85,10 @@ public function writeToFs()
7785
*/
7886
public function removeFromFs()
7987
{
80-
return unlink($this->path());
88+
if ($this->_is_written) {
89+
return unlink($this->path());
90+
}
91+
return true;
8192
}
8293

8394
/**

test/FileCacheTest.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,59 @@
1-
<?php namespace Dtkahl\SimpleConfig;
1+
<?php namespace Dtkahl\FileCache;
22

33
class FileCacheTest extends \PHPUnit_Framework_TestCase
44
{
55

66
public function test()
77
{
8-
var_dump(time());
8+
$cache = new Cache(__DIR__."/test_path/");
9+
10+
// get if not set
11+
$this->assertNull($cache->get("foo"));
12+
$this->assertEquals("default", $cache->get("foo", "default"));
13+
14+
// test has = false
15+
$this->assertFalse($cache->has("foo"));
16+
17+
// set & get if set
18+
$cache->set("foo", "bar");
19+
$this->assertEquals("bar", $cache->get("foo"));
20+
21+
// test has = true
22+
$this->assertTrue($cache->has("foo"));
23+
24+
//test if cache has been written
25+
$cache->writeCache()->flush(true);
26+
$this->assertEquals("bar", $cache->get("foo"));
27+
28+
// test remove
29+
$cache->forget("foo");
30+
$this->assertNull($cache->get("foo"));
31+
32+
// test remember
33+
$cache->remember("foo2", function () {
34+
return "bar2";
35+
});
36+
$cache->remember("foo2", function () {
37+
return "this will never be set";
38+
});
39+
$this->assertEquals("bar2", $cache->get("foo2"));
40+
41+
// test timeout
42+
$cache->set("foo3", "bar3", 1);
43+
sleep(2);
44+
$this->assertNull($cache->get("foo3"));
45+
46+
// test refresh
47+
$cache->set("foo4", "bar4", 4);
48+
sleep(2);
49+
$this->assertEquals("bar4", $cache->get("foo4"));
50+
sleep(2);
51+
$this->assertEquals("bar4", $cache->get("foo4"));
52+
sleep(5);
53+
$this->assertNull($cache->get("foo4"));
54+
55+
// test flush
56+
$cache->flush();
957
}
1058

1159
}

test/test_path/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FileCacheTest will cache to this directory.
2+
3+
This file is only present to commit this dir :P

0 commit comments

Comments
 (0)