Projet

Général

Profil

Paste
Télécharger (6,48 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * The non-volatile object cache is used to store an object while it is
6
 * being edited, so that we don't have to save until we're completely
7
 * done. The cache should be 'cleaned' on a regular basis, meaning to
8
 * remove old objects from the cache, but otherwise the data in this
9
 * cache must remain stable, as it includes unsaved changes.
10
 */
11

    
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
 * @param $sid
26
 *   The session id, allowing someone to use Session API or their own solution;
27
 *   defaults to session_id().
28
 *
29
 * @deprecated $skip_cache is deprecated in favor of drupal_static*
30
 *
31
 * @return
32
 *   The data that was cached.
33
 */
34
function ctools_object_cache_get($obj, $name, $skip_cache = FALSE, $sid = NULL) {
35
  $cache = &drupal_static(__FUNCTION__, array());
36
  $key = "$obj:$name";
37
  if ($skip_cache) {
38
    unset($cache[$key]);
39
  }
40

    
41
  if (!$sid) {
42
    $sid = session_id();
43
  }
44

    
45
  if (!array_key_exists($key, $cache)) {
46
    $data = db_query('SELECT * FROM {ctools_object_cache} WHERE sid = :session_id AND obj = :object AND name = :name', array(
47
      ':session_id' => $sid,
48
      ':object' => $obj,
49
      ':name' => md5($name),
50
    ))->fetchObject();
51
    if ($data) {
52
      $cache[$key] = unserialize($data->data);
53
    }
54
  }
55
  return isset($cache[$key]) ? $cache[$key] : NULL;
56
}
57

    
58
/**
59
 * Store an object in the non-volatile ctools cache.
60
 *
61
 * @param $obj
62
 *   A 128 character or less string to define what kind of object is being
63
 *   stored; primarily this is used to prevent collisions.
64
 * @param $name
65
 *   The name of the object being stored.
66
 * @param $cache
67
 *   The object to be cached. This will be serialized prior to writing.
68
 * @param $sid
69
 *   The session id, allowing someone to use Session API or their own solution;
70
 *   defaults to session_id().
71
 */
72
function ctools_object_cache_set($obj, $name, $cache, $sid = NULL) {
73
  // Store the CTools session id in the user session to force a
74
  // session for anonymous users in Drupal 7 and Drupal 6 Pressflow.
75
  // see http://drupal.org/node/562374, http://drupal.org/node/861778
76
  if (empty($GLOBALS['user']->uid) && empty($_SESSION['ctools_session_id'])) {
77
    $_SESSION['ctools_hold_session'] = TRUE;
78
  }
79

    
80
  ctools_object_cache_clear($obj, $name, $sid);
81

    
82
  if (!$sid) {
83
    $sid = session_id();
84
  }
85

    
86
  db_insert('ctools_object_cache')
87
    ->fields(array(
88
      'sid' => $sid,
89
      'obj' => $obj,
90
      'name' => md5($name),
91
      'data' => serialize($cache),
92
      'updated' => REQUEST_TIME,
93
    ))
94
    ->execute();
95
}
96

    
97
/**
98
 * Remove an object from the non-volatile ctools cache.
99
 *
100
 * @param $obj
101
 *   A 128 character or less string to define what kind of object is being
102
 *   stored; primarily this is used to prevent collisions.
103
 * @param $name
104
 *   The name of the object being removed.
105
 * @param $sid
106
 *   The session id, allowing someone to use Session API or their own solution;
107
 *   defaults to session_id().
108
 */
109
function ctools_object_cache_clear($obj, $name, $sid = NULL) {
110
  if (!$sid) {
111
    $sid = session_id();
112
  }
113

    
114
  db_delete('ctools_object_cache')
115
    ->condition('sid', $sid)
116
    ->condition('obj', $obj)
117
    ->condition('name', md5($name))
118
    ->execute();
119
  // Ensure the static cache is emptied of this obj:name set.
120
  drupal_static_reset('ctools_object_cache_get');
121
}
122

    
123
/**
124
 * Determine if another user has a given object cached.
125
 *
126
 * This is very useful for 'locking' objects so that only one user can
127
 * modify them.
128
 *
129
 * @param $obj
130
 *   A 128 character or less string to define what kind of object is being
131
 *   stored; primarily this is used to prevent collisions.
132
 * @param $name
133
 *   The name of the object being removed.
134
 * @param $sid
135
 *   The session id, allowing someone to use Session API or their own solution;
136
 *   defaults to session_id().
137
 *
138
 * @return
139
 *   An object containing the UID and updated date if found; NULL if not.
140
 */
141
function ctools_object_cache_test($obj, $name, $sid = NULL) {
142

    
143
  if (!$sid) {
144
    $sid = session_id();
145
  }
146

    
147
  return db_query('SELECT s.uid, c.updated FROM {ctools_object_cache} c INNER JOIN {sessions} s ON c.sid = s.sid WHERE s.sid <> :session_id AND c.obj = :obj AND c.name = :name ORDER BY c.updated ASC', array(
148
    ':session_id' => $sid,
149
    ':obj' => $obj,
150
    ':name' => md5($name),
151
  ))->fetchObject();
152
}
153

    
154
/**
155
 * Get the cache status of a group of objects.
156
 *
157
 * This is useful for displaying lock status when listing a number of objects
158
 * an an administration UI.
159
 *
160
 * @param $obj
161
 *   A 128 character or less string to define what kind of object is being
162
 *   stored; primarily this is used to prevent collisions.
163
 * @param $names
164
 *   An array of names of objects
165
 *
166
 * @return
167
 *   An array of objects containing the UID and updated date for each name found.
168
 */
169
function ctools_object_cache_test_objects($obj, $names) {
170
  array_walk($names, 'md5');
171
  return db_query("SELECT c.name, s.uid, c.updated FROM {ctools_object_cache} c INNER JOIN {sessions} s ON c.sid = s.sid WHERE c.obj = :obj AND c.name IN (:names) ORDER BY c.updated ASC", array(':obj' => $obj, ':names' => $names))
172
    ->fetchAllAssoc('name');
173
}
174

    
175
/**
176
 * Remove an object from the non-volatile ctools cache for all session IDs.
177
 *
178
 * This is useful for clearing a lock.
179
 *
180
 * @param $obj
181
 *   A 128 character or less string to define what kind of object is being
182
 *   stored; primarily this is used to prevent collisions.
183
 * @param $name
184
 *   The name of the object being removed.
185
 */
186
function ctools_object_cache_clear_all($obj, $name) {
187
  db_delete('ctools_object_cache')
188
    ->condition('obj', $obj)
189
    ->condition('name', md5($name))
190
    ->execute();
191
  // Ensure the static cache is emptied of this obj:name set.
192
  $cache = &drupal_static('ctools_object_cache_get', array());
193
  unset($cache["$obj:$name"]);
194
}
195

    
196
/**
197
 * Remove all objects in the object cache that are older than the
198
 * specified age.
199
 *
200
 * @param $age
201
 *   The minimum age of objects to remove, in seconds. For example, 86400 is
202
 *   one day. Defaults to 7 days.
203
 */
204
function ctools_object_cache_clean($age = NULL) {
205
  if (empty($age)) {
206
    // 7 days.
207
    $age = 86400 * 7;
208
  }
209
  db_delete('ctools_object_cache')
210
    ->condition('updated', REQUEST_TIME - $age, '<')
211
    ->execute();
212
}