123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- <?php
- function _cache_get_object($bin) {
-
-
- static $cache_objects;
- if (!isset($cache_objects[$bin])) {
- $class = variable_get('cache_class_' . $bin);
- if (!isset($class)) {
- $class = variable_get('cache_default_class', 'DrupalDatabaseCache');
- }
- $cache_objects[$bin] = new $class($bin);
- }
- return $cache_objects[$bin];
- }
- function cache_get($cid, $bin = 'cache') {
- return _cache_get_object($bin)->get($cid);
- }
- function cache_get_multiple(array &$cids, $bin = 'cache') {
- return _cache_get_object($bin)->getMultiple($cids);
- }
- function cache_set($cid, $data, $bin = 'cache', $expire = CACHE_PERMANENT) {
- return _cache_get_object($bin)->set($cid, $data, $expire);
- }
- function cache_clear_all($cid = NULL, $bin = NULL, $wildcard = FALSE) {
- if (!isset($cid) && !isset($bin)) {
-
-
- if (module_exists('block')) {
- cache_clear_all(NULL, 'cache_block');
- }
- cache_clear_all(NULL, 'cache_page');
- return;
- }
- return _cache_get_object($bin)->clear($cid, $wildcard);
- }
- function cache_is_empty($bin) {
- return _cache_get_object($bin)->isEmpty();
- }
- interface DrupalCacheInterface {
-
- function __construct($bin);
-
- function get($cid);
-
- function getMultiple(&$cids);
-
- function set($cid, $data, $expire = CACHE_PERMANENT);
-
- function clear($cid = NULL, $wildcard = FALSE);
-
- function isEmpty();
- }
- class DrupalDatabaseCache implements DrupalCacheInterface {
- protected $bin;
-
- function __construct($bin) {
- $this->bin = $bin;
- }
-
- function get($cid) {
- $cids = array($cid);
- $cache = $this->getMultiple($cids);
- return reset($cache);
- }
-
- function getMultiple(&$cids) {
- try {
-
- $this->garbageCollection($this->bin);
-
-
-
-
-
-
-
- $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();
- }
- }
-
- protected function garbageCollection() {
- $cache_lifetime = variable_get('cache_lifetime', 0);
-
-
- if (isset($_SESSION['cache_expiration'])) {
- $expire = REQUEST_TIME - $cache_lifetime;
- foreach ($_SESSION['cache_expiration'] as $bin => $timestamp) {
- if ($timestamp < $expire) {
- unset($_SESSION['cache_expiration'][$bin]);
- }
- }
- if (!$_SESSION['cache_expiration']) {
- unset($_SESSION['cache_expiration']);
- }
- }
-
-
- if (!$cache_lifetime) {
- return;
- }
-
-
- $cache_flush = variable_get('cache_flush_' . $this->bin, 0);
- if ($cache_flush && ($cache_flush + $cache_lifetime <= REQUEST_TIME)) {
-
- variable_set('cache_flush_' . $this->bin, 0);
-
- db_delete($this->bin)
- ->condition('expire', CACHE_PERMANENT, '<>')
- ->condition('expire', $cache_flush, '<=')
- ->execute();
- }
- }
-
- protected function prepareItem($cache) {
- global $user;
- if (!isset($cache->data)) {
- return FALSE;
- }
-
-
-
- if ($cache->expire != CACHE_PERMANENT && variable_get('cache_lifetime', 0) && isset($_SESSION['cache_expiration'][$this->bin]) && $_SESSION['cache_expiration'][$this->bin] > $cache->created) {
-
- return FALSE;
- }
-
-
- if ($cache->serialized) {
- $cache->data = unserialize($cache->data);
- }
- return $cache;
- }
-
- function set($cid, $data, $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) {
-
- }
- }
-
- function clear($cid = NULL, $wildcard = FALSE) {
- global $user;
- if (empty($cid)) {
- if (variable_get('cache_lifetime', 0)) {
-
-
-
- $_SESSION['cache_expiration'][$this->bin] = REQUEST_TIME;
- $cache_flush = variable_get('cache_flush_' . $this->bin, 0);
- if ($cache_flush == 0) {
-
- variable_set('cache_flush_' . $this->bin, REQUEST_TIME);
- }
- elseif (REQUEST_TIME > ($cache_flush + variable_get('cache_lifetime', 0))) {
-
-
- db_delete($this->bin)
- ->condition('expire', CACHE_PERMANENT, '<>')
- ->condition('expire', REQUEST_TIME, '<')
- ->execute();
- variable_set('cache_flush_' . $this->bin, 0);
- }
- }
- else {
-
- db_delete($this->bin)
- ->condition('expire', CACHE_PERMANENT, '<>')
- ->condition('expire', REQUEST_TIME, '<')
- ->execute();
- }
- }
- else {
- if ($wildcard) {
- if ($cid == '*') {
- db_truncate($this->bin)->execute();
- }
- else {
- db_delete($this->bin)
- ->condition('cid', db_like($cid) . '%', 'LIKE')
- ->execute();
- }
- }
- elseif (is_array($cid)) {
-
- do {
- db_delete($this->bin)
- ->condition('cid', array_splice($cid, 0, 1000), 'IN')
- ->execute();
- }
- while (count($cid));
- }
- else {
- db_delete($this->bin)
- ->condition('cid', $cid)
- ->execute();
- }
- }
- }
-
- function isEmpty() {
- $this->garbageCollection();
- $query = db_select($this->bin);
- $query->addExpression('1');
- $result = $query->range(0, 1)
- ->execute()
- ->fetchField();
- return empty($result);
- }
- }
|