CacheManager.php 2.69 KB
Newer Older
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
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-include/Q2A/Storage/CacheManager.php
	Description: Handler for caching system.


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

/**
 * Caches data (typically from database queries) to the filesystem.
 */
class Q2A_Storage_CacheManager
{
28
	private static $instance;
29
	private $enabled = false;
30
	private $cacheDriver;
31 32

	/**
33
	 * Creates a new CacheManager instance and sets up the cache driver.
34
	 */
35
	private function __construct()
36
	{
37 38
		if (qa_opt('caching_enabled') != 1)
			return;
39

40 41 42
		$config = array(
			'dir' => defined('QA_CACHE_DIRECTORY') ? QA_CACHE_DIRECTORY : null,
		);
43

44
		$this->cacheDriver = new Q2A_Storage_FileCache($config);
45
		$this->enabled = $this->cacheDriver->isEnabled();
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
	}

	/**
	 * Initializes the class and returns the singleton.
	 * @return Q2A_Storage_CacheManager
	 */
	public static function getInstance()
	{
		if (!isset(self::$instance)) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Get the cached data for the supplied key.
	 * @param string $key The unique cache identifier
	 * @return mixed The cached data, or null otherwise.
	 */
	public function get($key)
	{
		if ($this->enabled) {
			$encData = $this->cacheDriver->get($key);

			// retrieve data, ignoring any notices
			$data = @unserialize($encData);
			if ($data !== false) {
				return $data;
75
			}
76 77 78 79
		}

		return null;
	}
80

81 82 83 84 85 86 87 88 89 90 91 92
	/**
	 * Serialize some data and store it in the cache.
	 * @param string $key The unique cache identifier
	 * @param mixed $data The data to cache - must be scalar values (i.e. string, int, array).
	 * @param int $ttl Number of minutes for which to cache the data.
	 * @return bool Whether the data was successfully cached.
	 */
	public function set($key, $data, $ttl)
	{
		if ($this->enabled) {
			$encData = serialize($data);
			return $this->cacheDriver->set($key, $encData, $ttl);
93
		}
94 95

		return false;
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	}

	/**
	 * Whether caching is available.
	 * @return bool
	 */
	public function isEnabled()
	{
		return $this->enabled;
	}

	/**
	 * Get the last error.
	 * @return string
	 */
	public function getError()
	{
113
		return isset($this->cacheDriver) ? $this->cacheDriver->getError() : '';
114 115
	}
}