SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Cache.php

Copyright (C) 2017 Karmabunny Pty Ltd.

This file is a part of SproutCMS.

SproutCMS 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.

For more information, visit <http://getsproutcms.com>.

This class was originally from Kohana 2.3.4
Copyright 2007-2008 Kohana Team
  1. <?php
  2. /**
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  *
  13.  * This class was originally from Kohana 2.3.4
  14.  * Copyright 2007-2008 Kohana Team
  15.  */
  16. namespace Sprout\Helpers;
  17.  
  18. use Kohana;
  19. use Kohana_Exception;
  20.  
  21. use Sprout\Helpers\Drivers\CacheDriver;
  22.  
  23.  
  24. /**
  25.  * Provides a driver-based interface for finding, creating, and deleting cached
  26.  * resources. Caches are identified by a unique string. Tagging of caches is
  27.  * also supported, and caches can be found and deleted by id or tag.
  28.  */
  29. class Cache
  30. {
  31.  
  32. protected static $instances = array();
  33.  
  34. // For garbage collection
  35. protected static $loaded;
  36.  
  37. // Configuration
  38. protected $config;
  39.  
  40. // Driver object
  41. protected $driver;
  42.  
  43. /**
  44.   * Returns a singleton instance of Cache.
  45.   *
  46.   * @param string configuration
  47.   * @return Cache
  48.   */
  49. public static function & instance($config = FALSE)
  50. {
  51. if ( ! isset(Cache::$instances[$config]))
  52. {
  53. // Create a new instance
  54. Cache::$instances[$config] = new Cache($config);
  55. }
  56.  
  57. return Cache::$instances[$config];
  58. }
  59.  
  60. /**
  61.   * Loads the configured driver and validates it.
  62.   *
  63.   * @param array|string custom configuration or config group name
  64.   * @return void
  65.   */
  66. public function __construct($config = FALSE)
  67. {
  68. if (is_string($config))
  69. {
  70. $name = $config;
  71.  
  72. // Test the config group name
  73. if (($config = Kohana::config('cache.'.$config)) === NULL)
  74. throw new Kohana_Exception('cache.undefined_group', $name);
  75. }
  76.  
  77. if (is_array($config))
  78. {
  79. // Append the default configuration options
  80. $config += Kohana::config('cache.default');
  81. }
  82. else
  83. {
  84. // Load the default group
  85. $config = Kohana::config('cache.default');
  86. }
  87.  
  88. // Cache the config in the object
  89. $this->config = $config;
  90.  
  91. // Set driver name
  92. $driver = 'Cache_'.ucfirst($this->config['driver']).'_Driver';
  93.  
  94. // Load the driver
  95. if ( ! Kohana::auto_load($driver))
  96. throw new Kohana_Exception('core.driver_not_found', $this->config['driver'], get_class($this));
  97.  
  98. // Initialize the driver
  99. $this->driver = new $driver($this->config['params']);
  100.  
  101. // Validate the driver
  102. if ( ! ($this->driver instanceof CacheDriver))
  103. throw new Kohana_Exception('core.driver_implements', $this->config['driver'], get_class($this), 'CacheDriver');
  104.  
  105. if (Cache::$loaded !== TRUE)
  106. {
  107. $this->config['requests'] = (int) $this->config['requests'];
  108.  
  109. if ($this->config['requests'] > 0 AND mt_rand(1, $this->config['requests']) === 1)
  110. {
  111. // Do garbage collection
  112. $this->driver->deleteExpired();
  113. }
  114.  
  115. // Cache has been loaded once
  116. Cache::$loaded = TRUE;
  117. }
  118. }
  119.  
  120. /**
  121.   * Fetches a cache by id. NULL is returned when a cache item is not found.
  122.   *
  123.   * @param string cache id
  124.   * @return mixed cached data or NULL
  125.   */
  126. public function get($id)
  127. {
  128. // Sanitize the ID
  129. $id = $this->sanitizeId($id);
  130.  
  131. return $this->driver->get($id);
  132. }
  133.  
  134. /**
  135.   * Fetches all of the caches for a given tag. An empty array will be
  136.   * returned when no matching caches are found.
  137.   *
  138.   * @param string cache tag
  139.   * @return array all cache items matching the tag
  140.   */
  141. public function find($tag)
  142. {
  143. return $this->driver->find($tag);
  144. }
  145.  
  146. /**
  147.   * Set a cache item by id. Tags may also be added and a custom lifetime
  148.   * can be set. Non-string data is automatically serialized.
  149.   *
  150.   * @param string unique cache id
  151.   * @param mixed data to cache
  152.   * @param array|string tags for this item
  153.   * @param integer number of seconds until the cache expires
  154.   * @return boolean
  155.   */
  156. function set($id, $data, $tags = NULL, $lifetime = NULL)
  157. {
  158. if (is_resource($data))
  159. throw new Kohana_Exception('cache.resources');
  160.  
  161. // Sanitize the ID
  162. $id = $this->sanitizeId($id);
  163.  
  164. if ($lifetime === NULL)
  165. {
  166. // Get the default lifetime
  167. $lifetime = $this->config['lifetime'];
  168. }
  169.  
  170. return $this->driver->set($id, $data, (array) $tags, $lifetime);
  171. }
  172.  
  173. /**
  174.   * Delete a cache item by id.
  175.   *
  176.   * @param string cache id
  177.   * @return boolean
  178.   */
  179. public function delete($id)
  180. {
  181. // Sanitize the ID
  182. $id = $this->sanitizeId($id);
  183.  
  184. return $this->driver->delete($id);
  185. }
  186.  
  187. /**
  188.   * Delete all cache items with a given tag.
  189.   *
  190.   * @param string cache tag name
  191.   * @return boolean
  192.   */
  193. public function deleteTag($tag)
  194. {
  195. return $this->driver->delete($tag, TRUE);
  196. }
  197.  
  198. /**
  199.   * Delete ALL cache items items.
  200.   *
  201.   * @return boolean
  202.   */
  203. public function deleteAll()
  204. {
  205. return $this->driver->delete(TRUE);
  206. }
  207.  
  208. /**
  209.   * Replaces troublesome characters with underscores.
  210.   *
  211.   * @param string cache id
  212.   * @return string
  213.   */
  214. protected function sanitizeId($id)
  215. {
  216. // Change slashes and spaces to underscores
  217. return str_replace(array('/', '\\', ' '), '_', $id);
  218. }
  219.  
  220. } // End Cache
  221.