This repository has been archived by the owner on May 31, 2020. It is now read-only.
/
CachedQuery.php
executable file
·82 lines (70 loc) · 2.21 KB
/
CachedQuery.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
<?php
namespace JamesMoss\Flywheel;
/**
* CachedQuery
*
* A extension of Query that is able to store results in shared memory (via
* the apc_* functions) so that they can be retieved for future use with
* minimal performance impact.
*/
class CachedQuery extends Query
{
/**
* Checks the cache to see if this query exists and returns it. If it's
* not in the cache then the query is run, stored and then returned.
*
* @return Result The result of the query.
*/
public function execute()
{
// Generate a cache key by comparing our parameters to see if we've
// made this query before
$key = $this->getParameterHash().$this->getFileHash();
// Try and fetch a cached result object from APC
$result = apc_fetch($key, $success);
// If the result isn't in the cache then we run the real query
if (!$success) {
$result = parent::execute();
apc_store($key, $result);
}
return $result;
}
/**
* Gets a hash based on the files in the repo directory. If the contents
* of a file changes, or other files are added/deleted the hash will change.
* Uses filematime() for speed when checking for file changes (rather than
* using crc32 or md5 etc)
*
* @return string A 128bit hash in hexadecimal format.
*/
protected function getFileHash()
{
$path = $this->repo->getPath();
$files = scandir($path);
$hash = '';
foreach ($files as $file) {
if ($file == '..' || $file == '.') {
continue;
}
$hash .= $file.'|';
$hash .= (string) filemtime($path.'/'.$file).'|';
}
$hash = md5($hash);
return $hash;
}
/**
* Generates a hash based on the parameters set in the query.
*
* @return string A 128bit hash in hexadecimal format.
*/
protected function getParameterHash()
{
$parts = array(
$this->repo->getName(),
serialize((array) $this->limit),
serialize((array) $this->orderBy),
serialize((array) $this->where),
);
return md5(implode('|', $parts));
}
}