forked from nella/doctrine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cache.php
130 lines (116 loc) · 3.12 KB
/
Cache.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* This file is part of the Nella Framework (http://nellafw.org).
*
* Copyright (c) 2006, 2012 Patrik Votoček (http://patrik.votocek.cz)
*
* For the full copyright and license information,
* please view the file LICENSE.txt that was distributed with this source code.
*/
namespace Nella\Doctrine;
use Nette\Caching\Cache as NCache,
Nette\Caching\IStorage,
Doctrine\ORM\Mapping\ClassMetadata,
Nette\Reflection\ClassType;
/**
* Nette cache driver for doctrine
*
* @author Patrik Votoček
*/
class Cache extends \Doctrine\Common\Cache\CacheProvider
{
const CACHE_TAG = 'doctrine',
CACHE_NAMESPACE = 'Nella.Doctrine';
/** @var \Nette\Caching\Cache */
private $storage = array();
/**
* @param \Nette\Caching\IStorage
* @param string
*/
public function __construct(IStorage $cacheStorage, $name = self::CACHE_NAMESPACE)
{
$this->storage = new NCache($cacheStorage, $name);
}
/**
* Fetches an entry from the cache.
*
* @param string cache id The id of the cache entry to fetch.
* @return string The cached data or FALSE, if no cache entry exists for the given id.
*/
protected function doFetch($id)
{
return $this->storage->load($id) ?: FALSE;
}
/**
* Test if an entry exists in the cache
*
* @param string cache id The cache id of the entry to check for.
* @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise.
*/
protected function doContains($id)
{
return $this->storage->load($id) !== NULL;
}
/**
* Puts data into the cache
*
* @param string The cache id.
* @param string The cache entry/data.
* @param int The lifetime. If != false, sets a specific lifetime for this cache entry (null => infinite lifeTime).
* @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise.
*/
protected function doSave($id, $data, $lifeTime = FALSE)
{
$files = array();
if ($data instanceof ClassMetadata) {
$ref = ClassType::from($data->name);
$files[] = $ref->getFileName();
foreach ($data->parentClasses as $class) {
$ref = ClassType::from($class);
$files[] = $ref->getFileName();
}
}
if ($lifeTime != 0) {
$this->storage->save($id, $data, array(
NCache::EXPIRE => time() + $lifeTime,
NCache::TAGS => array(static::CACHE_TAG),
NCache::FILES => $files,
));
} else {
$this->storage->save($id, $data, array(
NCache::TAGS => array(static::CACHE_TAG),
NCache::FILES => $files,
));
}
return TRUE;
}
/**
* Deletes a cache entry.
*
* @param string cache id
* @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
*/
protected function doDelete($id)
{
$this->storage->save($id, NULL);
return TRUE;
}
/**
* Deletes all cache entries
*
* @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
*/
protected function doFlush()
{
$this->storage->clean(array(NCache::ALL => TRUE));
}
/**
* Retrieves cached information from data store
*
* @return array An associative array with server's statistics if available, NULL otherwise.
*/
protected function doGetStats()
{
return NULL; // @TODO
}
}