Projet

Général

Profil

Paste
Télécharger (5,79 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / includes / cache.inc @ 7e72b748

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugins to handle cache-indirection.
6
 *
7
 * Simple plugin management to allow clients to more tightly control where
8
 * object caches are stored.
9
 *
10
 * CTools provides an object cache mechanism, and it also provides a number
11
 * of subsystems that are designed to plug into larger systems. When doing
12
 * caching on multi-step forms (in particular during AJAX operations) these
13
 * subsystems often need to operate their own cache. In reality, its best
14
 * for everyone if they are able to piggyback off of the larger cache.
15
 *
16
 * This system allows this by registering plugins to control where caches
17
 * are actually stored. For the most part, the subsystems could care less
18
 * where the data is fetched from and stored to. All it needs to know is
19
 * that it can 'get', 'set' and 'clear' caches. Additionally, some caches
20
 * might need extra operations such as 'lock' and 'finalize', and other
21
 * operations may be needed based upon the specific uses for the cache
22
 * plugins.
23
 *
24
 * To utilize cache plugins, there are two pieces of data. First, there is
25
 * the mechanism, which is simply the name of the plugin to use. CTools
26
 * provides a 'simple' mechanism which goes straight through to the object
27
 * cache. The second piece of data is the 'key' which is a unique identifier
28
 * that can be used to find the data needed. Keys can be generated any number
29
 * of ways, and the plugin must be agnostic about the key itself.
30
 *
31
 * That said, the 'mechanism' can be specified as pluginame::data and that
32
 * data can be used to derive additional data. For example, it is often
33
 * desirable to NOT store any cached data until original data (i.e, user
34
 * input) has been received. The data can be used to derive this original
35
 * data so that when a 'get' is called, if the cache is missed it can create
36
 * the data needed. This can help prevent unwanted cache entries from
37
 * building up just by visiting edit UIs without actually modifying anything.
38
 *
39
 * Modules wishing to implement cache indirection mechanisms need to implement
40
 * a plugin of type 'cache' for the module 'ctools' and provide the .inc file.
41
 * It should provide callbacks for 'cache set', 'cache get', and 'cache clear'.
42
 * It can provide callbacks for 'break' and 'finalize' if these are relevant
43
 * to the caching mechanism (i.e, for use with locking caches such as the page
44
 * manager cache). Other operations may be utilized but at this time are not part
45
 * of CTools.
46
 */
47

    
48
/**
49
 * Fetch data from an indirect cache.
50
 *
51
 * @param string $mechanism
52
 *   A string containing the plugin name, and an optional data element to
53
 *   send to the plugin separated by two colons.
54
 *
55
 * @param string $key
56
 *   The key used to identify the cache.
57
 *
58
 * @return mixed
59
 *   The cached data. This can be any format as the plugin does not necessarily
60
 *   have knowledge of what is being cached.
61
 */
62
function ctools_cache_get($mechanism, $key) {
63
  return ctools_cache_operation($mechanism, $key, 'get');
64
}
65

    
66
/**
67
 * Store data in an indirect cache.
68
 *
69
 * @param string $mechanism
70
 *   A string containing the plugin name, and an optional data element to
71
 *   send to the plugin separated by two colons.
72
 *
73
 * @param string $key
74
 *   The key used to identify the cache.
75
 *
76
 * @param mixed $object
77
 *   The data to cache. This can be any format as the plugin does not
78
 *   necessarily have knowledge of what is being cached.
79
 */
80
function ctools_cache_set($mechanism, $key, $object) {
81
  return ctools_cache_operation($mechanism, $key, 'set', $object);
82
}
83

    
84
/**
85
 * Clear data from an indirect cache.
86
 *
87
 * @param string $mechanism
88
 *   A string containing the plugin name, and an optional data element to
89
 *   send to the plugin separated by two colons.
90
 *
91
 * @param string $key
92
 *   The key used to identify the cache.
93
 */
94
function ctools_cache_clear($mechanism, $key) {
95
  return ctools_cache_operation($mechanism, $key, 'clear');
96

    
97
}
98

    
99
/**
100
 * Perform a secondary operation on an indirect cache.
101
 *
102
 * Additional operations, beyond get, set and clear may be items
103
 * such as 'break' and 'finalize', which are needed to support cache
104
 * locking. Other operations may be added by users of the indirect
105
 * caching functions as needed.
106
 *
107
 * @param string $mechanism
108
 *   A string containing the plugin name, and an optional data element to
109
 *   send to the plugin separated by two colons.
110
 *
111
 * @param string $key
112
 *   The key used to identify the cache.
113
 *
114
 * @param string $op
115
 *   The operation to call, such as 'break' or 'finalize'.
116
 *
117
 * @param mixed $object
118
 *   The cache data being operated on, in case it is necessary. This is
119
 *   optional so no references should be used.
120
 *
121
 * @return mixed
122
 *   The operation may or may not return a value.
123
 */
124
function ctools_cache_operation($mechanism, $key, $op, $object = NULL) {
125
  list($plugin, $data) = ctools_cache_find_plugin($mechanism);
126
  if (empty($plugin)) {
127
    return;
128
  }
129

    
130
  $function = ctools_plugin_get_function($plugin, "cache $op");
131
  if (empty($function)) {
132
    return;
133
  }
134

    
135
  return $function($data, $key, $object, $op);
136
}
137

    
138
/**
139
 * Take a mechanism and return a plugin and data.
140
 *
141
 * @param string $mechanism
142
 *   A string containing the plugin name, and an optional data element to
143
 *   send to the plugin separated by two colons.
144
 *
145
 * @return array
146
 *   An array, the first element will be the plugin and the second element
147
 *   will be the data. If the plugin could not be found, the $plugin will
148
 *   be NULL.
149
 */
150
function ctools_cache_find_plugin($mechanism) {
151
  if (strpos($mechanism, '::') !== FALSE) {
152
    // Use explode(2) to ensure that the data can contain double
153
    // colons, just in case.
154
    list($name, $data) = explode('::', $mechanism, 2);
155
  }
156
  else {
157
    $name = $mechanism;
158
    $data = '';
159
  }
160

    
161
  if (empty($name)) {
162
    return array(NULL, $data);
163
  }
164

    
165
  ctools_include('plugins');
166
  $plugin = ctools_get_plugins('ctools', 'cache', $name);
167
  return array($plugin, $data);
168
}