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