PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/core/classes/cache.php

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