- <?php
-
- * Defines a default cache implementation.
- *
- * This is Backdrop's default cache implementation. It uses the database to store
- * cached data. Each cache bin corresponds to a database table by the same name.
- */
- class BackdropDatabaseCache implements BackdropCacheInterface {
- protected $bin;
-
-
- * Constructs a new BackdropDatabaseCache object.
- */
- function __construct(string $bin) {
-
-
- if ($bin != 'cache') {
- $bin = 'cache_' . $bin;
- }
- $this->bin = $bin;
-
-
- if (!function_exists('db_query') || backdrop_get_bootstrap_phase() < BACKDROP_BOOTSTRAP_DATABASE) {
- backdrop_bootstrap(BACKDROP_BOOTSTRAP_DATABASE, FALSE);
- }
- }
-
-
- * Implements BackdropCacheInterface::get().
- */
- function get(string $cid) {
- $cids = array($cid);
- $cache = $this->getMultiple($cids);
- return reset($cache);
- }
-
-
- * Implements BackdropCacheInterface::getMultiple().
- */
- function getMultiple(array &$cids) {
- try {
-
-
-
-
-
-
-
- $result = db_query('SELECT cid, data, created, expire, serialized FROM {' . db_escape_table($this->bin) . '} WHERE cid IN (:cids)', array(':cids' => $cids));
- $cache = array();
- foreach ($result as $item) {
- $item = $this->prepareItem($item);
- if ($item) {
- $cache[$item->cid] = $item;
- }
- }
- $cids = array_diff($cids, array_keys($cache));
- return $cache;
- }
- catch (Exception $e) {
-
-
- return array();
- }
- }
-
-
- * Prepares a cached item.
- *
- * Checks that items are either permanent or did not expire, and unserializes
- * data as appropriate.
- *
- * @param $cache
- * An item loaded from BackdropCacheInterface::get() or BackdropCacheInterface::getMultiple().
- *
- * @return stdClass|FALSE
- * The item with data unserialized as appropriate or FALSE if there is no
- * valid item to load.
- */
- protected function prepareItem($cache) {
- if (!isset($cache->data)) {
- return FALSE;
- }
-
- if ($cache->serialized) {
- $cache->data = unserialize($cache->data);
- }
-
- return $cache;
- }
-
-
- * Implements BackdropCacheInterface::set().
- */
- function set(string $cid, $data, int $expire = CACHE_PERMANENT) {
- $fields = array(
- 'serialized' => 0,
- 'created' => REQUEST_TIME,
- 'expire' => $expire,
- );
- if (!is_string($data)) {
- $fields['data'] = serialize($data);
- $fields['serialized'] = 1;
- }
- else {
- $fields['data'] = $data;
- $fields['serialized'] = 0;
- }
-
- try {
- db_merge($this->bin)
- ->key(array('cid' => $cid))
- ->fields($fields)
- ->execute();
- }
- catch (Exception $e) {
-
- }
- }
-
-
- * Implements BackdropCacheInterface::delete().
- */
- function delete(string $cid) {
- db_delete($this->bin)
- ->condition('cid', $cid)
- ->execute();
- }
-
-
- * Implements BackdropCacheInterface::deleteMultiple().
- */
- function deleteMultiple(array $cids) {
-
- do {
- db_delete($this->bin)
- ->condition('cid', array_splice($cids, 0, 1000), 'IN')
- ->execute();
- }
- while (count($cids));
- }
-
-
- * Implements BackdropCacheInterface::deletePrefix().
- */
- function deletePrefix(string $prefix) {
- db_delete($this->bin)
- ->condition('cid', db_like($prefix) . '%', 'LIKE')
- ->execute();
- }
-
-
- * Implements BackdropCacheInterface::flush().
- */
- function flush() {
- db_truncate($this->bin)->execute();
- }
-
-
- * Implements BackdropCacheInterface::garbageCollection().
- */
- function garbageCollection() {
- db_delete($this->bin)
- ->condition('expire', CACHE_PERMANENT, '<>')
- ->condition('expire', REQUEST_TIME, '<')
- ->execute();
- }
-
-
- * Implements BackdropCacheInterface::isEmpty().
- */
- function isEmpty() {
- $this->garbageCollection();
- $query = db_select($this->bin);
- $query->addExpression('1');
- $result = $query->range(0, 1)
- ->execute()
- ->fetchField();
- return empty($result);
- }
- }