/classes/cache.php

https://github.com/magicmarkker/core · PHP · 157 lines · 58 code · 22 blank · 77 comment · 2 complexity · d6556f97a2d6b2e381e0144b06ba34ef MD5 · raw file

  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2011 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. // Exception thrown when the Cache wasn't found
  14. class CacheNotFoundException extends \OutOfBoundsException {}
  15. // Exception thrown when the Cache was found but expired (auto deleted)
  16. class CacheExpiredException extends \CacheNotFoundException {}
  17. class Cache
  18. {
  19. /**
  20. * Loads any default caching settings when available
  21. */
  22. public static function _init()
  23. {
  24. \Config::load('cache', true);
  25. }
  26. /**
  27. * Creates a new cache instance.
  28. *
  29. * @param mixed The identifier of the cache, can be anything but empty
  30. * @param array|string Either an array of settings or the storage driver to be used
  31. * @return Cache_Storage_Driver The new cache object
  32. * @deprecated until 1.2
  33. */
  34. public static function factory($identifier, $config = array())
  35. {
  36. logger(\Fuel::L_WARNING, 'This method is deprecated. Please use a forge() instead.', __METHOD__);
  37. return static::forge($identifier, $config);
  38. }
  39. /**
  40. * Creates a new cache instance.
  41. *
  42. * @param mixed The identifier of the cache, can be anything but empty
  43. * @param array|string Either an array of settings or the storage driver to be used
  44. * @return Cache_Storage_Driver The new cache object
  45. */
  46. public static function forge($identifier, $config = array())
  47. {
  48. // load the default config
  49. $defaults = \Config::get('cache', array());
  50. // $config can be either an array of config settings or the name of the storage driver
  51. if ( ! empty($config) and ! is_array($config) and ! is_null($config))
  52. {
  53. $config = array('driver' => $config);
  54. }
  55. // Overwrite default values with given config
  56. $config = array_merge($defaults, (array) $config);
  57. if (empty($config['driver']))
  58. {
  59. throw new \FuelException('No cache driver given or no default cache driver set.');
  60. }
  61. $class = '\\Cache_Storage_'.ucfirst($config['driver']);
  62. // Convert the name to a string when necessary
  63. $identifier = call_user_func($class.'::stringify_identifier', $identifier);
  64. // Return instance of the requested cache object
  65. return new $class($identifier, $config);
  66. }
  67. /**
  68. * Front for writing the cache, ensures interchangebility of storage drivers. Actual writing
  69. * is being done by the _set() method which needs to be extended.
  70. *
  71. * @param mixed The identifier of the cache, can be anything but empty
  72. * @param mixed The content to be cached
  73. * @param int The time in seconds until the cache will expire, =< 0 or null means no expiration
  74. * @param array Contains the identifiers of caches this one will depend on (not supported by all drivers!)
  75. * @return Cache_Storage_Driver The new Cache object
  76. */
  77. public static function set($identifier, $contents = null, $expiration = false, $dependencies = array())
  78. {
  79. $contents = \Fuel::value($contents);
  80. $cache = static::forge($identifier);
  81. return $cache->set($contents, $expiration, $dependencies);
  82. }
  83. /**
  84. * Does get() & set() in one call that takes a callback and it's arguements to generate the contents
  85. *
  86. * @param mixed The identifier of the cache, can be anything but empty
  87. * @param string|array Valid PHP callback
  88. * @param array Arguements for the above function/method
  89. * @param int Cache expiration in seconds
  90. * @param array Contains the identifiers of caches this one will depend on (not supported by all drivers!)
  91. * @return mixed
  92. */
  93. public static function call($identifier, $callback, $args = array(), $expiration = null, $dependencies = array())
  94. {
  95. $cache = static::forge($identifier);
  96. return $cache->call($callback, $args, $expiration, $dependencies);
  97. }
  98. /**
  99. * Front for reading the cache, ensures interchangebility of storage drivers. Actual reading
  100. * is being done by the _get() method which needs to be extended.
  101. *
  102. * @param mixed The identifier of the cache, can be anything but empty
  103. * @param bool
  104. * @return mixed
  105. */
  106. public static function get($identifier, $use_expiration = true)
  107. {
  108. $cache = static::forge($identifier);
  109. return $cache->get($use_expiration);
  110. }
  111. /**
  112. * Frontend for deleting item from the cache, interchangable storage methods. Actual operation
  113. * handled by delete() call on storage driver class
  114. *
  115. * @param mixed The identifier of the cache, can be anything but empty
  116. */
  117. public static function delete($identifier)
  118. {
  119. $cache = static::forge($identifier);
  120. return $cache->delete();
  121. }
  122. /**
  123. * Flushes the whole cache for a specific storage driver or just a part of it when $section is set
  124. * (might not work with all storage drivers), defaults to the default storage driver
  125. *
  126. * @param null|string
  127. * @param null|string
  128. * @return bool
  129. */
  130. public static function delete_all($section = null, $driver = null)
  131. {
  132. $cache = static::forge('__NOT_USED__', $driver);
  133. return $cache->delete_all($section);
  134. }
  135. }