Skip to content

Commit dab19c9

Browse files
committed
dev
1 parent 5bc5b25 commit dab19c9

File tree

4 files changed

+256
-77
lines changed

4 files changed

+256
-77
lines changed

src/Cache.php

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php namespace Dtkahl\FileCache;
2+
3+
class Cache
4+
{
5+
6+
private $_path;
7+
private $_default_lifetime;
8+
private $_default_refresh;
9+
10+
/** @var CacheElement[] */
11+
private $_cache = [];
12+
13+
public function __construct($path, $default_lifetime = 60, $default_refresh = false)
14+
{
15+
$this->_path = $path;
16+
$this->_default_lifetime = $default_lifetime;
17+
$this->_default_refresh = $default_refresh;
18+
}
19+
20+
public function has($key)
21+
{
22+
if (!is_null($element = $this->getOrFetchCacheElement($key))) {
23+
if ($element->isAlive()) {
24+
return true;
25+
}else {
26+
$this->forget($key);
27+
}
28+
}
29+
return false;
30+
}
31+
32+
public function get($key, $default_value = null)
33+
{
34+
if (!is_null($element = $this->getOrFetchCacheElement($key))) {
35+
if ($element->isAlive()) {
36+
return $element->getValue();
37+
}else {
38+
$this->forget($key);
39+
}
40+
}
41+
return $default_value;
42+
}
43+
44+
public function set($key, $value, $lifetime = null, $refresh = null)
45+
{
46+
if (is_null($element = $this->getOrFetchCacheElement($key))) {
47+
$this->_cache[$key] = $this->newCacheElement($key, $value, $lifetime, $refresh);
48+
} else {
49+
$element->update(
50+
$value,
51+
$lifetime ?: $this->_default_lifetime,
52+
$refresh ?: $this->_default_refresh
53+
);
54+
}
55+
return $this;
56+
}
57+
58+
public function forget($key)
59+
{
60+
if (!is_null($element = $this->getOrFetchCacheElement($key))) {
61+
$element->removeFromFs();
62+
unset($this->_cache[$key]);
63+
}
64+
return $this;
65+
}
66+
67+
public function remember($key, \Closure $call, $lifetime = null, $refresh = null)
68+
{
69+
if (!$this->has($key)) {
70+
$this->_cache[$key] = $this->newCacheElement($key, $call(), $lifetime, $refresh);
71+
}
72+
return $this;
73+
}
74+
75+
public function flush()
76+
{
77+
array_map("unlink", glob($this->_path . "/cache_*.json"));
78+
}
79+
80+
public function writeCache()
81+
{
82+
foreach ($this->_cache as $element) {
83+
$element->writeToFs();
84+
}
85+
}
86+
87+
public function getPathForKey($key)
88+
{
89+
return $this->_path . "/cache_" . md5($key) . ".json";
90+
}
91+
92+
/**
93+
* @param $key
94+
* @return CacheElement|null
95+
*/
96+
private function getOrFetchCacheElement($key)
97+
{
98+
$element = $this->getCacheElement($key);
99+
if (is_null($element) && !is_null($element = $this->fetchCacheElement($key))) {
100+
$this->_cache[$key] = $element;
101+
}
102+
return $element;
103+
}
104+
105+
/**
106+
* @param $key
107+
* @return CacheElement|null
108+
*/
109+
private function getCacheElement($key)
110+
{
111+
return array_key_exists($key, $this->_cache) ? $this->_cache[$key] : null;
112+
}
113+
114+
/**
115+
* @param $key
116+
* @return CacheElement|null
117+
*/
118+
private function fetchCacheElement($key)
119+
{
120+
$path = $this->getPathForKey($key);
121+
if (is_file($path)) {
122+
$cache = json_decode(file_get_contents($path));
123+
$element = new CacheElement(
124+
$this,
125+
$key,
126+
$cache["value"],
127+
$cache["lifetime"],
128+
$cache["start"],
129+
$cache["refresh"]
130+
);
131+
$this->_cache[$key] = $element;
132+
return $element;
133+
}
134+
return null;
135+
}
136+
137+
/**
138+
* @param $key
139+
* @param $value
140+
* @param int|null $lifetime
141+
* @param int|null $refresh
142+
* @return CacheElement
143+
*/
144+
private function newCacheElement($key, $value, $lifetime = null, $refresh = null)
145+
{
146+
return new CacheElement(
147+
$this,
148+
$key,
149+
$value,
150+
$lifetime ?: $this->_default_lifetime,
151+
null,
152+
$refresh ?: $this->_default_refresh
153+
);
154+
}
155+
156+
}

src/CacheElement.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php namespace Dtkahl\FileCache;
2+
3+
class CacheElement
4+
{
5+
6+
private $_cache_instance;
7+
private $_key;
8+
private $_value;
9+
private $_lifetime;
10+
private $_start;
11+
private $_refresh;
12+
private $_modified;
13+
14+
/**
15+
* CacheElement constructor.
16+
* @param $cache_instance
17+
* @param $key
18+
* @param $value
19+
* @param int $lifetime
20+
* @param int|null $start
21+
* @param bool $refresh
22+
*/
23+
public function __construct(Cache $cache_instance, $key, $value, $lifetime = 60, $start = null, $refresh = false)
24+
{
25+
$this->_cache_instance = $cache_instance;
26+
$this->_key = (string) $key;
27+
$this->_value = (string) $value;
28+
$this->_lifetime = $lifetime;
29+
$this->_start = $start ?: time();
30+
$this->_refresh = $refresh;
31+
$this->_modified = $start === null;
32+
}
33+
34+
/**
35+
* @param $value
36+
* @param int $lifetime
37+
* @param bool $refresh
38+
*/
39+
public function update($value, $lifetime = 60, $refresh = false)
40+
{
41+
$this->_value = (string) $value;
42+
$this->_lifetime = $lifetime;
43+
$this->_start = time();
44+
$this->_refresh = $refresh;
45+
$this->_modified = true;
46+
}
47+
48+
/**
49+
* @return string
50+
*/
51+
public function getValue()
52+
{
53+
if ($this->_refresh) {
54+
$this->_start = time();
55+
}
56+
return $this->_value;
57+
}
58+
59+
/**
60+
*
61+
*/
62+
public function writeToFs()
63+
{
64+
if ($this->_modified) {
65+
file_put_contents($this->path(), json_encode([
66+
"value" => $this->_value,
67+
"lifetime" => $this->_lifetime,
68+
"start" => $this->_start,
69+
"refresh" => $this->_refresh,
70+
]));
71+
}
72+
$this->_modified = false;
73+
}
74+
75+
/**
76+
* @return bool
77+
*/
78+
public function removeFromFs()
79+
{
80+
return unlink($this->path());
81+
}
82+
83+
/**
84+
* @return bool
85+
*/
86+
public function isAlive()
87+
{
88+
return (time() - $this->_start) <= $this->_lifetime;
89+
}
90+
91+
/**
92+
* @return string
93+
*/
94+
private function path()
95+
{
96+
return $this->_cache_instance->getPathForKey($this->_key);
97+
}
98+
99+
}

src/FileCache.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

test/FileCacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class FileCacheTest extends \PHPUnit_Framework_TestCase
55

66
public function test()
77
{
8-
//
8+
var_dump(time());
99
}
1010

1111
}

0 commit comments

Comments
 (0)