Projet

Général

Profil

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

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

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

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

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

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

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

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

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

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

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

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

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

    
146
  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(
147
    ':session_id' => $sid,
148
    ':obj' => $obj,
149
    ':name' => md5($name),
150
  ))->fetchObject();
151
}
152

    
153
/**
154
 * Get the cache status of a group of objects.
155
 *
156
 * This is useful for displaying lock status when listing a number of objects
157
 * an an administration UI.
158
 *
159
 * @param $obj
160
 *   A 128 character or less string to define what kind of object is being
161
 *   stored; primarily this is used to prevent collisions.
162
 * @param $names
163
 *   An array of names of objects
164
 *
165
 * @return
166
 *   An array of objects containing the UID and updated date for each name found.
167
 */
168
function ctools_object_cache_test_objects($obj, $names) {
169
  array_walk($names, 'md5');
170
  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))
171
    ->fetchAllAssoc('name');
172
}
173

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

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