Projet

Général

Profil

Paste
Télécharger (4,22 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / ctools / help / object-cache.html @ a5572547

1
<p>The CTools Object Cache is a specialized cache for storing data that is non-volatile. This differs from the standard Drupal cache mechanism, which is volatile, meaning that the data can be cleared at any time and it is expected that any cached data can easily be reconstructed. In contrast, data stored in this cache is not expected to be reconstructable. It is primarily used for storing user input which is retrieved in stages, allowing for more complex user interface interactions.</p>
2

    
3
<p>The object cache consists of 3 normal functions for cache maintenance, and 2 additional functions to facilitate locking.</p>
4

    
5
<p>To use any of these functions, you must first use ctools_include:</p>
6

    
7
<pre>
8
ctools_include('object-cache');
9
</pre>
10

    
11
<pre>
12
/**
13
 * Get an object from the non-volatile ctools cache.
14
 *
15
 * This function caches in memory as well, so that multiple calls to this
16
 * will not result in multiple database reads.
17
 *
18
 * @param $obj
19
 *   A 128 character or less string to define what kind of object is being
20
 *   stored; primarily this is used to prevent collisions.
21
 * @param $name
22
 *   The name of the object being stored.
23
 * @param $skip_cache
24
 *   Skip the memory cache, meaning this must be read from the db again.
25
 *
26
 * @return
27
 *   The data that was cached.
28
 */
29
function ctools_object_cache_get($obj, $name, $skip_cache = FALSE) {
30
</pre>
31

    
32
<pre>
33
/**
34
 * Store an object in the non-volatile ctools cache.
35
 *
36
 * @param $obj
37
 *   A 128 character or less string to define what kind of object is being
38
 *   stored; primarily this is used to prevent collisions.
39
 * @param $name
40
 *   The name of the object being stored.
41
 * @param $cache
42
 *   The object to be cached. This will be serialized prior to writing.
43
 */
44
function ctools_object_cache_set($obj, $name, $cache) {
45
</pre>
46

    
47
<pre>
48
/**
49
 * Remove an object from the non-volatile ctools cache
50
 *
51
 * @param $obj
52
 *   A 128 character or less string to define what kind of object is being
53
 *   stored; primarily this is used to prevent collisions.
54
 * @param $name
55
 *   The name of the object being removed.
56
 */
57
function ctools_object_cache_clear($obj, $name) {
58
</pre>
59

    
60
<p>To facilitate locking, which is the ability to prohibit updates by other users while one user has an object cached, this system provides two functions:</p>
61

    
62
<pre>
63
/**
64
 * Determine if another user has a given object cached.
65
 *
66
 * This is very useful for 'locking' objects so that only one user can
67
 * modify them.
68
 *
69
 * @param $obj
70
 *   A 128 character or less string to define what kind of object is being
71
 *   stored; primarily this is used to prevent collisions.
72
 * @param $name
73
 *   The name of the object being removed.
74
 *
75
 * @return
76
 *   An object containing the UID and updated date if found; NULL if not.
77
 */
78
function ctools_object_cache_test($obj, $name) {
79
</pre>
80

    
81
<p>The object returned by ctools_object_cache_test can be directly used to determine whether a user should be allowed to cache their own version of an object.</p>
82

    
83
<p>To allow the concept of breaking a lock, that is, clearing another users changes:</p>
84

    
85
<pre>
86
/**
87
 * Remove an object from the non-volatile ctools cache for all session IDs.
88
 *
89
 * This is useful for clearing a lock.
90
 *
91
 * @param $obj
92
 *   A 128 character or less string to define what kind of object is being
93
 *   stored; primarily this is used to prevent collisions.
94
 * @param $name
95
 *   The name of the object being removed.
96
 */
97
function ctools_object_cache_clear_all($obj, $name) {
98
</pre>
99

    
100
<p>Typical best practice is to use wrapper functions such as these:</p>
101

    
102
<pre>
103
/**
104
 * Get the cached changes to a given task handler.
105
 */
106
function delegator_page_get_page_cache($name) {
107
  ctools_include('object-cache');
108
  $cache = ctools_object_cache_get('delegator_page', $name);
109
  if (!$cache) {
110
    $cache = delegator_page_load($name);
111
    $cache->locked = ctools_object_cache_test('delegator_page', $name);
112
  }
113

    
114
  return $cache;
115
}
116

    
117
/**
118
 * Store changes to a task handler in the object cache.
119
 */
120
function delegator_page_set_page_cache($name, $page) {
121
  ctools_include('object-cache');
122
  $cache = ctools_object_cache_set('delegator_page', $name, $page);
123
}
124

    
125
/**
126
 * Remove an item from the object cache.
127
 */
128
function delegator_page_clear_page_cache($name) {
129
  ctools_include('object-cache');
130
  ctools_object_cache_clear('delegator_page', $name);
131
}
132
</pre>