simple.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * @file
  4. * Provides a simple time-based caching option for panel panes.
  5. */
  6. // Plugin definition
  7. $plugin = array(
  8. 'title' => t("Simple cache"),
  9. 'description' => t('Simple caching is a time-based cache. This is a hard limit, and once cached it will remain that way until the time limit expires.'),
  10. 'cache get' => 'panels_simple_cache_get_cache',
  11. 'cache set' => 'panels_simple_cache_set_cache',
  12. 'cache clear' => 'panels_simple_cache_clear_cache',
  13. 'settings form' => 'panels_simple_cache_settings_form',
  14. 'settings form submit' => 'panels_simple_cache_settings_form_submit',
  15. 'defaults' => array(
  16. 'lifetime' => 15,
  17. 'granularity' => 'none',
  18. ),
  19. );
  20. /**
  21. * Get cached content.
  22. */
  23. function panels_simple_cache_get_cache($conf, $display, $args, $contexts, $pane = NULL) {
  24. $cid = panels_simple_cache_get_id($conf, $display, $args, $contexts, $pane);
  25. $cache = cache_get($cid, 'cache_panels');
  26. if (!$cache) {
  27. return FALSE;
  28. }
  29. if ((time() - $cache->created) > $conf['lifetime']) {
  30. return FALSE;
  31. }
  32. return $cache->data;
  33. }
  34. /**
  35. * Set cached content.
  36. */
  37. function panels_simple_cache_set_cache($conf, $content, $display, $args, $contexts, $pane = NULL) {
  38. $cid = panels_simple_cache_get_id($conf, $display, $args, $contexts, $pane);
  39. cache_set($cid, $content, 'cache_panels');
  40. }
  41. /**
  42. * Clear cached content.
  43. *
  44. * Cache clears are always for an entire display, regardless of arguments.
  45. */
  46. function panels_simple_cache_clear_cache($display) {
  47. $cid = 'panels_simple_cache';
  48. // If the panel is stored in the database it'll have a numeric did value.
  49. if (is_numeric($display->did)) {
  50. $cid .= ':' . $display->did;
  51. }
  52. // Exported panels won't have a numeric did but may have a usable cache_key.
  53. elseif (!empty($display->cache_key)) {
  54. $cid .= ':' . str_replace('panel_context:', '', $display->cache_key);
  55. }
  56. // Alternatively use the css_id.
  57. elseif (!empty($display->css_id)) {
  58. $cid .= ':' . $display->css_id;
  59. }
  60. // Failover to just appending the did, which may be the completely unusable
  61. // string 'new'.
  62. else {
  63. $cid .= ':' . $display->did;
  64. }
  65. cache_clear_all($cid, 'cache_panels', TRUE);
  66. }
  67. /**
  68. * Figure out an id for our cache based upon input and settings.
  69. */
  70. function panels_simple_cache_get_id($conf, $display, $args, $contexts, $pane) {
  71. $id = 'panels_simple_cache';
  72. // If the panel is stored in the database it'll have a numeric did value.
  73. if (is_numeric($display->did)) {
  74. $id .= ':' . $display->did;
  75. }
  76. // Exported panels won't have a numeric did but may have a usable cache_key.
  77. elseif (!empty($display->cache_key)) {
  78. $id .= ':' . str_replace('panel_context:', '', $display->cache_key);
  79. }
  80. // Alternatively use the css_id.
  81. elseif (!empty($display->css_id)) {
  82. $id .= ':' . $display->css_id;
  83. }
  84. // Failover to just appending the did, which may be the completely unusable
  85. // string 'new'.
  86. else {
  87. $id .= ':' . $display->did;
  88. }
  89. if ($pane) {
  90. $id .= ':' . $pane->pid;
  91. }
  92. if (user_access('view pane admin links')) {
  93. $id .= ':admin';
  94. }
  95. switch ($conf['granularity']) {
  96. case 'args':
  97. foreach ($args as $arg) {
  98. $id .= ':' . $arg;
  99. }
  100. break;
  101. case 'context':
  102. if (!is_array($contexts)) {
  103. $contexts = array($contexts);
  104. }
  105. foreach ($contexts as $context) {
  106. if (isset($context->argument)) {
  107. $id .= ':' . $context->argument;
  108. }
  109. }
  110. }
  111. if (module_exists('locale')) {
  112. global $language;
  113. $id .= ':' . $language->language;
  114. }
  115. if(!empty($pane->configuration['use_pager']) && !empty($_GET['page'])) {
  116. $id .= ':p' . check_plain($_GET['page']);
  117. }
  118. return $id;
  119. }
  120. function panels_simple_cache_settings_form($conf, $display, $pid) {
  121. $options = drupal_map_assoc(array(15, 30, 60, 120, 180, 240, 300, 600, 900, 1200, 1800, 3600, 7200, 14400, 28800, 43200, 86400, 172800, 259200, 345600, 604800), 'format_interval');
  122. $form['lifetime'] = array(
  123. '#title' => t('Lifetime'),
  124. '#type' => 'select',
  125. '#options' => $options,
  126. '#default_value' => $conf['lifetime'],
  127. );
  128. $form['granularity'] = array(
  129. '#title' => t('Granularity'),
  130. '#type' => 'select',
  131. '#options' => array(
  132. 'args' => t('Arguments'),
  133. 'context' => t('Context'),
  134. 'none' => t('None'),
  135. ),
  136. '#description' => t('If "arguments" are selected, this content will be cached per individual argument to the entire display; if "contexts" are selected, this content will be cached per unique context in the pane or display; if "neither" there will be only one cache for this pane.'),
  137. '#default_value' => $conf['granularity'],
  138. );
  139. return $form;
  140. }