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 @ 3aa14731

1
<?php
2

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

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

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

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

    
98
}
99

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

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

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

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

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

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