Skip to content

Commit 28e67ce

Browse files
committed
Ensure unique prefix for cache keys
1 parent 618372e commit 28e67ce

6 files changed

Lines changed: 56 additions & 16 deletions

File tree

qa-include/Q2A/Storage/CacheDriver.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public function isEnabled();
7575
*/
7676
public function getError();
7777

78+
/**
79+
* Get the prefix used for all cache keys.
80+
*
81+
* @return string
82+
*/
83+
public function getKeyPrefix();
84+
7885
/**
7986
* Get current statistics for the cache.
8087
*

qa-include/Q2A/Storage/CacheFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static function getCacheDriver()
3636
if (self::$cacheDriver === null) {
3737
$config = array(
3838
'enabled' => (int) qa_opt('caching_enabled') === 1,
39+
'keyprefix' => QA_FINAL_MYSQL_DATABASE . '.' . QA_MYSQL_TABLE_PREFIX . '.',
3940
'dir' => defined('QA_CACHE_DIRECTORY') ? QA_CACHE_DIRECTORY : null,
4041
);
4142

qa-include/Q2A/Storage/FileCacheDriver.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
2727
{
2828
private $enabled = false;
29+
private $keyPrefix = '';
2930
private $error;
3031
private $cacheDir;
3132

@@ -39,6 +40,10 @@ public function __construct($config)
3940
return;
4041
}
4142

43+
if (isset($config['keyprefix'])) {
44+
$this->keyPrefix = $config['keyprefix'];
45+
}
46+
4247
if (isset($config['dir'])) {
4348
$this->cacheDir = realpath($config['dir']);
4449

@@ -67,14 +72,15 @@ public function get($key)
6772
return null;
6873
}
6974

70-
$file = $this->getFilename($key);
75+
$fullKey = $this->keyPrefix . $key;
76+
$file = $this->getFilename($fullKey);
7177

7278
if (is_readable($file)) {
7379
$lines = file($file, FILE_IGNORE_NEW_LINES);
7480
$actualKey = array_shift($lines);
7581

7682
// double check this is the correct data
77-
if ($key === $actualKey) {
83+
if ($fullKey === $actualKey) {
7884
$expiry = array_shift($lines);
7985

8086
if (is_numeric($expiry) && time() < $expiry) {
@@ -103,13 +109,14 @@ public function set($key, $data, $ttl)
103109
{
104110
$success = false;
105111
$ttl = (int) $ttl;
112+
$fullKey = $this->keyPrefix . $key;
106113

107114
if ($this->enabled && $ttl > 0) {
108115
$encData = serialize($data);
109116
$expiry = time() + ($ttl * 60);
110-
$cache = $key . "\n" . $expiry . "\n" . $encData;
117+
$cache = $fullKey . "\n" . $expiry . "\n" . $encData;
111118

112-
$file = $this->getFilename($key);
119+
$file = $this->getFilename($fullKey);
113120
$dir = dirname($file);
114121
if (is_dir($dir) || mkdir($dir, 0777, true)) {
115122
$success = @file_put_contents($file, $cache) !== false;
@@ -127,10 +134,10 @@ public function set($key, $data, $ttl)
127134
*/
128135
public function delete($key)
129136
{
130-
if ($this->enabled) {
131-
$file = $this->getFilename($key);
132-
$dir = dirname($key);
137+
$fullKey = $this->keyPrefix . $key;
133138

139+
if ($this->enabled) {
140+
$file = $this->getFilename($fullKey);
134141
return $this->deleteFile($file);
135142
}
136143

@@ -208,6 +215,16 @@ public function getError()
208215
return $this->error;
209216
}
210217

218+
/**
219+
* Get the prefix used for all cache keys.
220+
*
221+
* @return string
222+
*/
223+
public function getKeyPrefix()
224+
{
225+
return $this->keyPrefix;
226+
}
227+
211228
/**
212229
* Get current statistics for the cache.
213230
*
@@ -255,13 +272,13 @@ private function deleteFile($file)
255272

256273
/**
257274
* Generates filename for cache key, of the form `1/23/123abc`
258-
* @param string $key The unique cache key.
275+
* @param string $key The unique cache key (including prefix).
259276
*
260277
* @return string
261278
*/
262-
private function getFilename($key)
279+
private function getFilename($fullKey)
263280
{
264-
$filename = sha1($key);
281+
$filename = sha1($fullKey);
265282
return $this->cacheDir . '/' . substr($filename, 0, 1) . '/' . substr($filename, 1, 2) . '/' . $filename;
266283
}
267284
}

