Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some improvements to the cache driver #961

Open
wants to merge 2 commits into
base: bugfix
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions qa-config-example.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@
define('QA_CACHE_DIRECTORY', '/path/to/writable_cache_directory/');
*/

/*
If you wish to use memcached-based caching, you can define the host and port in which the
memcached server resides. Default values are 127.0.0.1 and 11211, respectively. You only need
to define the constats below if you want to use different values for them.

define('QA_MEMCACHED_HOST', '123.123.123.123');
define('QA_MEMCACHED_PORT', 12345);
*/

/*
If you wish, you can define QA_COOKIE_DOMAIN so that any cookies created by Q2A are assigned
to a specific domain name, instead of the full domain name of the request by default. This is
Expand Down
18 changes: 9 additions & 9 deletions qa-include/Q2A/Storage/CacheDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function delete($key);
* @param int $start Offset from which to start (used for 'batching' deletes).
* @param bool $expiredOnly Delete cache only if it has expired.
*
* @return int Number of files deleted.
* @return int Number of items deleted.
*/
public function clear($limit = 0, $start = 0, $expiredOnly = false);

Expand All @@ -68,13 +68,6 @@ public function clear($limit = 0, $start = 0, $expiredOnly = false);
*/
public function isEnabled();

/**
* Get the last error.
*
* @return string
*/
public function getError();

/**
* Get the prefix used for all cache keys.
*
Expand All @@ -85,7 +78,14 @@ public function getKeyPrefix();
/**
* Get current statistics for the cache.
*
* @return array Array of stats: 'files' => number of files, 'size' => total file size in bytes.
* @return array Array of stats: 'items' => number of items, 'size' => total item size in bytes.
*/
public function getStats();

/**
* Perform test operations and return an error string or null if no error was found
*
* @return string|null
*/
public function test();
}
7 changes: 3 additions & 4 deletions qa-include/Q2A/Storage/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,22 @@ public static function getCacheDriver()
$config = array(
'enabled' => (int) qa_opt('caching_enabled') === 1,
'keyprefix' => QA_FINAL_MYSQL_DATABASE . '.' . QA_MYSQL_TABLE_PREFIX . '.',
'dir' => defined('QA_CACHE_DIRECTORY') ? QA_CACHE_DIRECTORY : null,
);

$driver = qa_opt('caching_driver');

switch($driver)
{
switch ($driver) {
case 'memcached':
self::$cacheDriver = new Q2A_Storage_MemcachedDriver($config);
break;

case 'filesystem':
default:
$config['dir'] = defined('QA_CACHE_DIRECTORY') ? QA_CACHE_DIRECTORY : null;

self::$cacheDriver = new Q2A_Storage_FileCacheDriver($config);
break;
}

}

return self::$cacheDriver;
Expand Down
95 changes: 63 additions & 32 deletions qa-include/Q2A/Storage/FileCacheDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
{
private $enabled = false;
private $keyPrefix = '';
private $error;
private $cacheDir;

private $phpProtect = '<?php header($_SERVER[\'SERVER_PROTOCOL\'].\' 404 Not Found\'); die; ?>';
Expand All @@ -46,16 +45,12 @@ public function __construct($config)
$this->keyPrefix = $config['keyprefix'];
}

if (isset($config['dir'])) {
$this->cacheDir = realpath($config['dir']);
if (!is_writable($this->cacheDir)) {
$this->error = qa_lang_html_sub('admin/caching_dir_error', $config['dir']);
}
} else {
$this->error = qa_lang_html('admin/caching_dir_missing');
if (!isset($config['dir'])) {
return;
}

$this->enabled = empty($this->error);
$this->enabled = true;
$this->cacheDir = realpath($config['dir']);
}

/**
Expand Down Expand Up @@ -107,7 +102,7 @@ public function get($key)
public function set($key, $data, $ttl)
{
$success = false;
$ttl = (int) $ttl;
$ttl = (int)$ttl;
$fullKey = $this->keyPrefix . $key;

if ($this->enabled && $ttl > 0) {
Expand Down Expand Up @@ -171,7 +166,7 @@ public function clear($limit = 0, $start = 0, $expiredOnly = false)
$fp = fopen($file, 'r');
$skipLine = fgets($fp);
$key = fgets($fp);
$expiry = (int) trim(fgets($fp));
$expiry = (int)trim(fgets($fp));
if (time() > $expiry) {
$wasDeleted = $this->deleteFile($file);
}
Expand Down Expand Up @@ -205,16 +200,6 @@ public function isEnabled()
return $this->enabled;
}

/**
* Get the last error.
*
* @return string
*/
public function getError()
{
return $this->error;
}

/**
* Get the prefix used for all cache keys.
*
Expand All @@ -228,29 +213,32 @@ public function getKeyPrefix()
/**
* Get current statistics for the cache.
*
* @return array Array of stats: 'files' => number of files, 'size' => total file size in bytes.
* @return array Array of stats: 'items' => number of files, 'size' => total item size in bytes.
*/
public function getStats()
{
if (!$this->enabled) {
return array('files' => 0, 'size' => 0);
return array('items' => 0, 'size' => 0);
}

$totalFiles = 0;
$totalBytes = 0;
$dirIter = new RecursiveDirectoryIterator($this->cacheDir);
foreach (new RecursiveIteratorIterator($dirIter) as $file) {
if (strpos($file->getFilename(), '.') === 0) {
// TODO: use FilesystemIterator::SKIP_DOTS once we're on minimum PHP 5.3
continue;
}
try {
$dirIter = new RecursiveDirectoryIterator($this->cacheDir);
foreach (new RecursiveIteratorIterator($dirIter) as $file) {
if (strpos($file->getFilename(), '.') === 0) {
// TODO: use FilesystemIterator::SKIP_DOTS once we're on minimum PHP 5.3
continue;
}

$totalFiles++;
$totalBytes += $file->getSize();
$totalFiles++;
$totalBytes += $file->getSize();
}
} catch (Exception $e) {
}

return array(
'files' => $totalFiles,
'items' => $totalFiles,
'size' => $totalBytes,
);
}
Expand Down Expand Up @@ -281,4 +269,47 @@ private function getFilename($fullKey)
$filename = sha1($fullKey);
return $this->cacheDir . '/' . substr($filename, 0, 1) . '/' . substr($filename, 1, 2) . '/' . $filename . '.php';
}

/**
* Perform test operations and return an error string or null if no error was found
*
* @return string|null
*/
public function test()
{
if (!$this->enabled) {
return null;
}

if (!isset($this->cacheDir)) {
return qa_lang_html('admin/caching_dir_missing');
}

try {
if (!is_writable($this->cacheDir)) {
throw new Exception();
}

$dirIter = new RecursiveDirectoryIterator($this->cacheDir);
foreach (new RecursiveIteratorIterator($dirIter) as $file) {
if (strpos($file->getFilename(), '.') === 0) {
// TODO: use FilesystemIterator::SKIP_DOTS once we're on minimum PHP 5.3
continue;
}

break;
}

$result = $this->set('test', 'TEST', 1);
if (!$result) {
throw new Exception();
}
} catch (Exception $e) {
return qa_lang_html_sub('admin/caching_dir_error', $this->cacheDir);
} finally {
$this->delete('test');
}

return null;
}
}
Loading