/
RedisPageCache.php
105 lines (92 loc) · 2.28 KB
/
RedisPageCache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Concrete\Core\Cache\Page;
use Stash\Pool;
use Concrete\Core\Page\Page as ConcretePage;
use Concrete\Core\Support\Facade\Facade;
use Concrete\Core\Cache\Driver\RedisStashDriver as Redis;
/**
* Class RedisPageCache
* @author Derek Cameron <info@derekcameron.com>
*/
class RedisPageCache extends PageCache
{
/**
* @var Pool
*/
public static $pool;
/**
* RedisPageCache constructor.
*/
public function __construct()
{
$app = Facade::getFacadeApplication();
$driver = new Redis($app['config']->get('concrete.cache.page.redis'));
self::$pool = new Pool($driver);
}
/**
* @param \Concrete\Core\Http\Request|ConcretePage|mixed $mixed
* @return PageCacheRecord|mixed|null
*/
public function getRecord($mixed)
{
$item = $this->getCacheItem($mixed);
$record = $item->get();
if ($record instanceof PageCacheRecord) {
return $record;
}
}
/**
* @param ConcretePage $c
* @param string $content
*/
public function set(ConcretePage $c, $content)
{
if ($content) {
$item = $this->getCacheItem($c);
// Let other processes know that this one is rebuilding the data.
$item->lock();
$lifetime = $c->getCollectionFullPageCachingLifetimeValue();
$response = new PageCacheRecord($c, $content, $lifetime);
$item->set($response);
self::$pool->save($item);
}
}
/**
* @param PageCacheRecord $rec
*/
public function purgeByRecord(PageCacheRecord $rec)
{
$item = $this->getCacheItem($rec);
if ($item !== null) {
$item->clear();
}
}
/**
* @param ConcretePage $c
*/
public function purge(ConcretePage $c)
{
$item = $this->getCacheItem($c);
if ($item !== null) {
$item->clear();
}
}
/**
*
*/
public function flush()
{
self::$pool->clear();
}
/**
* @param $mixed
* @return \Stash\Interfaces\ItemInterface
*/
protected function getCacheItem($mixed)
{
$key = $this->getCacheKey($mixed);
if ($key) {
return self::$pool->getItem($key);
}
}
}