cache.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. * @param string $key
  53. * The key used to identify the cache.
  54. *
  55. * @return mixed
  56. * The cached data. This can be any format as the plugin does not necessarily
  57. * have knowledge of what is being cached.
  58. */
  59. function ctools_cache_get($mechanism, $key) {
  60. return ctools_cache_operation($mechanism, $key, 'get');
  61. }
  62. /**
  63. * Store data in an indirect cache.
  64. *
  65. * @param string $mechanism
  66. * A string containing the plugin name, and an optional data element to
  67. * send to the plugin separated by two colons.
  68. * @param string $key
  69. * The key used to identify the cache.
  70. *
  71. * @param mixed $object
  72. * The data to cache. This can be any format as the plugin does not
  73. * necessarily have knowledge of what is being cached.
  74. */
  75. function ctools_cache_set($mechanism, $key, $object) {
  76. return ctools_cache_operation($mechanism, $key, 'set', $object);
  77. }
  78. /**
  79. * Clear data from an indirect cache.
  80. *
  81. * @param string $mechanism
  82. * A string containing the plugin name, and an optional data element to
  83. * send to the plugin separated by two colons.
  84. * @param string $key
  85. * The key used to identify the cache.
  86. */
  87. function ctools_cache_clear($mechanism, $key) {
  88. return ctools_cache_operation($mechanism, $key, 'clear');
  89. }
  90. /**
  91. * Perform a secondary operation on an indirect cache.
  92. *
  93. * Additional operations, beyond get, set and clear may be items
  94. * such as 'break' and 'finalize', which are needed to support cache
  95. * locking. Other operations may be added by users of the indirect
  96. * caching functions as needed.
  97. *
  98. * @param string $mechanism
  99. * A string containing the plugin name, and an optional data element to
  100. * send to the plugin separated by two colons.
  101. * @param string $key
  102. * The key used to identify the cache.
  103. * @param string $op
  104. * The operation to call, such as 'break' or 'finalize'.
  105. * @param mixed $object
  106. * The cache data being operated on, in case it is necessary. This is
  107. * optional so no references should be used.
  108. *
  109. * @return mixed
  110. * The operation may or may not return a value.
  111. */
  112. function ctools_cache_operation($mechanism, $key, $op, $object = NULL) {
  113. list($plugin, $data) = ctools_cache_find_plugin($mechanism);
  114. if (empty($plugin)) {
  115. return;
  116. }
  117. $function = ctools_plugin_get_function($plugin, "cache $op");
  118. if (empty($function)) {
  119. return;
  120. }
  121. return $function($data, $key, $object, $op);
  122. }
  123. /**
  124. * Take a mechanism and return a plugin and data.
  125. *
  126. * @param string $mechanism
  127. * A string containing the plugin name, and an optional data element to
  128. * send to the plugin separated by two colons.
  129. *
  130. * @return array
  131. * An array, the first element will be the plugin and the second element
  132. * will be the data. If the plugin could not be found, the $plugin will
  133. * be NULL.
  134. */
  135. function ctools_cache_find_plugin($mechanism) {
  136. if (strpos($mechanism, '::') !== FALSE) {
  137. // Use explode(2) to ensure that the data can contain double
  138. // colons, just in case.
  139. list($name, $data) = explode('::', $mechanism, 2);
  140. }
  141. else {
  142. $name = $mechanism;
  143. $data = '';
  144. }
  145. if (empty($name)) {
  146. return array(NULL, $data);
  147. }
  148. ctools_include('plugins');
  149. $plugin = ctools_get_plugins('ctools', 'cache', $name);
  150. return array($plugin, $data);
  151. }