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.
13 * Get an object from the non-volatile ctools cache.
15 * This function caches in memory as well, so that multiple calls to this
16 * will not result in multiple database reads.
19 * A 128 character or less string to define what kind of object is being
20 * stored; primarily this is used to prevent collisions.
22 * The name of the object being stored.
24 * Skip the memory cache, meaning this must be read from the db again.
26 * The session id, allowing someone to use Session API or their own solution;
27 * defaults to session_id().
29 * @deprecated $skip_cache is deprecated in favor of drupal_static*
31 * The data that was cached.
33 function ctools_object_cache_get($obj, $name, $skip_cache = FALSE, $sid = NULL) {
34 $cache = &drupal_static(__FUNCTION__, array());
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(':session_id' => $sid, ':object' => $obj, ':name' => $name))
48 $cache[$key] = unserialize($data->data);
51 return isset($cache[$key]) ? $cache[$key] : NULL;
55 * Store an object in the non-volatile ctools cache.
58 * A 128 character or less string to define what kind of object is being
59 * stored; primarily this is used to prevent collisions.
61 * The name of the object being stored.
63 * The object to be cached. This will be serialized prior to writing.
65 * The session id, allowing someone to use Session API or their own solution;
66 * defaults to session_id().
68 function ctools_object_cache_set($obj, $name, $cache, $sid = NULL) {
69 // Store the CTools session id in the user session to force a
70 // session for anonymous users in Drupal 7 and Drupal 6 Pressflow.
71 // see http://drupal.org/node/562374, http://drupal.org/node/861778
72 if (empty($GLOBALS['user']->uid) && empty($_SESSION['ctools_session_id'])) {
73 $_SESSION['ctools_hold_session'] = TRUE;
76 ctools_object_cache_clear($obj, $name, $sid);
82 db_insert('ctools_object_cache')
87 'data' => serialize($cache),
88 'updated' => REQUEST_TIME,
94 * Remove an object from the non-volatile ctools cache
97 * A 128 character or less string to define what kind of object is being
98 * stored; primarily this is used to prevent collisions.
100 * The name of the object being removed.
102 * The session id, allowing someone to use Session API or their own solution;
103 * defaults to session_id().
105 function ctools_object_cache_clear($obj, $name, $sid = NULL) {
111 db_delete('ctools_object_cache')
112 ->condition('sid', $sid)
113 ->condition('obj', $obj)
114 ->condition('name', $name)
116 // Ensure the static cache is emptied of this obj:name set.
117 drupal_static_reset('ctools_object_cache_get');
122 * Determine if another user has a given object cached.
124 * This is very useful for 'locking' objects so that only one user can
128 * A 128 character or less string to define what kind of object is being
129 * stored; primarily this is used to prevent collisions.
131 * The name of the object being removed.
133 * The session id, allowing someone to use Session API or their own solution;
134 * defaults to session_id().
137 * An object containing the UID and updated date if found; NULL if not.
139 function ctools_object_cache_test($obj, $name, $sid = NULL) {
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))
150 * Get the cache status of a group of objects.
152 * This is useful for displaying lock status when listing a number of objects
153 * an an administration UI.
156 * A 128 character or less string to define what kind of object is being
157 * stored; primarily this is used to prevent collisions.
159 * An array of names of objects
162 * An array of objects containing the UID and updated date for each name found.
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');
170 * Remove an object from the non-volatile ctools cache for all session IDs.
172 * This is useful for clearing a lock.
175 * A 128 character or less string to define what kind of object is being
176 * stored; primarily this is used to prevent collisions.
178 * The name of the object being removed.
180 function ctools_object_cache_clear_all($obj, $name) {
181 db_delete('ctools_object_cache')
182 ->condition('obj', $obj)
183 ->condition('name', $name)
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"]);
191 * Remove all objects in the object cache that are older than the
195 * The minimum age of objects to remove, in seconds. For example, 86400 is
196 * one day. Defaults to 7 days.
198 function ctools_object_cache_clean($age = NULL) {
200 $age = 86400 * 7; // 7 days
202 db_delete('ctools_object_cache')
203 ->condition('updated', REQUEST_TIME - $age, '<')