Projet

Général

Profil

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

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

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(':session_id' => $sid, ':object' => $obj, ':name' => $name))
47
      ->fetchObject();
48
    if ($data) {
49
      $cache[$key] = unserialize($data->data);
50
    }
51
  }
52
  return isset($cache[$key]) ? $cache[$key] : NULL;
53
}
54

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

    
77
  ctools_object_cache_clear($obj, $name, $sid);
78

    
79
  if (!$sid) {
80
    $sid = session_id();
81
  }
82

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

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

    
108
  if (!$sid) {
109
    $sid = session_id();
110
  }
111

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

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

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

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

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

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

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