cache.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @file
  4. * Plugins to handle cache-indirection.
  5. *
  6. * Simple plugin management to allow clients to more tightly control where
  7. * object caches are stored.
  8. *
  9. * CTools provides an object cache mechanism, and it also provides a number
  10. * of subsystems that are designed to plug into larger systems. When doing
  11. * caching on multi-step forms (in particular during AJAX operations) these
  12. * subsystems often need to operate their own cache. In reality, its best
  13. * for everyone if they are able to piggyback off of the larger cache.
  14. *
  15. * This system allows this by registering plugins to control where caches
  16. * are actually stored. For the most part, the subsystems could care less
  17. * where the data is fetched from and stored to. All it needs to know is
  18. * that it can 'get', 'set' and 'clear' caches. Additionally, some caches
  19. * might need extra operations such as 'lock' and 'finalize', and other
  20. * operations may be needed based upon the specific uses for the cache
  21. * plugins.
  22. *
  23. * To utilize cache plugins, there are two pieces of data. First, there is
  24. * the mechanism, which is simply the name of the plugin to use. CTools
  25. * provides a 'simple' mechanism which goes straight through to the object
  26. * cache. The second piece of data is the 'key' which is a unique identifier
  27. * that can be used to find the data needed. Keys can be generated any number
  28. * of ways, and the plugin must be agnostic about the key itself.
  29. *
  30. * That said, the 'mechanism' can be specified as pluginame::data and that
  31. * data can be used to derive additional data. For example, it is often
  32. * desirable to NOT store any cached data until original data (i.e, user
  33. * input) has been received. The data can be used to derive this original
  34. * data so that when a 'get' is called, if the cache is missed it can create
  35. * the data needed. This can help prevent unwanted cache entries from
  36. * building up just by visiting edit UIs without actually modifying anything.
  37. *
  38. * Modules wishing to implement cache indirection mechanisms need to implement
  39. * a plugin of type 'cache' for the module 'ctools' and provide the .inc file.
  40. * It should provide callbacks for 'cache set', 'cache get', and 'cache clear'.
  41. * It can provide callbacks for 'break' and 'finalize' if these are relevant
  42. * to the caching mechanism (i.e, for use with locking caches such as the page
  43. * manager cache). Other operations may be utilized but at this time are not part
  44. * of CTools.
  45. */
  46. /**
  47. * Fetch data from an indirect cache.
  48. *
  49. * @param string $mechanism
  50. * A string containing the plugin name, and an optional data element to
  51. * send to the plugin separated by two colons.
  52. *
  53. * @param string $key
  54. * The key used to identify the cache.
  55. *
  56. * @return mixed
  57. * The cached data. This can be any format as the plugin does not necessarily
  58. * have knowledge of what is being cached.
  59. */
  60. function ctools_cache_get($mechanism, $key) {
  61. return ctools_cache_operation($mechanism, $key, 'get');
  62. }
  63. /**
  64. * Store data in an indirect cache.
  65. *
  66. * @param string $mechanism
  67. * A string containing the plugin name, and an optional data element to
  68. * send to the plugin separated by two colons.
  69. *
  70. * @param string $key
  71. * The key used to identify the cache.
  72. *
  73. * @param mixed $object
  74. * The data to cache. This can be any format as the plugin does not
  75. * necessarily have knowledge of what is being cached.
  76. */
  77. function ctools_cache_set($mechanism, $key, $object) {
  78. return ctools_cache_operation($mechanism, $key, 'set', $object);
  79. }
  80. /**
  81. * Clear data from an indirect cache.
  82. *
  83. * @param string $mechanism
  84. * A string containing the plugin name, and an optional data element to
  85. * send to the plugin separated by two colons.
  86. *
  87. * @param string $key
  88. * The key used to identify the cache.
  89. */
  90. function ctools_cache_clear($mechanism, $key) {
  91. return ctools_cache_operation($mechanism, $key, 'clear');
  92. }
  93. /**
  94. * Perform a secondary operation on an indirect cache.
  95. *
  96. * Additional operations, beyond get, set and clear may be items
  97. * such as 'break' and 'finalize', which are needed to support cache
  98. * locking. Other operations may be added by users of the indirect
  99. * caching functions as needed.
  100. *
  101. * @param string $mechanism
  102. * A string containing the plugin name, and an optional data element to
  103. * send to the plugin separated by two colons.
  104. *
  105. * @param string $key
  106. * The key used to identify the cache.
  107. *
  108. * @param string $op
  109. * The operation to call, such as 'break' or 'finalize'.
  110. *
  111. * @param mixed $object
  112. * The cache data being operated on, in case it is necessary. This is
  113. * optional so no references should be used.
  114. *
  115. * @return mixed
  116. * The operation may or may not return a value.
  117. */
  118. function ctools_cache_operation($mechanism, $key, $op, $object = NULL) {
  119. list($plugin, $data) = ctools_cache_find_plugin($mechanism);
  120. if (empty($plugin)) {
  121. return;
  122. }
  123. $function = ctools_plugin_get_function($plugin, "cache $op");
  124. if (empty($function)) {
  125. return;
  126. }
  127. return $function($data, $key, $object, $op);
  128. }
  129. /**
  130. * Take a mechanism and return a plugin and data.
  131. *
  132. * @param string $mechanism
  133. * A string containing the plugin name, and an optional data element to
  134. * send to the plugin separated by two colons.
  135. *
  136. * @return array
  137. * An array, the first element will be the plugin and the second element
  138. * will be the data. If the plugin could not be found, the $plugin will
  139. * be NULL.
  140. */
  141. function ctools_cache_find_plugin($mechanism) {
  142. if (strpos($mechanism, '::') !== FALSE) {
  143. // Use explode(2) to ensure that the data can contain double
  144. // colons, just in case.
  145. list($name, $data) = explode('::', $mechanism, 2);
  146. }
  147. else {
  148. $name = $mechanism;
  149. $data = '';
  150. }
  151. if (empty($name)) {
  152. return array(NULL, $data);
  153. }
  154. ctools_include('plugins');
  155. $plugin = ctools_get_plugins('ctools', 'cache', $name);
  156. return array($plugin, $data);
  157. }