Projet

Général

Profil

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

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

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
 * @param string $key
55
 *   The key used to identify the cache.
56
 *
57
 * @return mixed
58
 *   The cached data. This can be any format as the plugin does not necessarily
59
 *   have knowledge of what is being cached.
60
 */
61
function ctools_cache_get($mechanism, $key) {
62
  return ctools_cache_operation($mechanism, $key, 'get');
63
}
64

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

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

    
94
}
95

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

    
124
  $function = ctools_plugin_get_function($plugin, "cache $op");
125
  if (empty($function)) {
126
    return;
127
  }
128

    
129
  return $function($data, $key, $object, $op);
130
}
131

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

    
155
  if (empty($name)) {
156
    return array(NULL, $data);
157
  }
158

    
159
  ctools_include('plugins');
160
  $plugin = ctools_get_plugins('ctools', 'cache', $name);
161
  return array($plugin, $data);
162
}