object-cache.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <p>The CTools Object Cache is a specialized cache for storing data that is non-volatile. This differs from the standard Drupal cache mechanism, which is volatile, meaning that the data can be cleared at any time and it is expected that any cached data can easily be reconstructed. In contrast, data stored in this cache is not expected to be reconstructable. It is primarily used for storing user input which is retrieved in stages, allowing for more complex user interface interactions.</p>
  2. <p>The object cache consists of 3 normal functions for cache maintenance, and 2 additional functions to facilitate locking.</p>
  3. <p>To use any of these functions, you must first use ctools_include:</p>
  4. <pre>
  5. ctools_include('object-cache');
  6. </pre>
  7. <pre>
  8. /**
  9. * Get an object from the non-volatile ctools cache.
  10. *
  11. * This function caches in memory as well, so that multiple calls to this
  12. * will not result in multiple database reads.
  13. *
  14. * @param $obj
  15. * A 128 character or less string to define what kind of object is being
  16. * stored; primarily this is used to prevent collisions.
  17. * @param $name
  18. * The name of the object being stored.
  19. * @param $skip_cache
  20. * Skip the memory cache, meaning this must be read from the db again.
  21. *
  22. * @return
  23. * The data that was cached.
  24. */
  25. function ctools_object_cache_get($obj, $name, $skip_cache = FALSE) {
  26. </pre>
  27. <pre>
  28. /**
  29. * Store an object in the non-volatile ctools cache.
  30. *
  31. * @param $obj
  32. * A 128 character or less string to define what kind of object is being
  33. * stored; primarily this is used to prevent collisions.
  34. * @param $name
  35. * The name of the object being stored.
  36. * @param $cache
  37. * The object to be cached. This will be serialized prior to writing.
  38. */
  39. function ctools_object_cache_set($obj, $name, $cache) {
  40. </pre>
  41. <pre>
  42. /**
  43. * Remove an object from the non-volatile ctools cache
  44. *
  45. * @param $obj
  46. * A 128 character or less string to define what kind of object is being
  47. * stored; primarily this is used to prevent collisions.
  48. * @param $name
  49. * The name of the object being removed.
  50. */
  51. function ctools_object_cache_clear($obj, $name) {
  52. </pre>
  53. <p>To facilitate locking, which is the ability to prohibit updates by other users while one user has an object cached, this system provides two functions:</p>
  54. <pre>
  55. /**
  56. * Determine if another user has a given object cached.
  57. *
  58. * This is very useful for 'locking' objects so that only one user can
  59. * modify them.
  60. *
  61. * @param $obj
  62. * A 128 character or less string to define what kind of object is being
  63. * stored; primarily this is used to prevent collisions.
  64. * @param $name
  65. * The name of the object being removed.
  66. *
  67. * @return
  68. * An object containing the UID and updated date if found; NULL if not.
  69. */
  70. function ctools_object_cache_test($obj, $name) {
  71. </pre>
  72. <p>The object returned by ctools_object_cache_test can be directly used to determine whether a user should be allowed to cache their own version of an object.</p>
  73. <p>To allow the concept of breaking a lock, that is, clearing another users changes:</p>
  74. <pre>
  75. /**
  76. * Remove an object from the non-volatile ctools cache for all session IDs.
  77. *
  78. * This is useful for clearing a lock.
  79. *
  80. * @param $obj
  81. * A 128 character or less string to define what kind of object is being
  82. * stored; primarily this is used to prevent collisions.
  83. * @param $name
  84. * The name of the object being removed.
  85. */
  86. function ctools_object_cache_clear_all($obj, $name) {
  87. </pre>
  88. <p>Typical best practice is to use wrapper functions such as these:</p>
  89. <pre>
  90. /**
  91. * Get the cached changes to a given task handler.
  92. */
  93. function delegator_page_get_page_cache($name) {
  94. ctools_include('object-cache');
  95. $cache = ctools_object_cache_get('delegator_page', $name);
  96. if (!$cache) {
  97. $cache = delegator_page_load($name);
  98. $cache->locked = ctools_object_cache_test('delegator_page', $name);
  99. }
  100. return $cache;
  101. }
  102. /**
  103. * Store changes to a task handler in the object cache.
  104. */
  105. function delegator_page_set_page_cache($name, $page) {
  106. ctools_include('object-cache');
  107. $cache = ctools_object_cache_set('delegator_page', $name, $page);
  108. }
  109. /**
  110. * Remove an item from the object cache.
  111. */
  112. function delegator_page_clear_page_cache($name) {
  113. ctools_include('object-cache');
  114. ctools_object_cache_clear('delegator_page', $name);
  115. }
  116. </pre>