qa-include/Q2A/Storage/MemcachedDriver.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Q2A_Storage_MemcachedDriver implements Q2A_Storage_CacheDriver
2727
{
2828
private $memcached;
2929
private $enabled = false;
30+
private $keyPrefix = '';
3031
private $error;
3132
private $flushed = false;
3233

@@ -45,10 +46,14 @@ public function __construct($config)
4546
return;
4647
}
4748

49+
if (isset($config['keyprefix'])) {
50+
$this->keyPrefix = $config['keyprefix'];
51+
}
52+
4853
if (extension_loaded('memcached')) {
4954
$this->memcached = new Memcached;
5055
$this->memcached->addServer(self::HOST, self::PORT);
51-
if ($this->memcached->set('q2a.test', 'TEST')) {
56+
if ($this->memcached->set($this->keyPrefix . 'test', 'TEST')) {
5257
$this->enabled = true;
5358
} else {
5459
$this->setMemcachedError();
@@ -70,7 +75,7 @@ public function get($key)
7075
return null;
7176
}
7277

73-
$result = $this->memcached->get($key);
78+
$result = $this->memcached->get($this->keyPrefix . $key);
7479

7580
if ($result === false) {
7681
$this->setMemcachedError();
@@ -96,7 +101,7 @@ public function set($key, $data, $ttl)
96101

97102
$ttl = (int) $ttl;
98103
$expiry = time() + ($ttl * 60);
99-
$success = $this->memcached->set($key, $data, $expiry);
104+
$success = $this->memcached->set($this->keyPrefix . $key, $data, $expiry);
100105

101106
if (!$success) {
102107
$this->setMemcachedError();
@@ -117,7 +122,7 @@ public function delete($key)
117122
return false;
118123
}
119124

120-
$success = $this->memcached->delete($key);
125+
$success = $this->memcached->delete($this->keyPrefix . $key);
121126

122127
if (!$success) {
123128
$this->setMemcachedError();
@@ -169,6 +174,16 @@ public function getError()
169174
return $this->error;
170175
}
171176

177+
/**
178+
* Get the prefix used for all cache keys.
179+
*
180+
* @return string
181+
*/
182+
public function getKeyPrefix()
183+
{
184+
return $this->keyPrefix;
185+
}
186+
172187
/**
173188
* Get current statistics for the cache.
174189
*

qa-include/pages/question.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
// Get information about this question
4343

4444
$cacheDriver = Q2A_Storage_CacheFactory::getCacheDriver();
45-
$cacheKey = "q2a.question:$questionid";
45+
$cacheKey = "question:$questionid";
4646
$useCache = $userid === null && $cacheDriver->isEnabled() && !qa_is_http_post() && empty($pagestate);
4747
$saveCache = false;
4848

qa-include/qa-db.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ function qa_db_single_select($selectspec)
524524
// check for cached results
525525
if (isset($selectspec['caching'])) {
526526
$cacheDriver = Q2A_Storage_CacheFactory::getCacheDriver();
527-
$cacheKey = 'q2a.query:' . $selectspec['caching']['key'];
527+
$cacheKey = 'query:' . $selectspec['caching']['key'];
528528

529529
if ($cacheDriver->isEnabled()) {
530530
$queryData = $cacheDriver->get($cacheKey);

0 commit comments

Comments
 (0)