Commit f4c18bae by Scott

Add interface for cache drivers

parent 7db0cba8
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
File: qa-include/Q2A/Storage/FileCache.php
Description: Interface for drivers of 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
*/
/**
* Interface for caching drivers.
*/
interface Q2A_Storage_CacheDriver
{
/**
* Get the cached data for the supplied key. Data can be any format but is usually an array.
* @param string $key The unique cache identifier.
*
* @return mixed The cached data, or null otherwise.
*/
public function get($key);
/**
* Store something in the cache along with the key and expiry time. Data gets 'serialized' to a string before storing.
* @param string $key The unique cache identifier.
* @param mixed $data The data to cache (in core Q2A this is usually an array).
* @param int $ttl Number of minutes for which to cache the data.
*
* @return bool Whether the file was successfully cached.
*/
public function set($key, $data, $ttl);
/**
* Delete an item from the cache.
* @param string $key The unique cache identifier.
*
* @return bool Whether the operation succeeded.
*/
public function delete($key);
/**
* Delete multiple items from the cache.
* @param int $limit Maximum number of items to process. 0 = unlimited
* @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.
*/
public function clear($limit = 0, $start = 0, $expiredOnly = false);
/**
* Whether caching is available.
*
* @return bool
*/
public function isEnabled();
/**
* Get the last error.
*
* @return string
*/
public function getError();
/**
* Get current statistics for the cache.
*
* @return array Array of stats: 'files' => number of files, 'size' => total file size in bytes.
*/
public function getStats();
}
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
/** /**
* Caches data (typically from database queries) to the filesystem. * Caches data (typically from database queries) to the filesystem.
*/ */
class Q2A_Storage_FileCacheDriver class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
{ {
private $enabled = false; private $enabled = false;
private $error; private $error;
...@@ -58,6 +58,7 @@ class Q2A_Storage_FileCacheDriver ...@@ -58,6 +58,7 @@ class Q2A_Storage_FileCacheDriver
/** /**
* Get the cached data for the supplied key. * Get the cached data for the supplied key.
* @param string $key The unique cache identifier. * @param string $key The unique cache identifier.
*
* @return string The cached data, or null otherwise. * @return string The cached data, or null otherwise.
*/ */
public function get($key) public function get($key)
...@@ -95,6 +96,7 @@ class Q2A_Storage_FileCacheDriver ...@@ -95,6 +96,7 @@ class Q2A_Storage_FileCacheDriver
* @param string $key The unique cache identifier. * @param string $key The unique cache identifier.
* @param mixed $data The data to cache (in core Q2A this is usually an array). * @param mixed $data The data to cache (in core Q2A this is usually an array).
* @param int $ttl Number of minutes for which to cache the data. * @param int $ttl Number of minutes for which to cache the data.
*
* @return bool Whether the file was successfully cached. * @return bool Whether the file was successfully cached.
*/ */
public function set($key, $data, $ttl) public function set($key, $data, $ttl)
...@@ -119,7 +121,8 @@ class Q2A_Storage_FileCacheDriver ...@@ -119,7 +121,8 @@ class Q2A_Storage_FileCacheDriver
/** /**
* Delete an item from the cache. * Delete an item from the cache.
* @param string $key The unique cache identifier. * @param string $key The unique cache identifier.
*
* @return bool Whether the operation succeeded. * @return bool Whether the operation succeeded.
*/ */
public function delete($key) public function delete($key)
...@@ -136,9 +139,10 @@ class Q2A_Storage_FileCacheDriver ...@@ -136,9 +139,10 @@ class Q2A_Storage_FileCacheDriver
/** /**
* Delete multiple items from the cache. * Delete multiple items from the cache.
* @param int $limit Maximum number of items to process. 0 = unlimited * @param int $limit Maximum number of items to process. 0 = unlimited
* @param int $start Offset from which to start (used for 'batching' deletes). * @param int $start Offset from which to start (used for 'batching' deletes).
* @param bool $expiredOnly Delete cache only if it has expired. * @param bool $expiredOnly Delete cache only if it has expired.
*
* @return int Number of files deleted. * @return int Number of files deleted.
*/ */
public function clear($limit = 0, $start = 0, $expiredOnly = false) public function clear($limit = 0, $start = 0, $expiredOnly = false)
...@@ -186,6 +190,7 @@ class Q2A_Storage_FileCacheDriver ...@@ -186,6 +190,7 @@ class Q2A_Storage_FileCacheDriver
/** /**
* Whether caching is available. * Whether caching is available.
*
* @return bool * @return bool
*/ */
public function isEnabled() public function isEnabled()
...@@ -195,6 +200,7 @@ class Q2A_Storage_FileCacheDriver ...@@ -195,6 +200,7 @@ class Q2A_Storage_FileCacheDriver
/** /**
* Get the last error. * Get the last error.
*
* @return string * @return string
*/ */
public function getError() public function getError()
...@@ -204,6 +210,7 @@ class Q2A_Storage_FileCacheDriver ...@@ -204,6 +210,7 @@ class Q2A_Storage_FileCacheDriver
/** /**
* Get current statistics for the cache. * 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: 'files' => number of files, 'size' => total file size in bytes.
*/ */
public function getStats() public function getStats()
...@@ -233,7 +240,8 @@ class Q2A_Storage_FileCacheDriver ...@@ -233,7 +240,8 @@ class Q2A_Storage_FileCacheDriver
/** /**
* Delete a specific file * Delete a specific file
* @param string $file Filename to delete * @param string $file Filename to delete.
*
* @return bool Whether the file was deleted successfully. * @return bool Whether the file was deleted successfully.
*/ */
private function deleteFile($file) private function deleteFile($file)
...@@ -248,6 +256,7 @@ class Q2A_Storage_FileCacheDriver ...@@ -248,6 +256,7 @@ class Q2A_Storage_FileCacheDriver
/** /**
* Generates filename for cache key, of the form `1/23/123abc` * Generates filename for cache key, of the form `1/23/123abc`
* @param string $key The unique cache key. * @param string $key The unique cache key.
*
* @return string * @return string
*/ */
private function getFilename($key) private function getFilename($key)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment