Aleksander Machniak
2016-03-28 46f7b7096450939fe03c95aa81ce06ae4bfca89d
program/lib/Roundcube/rcube_cache_shared.php
@@ -1,6 +1,6 @@
<?php
/*
/**
 +-----------------------------------------------------------------------+
 | This file is part of the Roundcube Webmail client                     |
 | Copyright (C) 2011-2013, The Roundcube Dev Team                       |
@@ -17,7 +17,6 @@
 | Author: Aleksander Machniak <alec@alec.pl>                            |
 +-----------------------------------------------------------------------+
*/
/**
 * Interface class for accessing Roundcube shared cache
@@ -89,7 +88,6 @@
        $this->prefix    = $prefix;
    }
    /**
     * Returns cached value.
     *
@@ -106,7 +104,6 @@
        return $this->cache[$key];
    }
    /**
     * Sets (add/update) value in cache.
     *
@@ -118,7 +115,6 @@
        $this->cache[$key]         = $data;
        $this->cache_changes[$key] = true;
    }
    /**
     * Returns cached value without storing it in internal memory.
@@ -136,7 +132,6 @@
        return $this->read_record($key, true);
    }
    /**
     * Sets (add/update) value in cache and immediately saves
     * it in the backend, no internal memory will be used.
@@ -150,7 +145,6 @@
    {
        return $this->write_record($key, $this->serialize($data));
    }
    /**
     * Clears the cache.
@@ -188,7 +182,6 @@
        $this->remove_record($key, $prefix_mode);
    }
    /**
     * Remove cache records older than ttl
     */
@@ -203,7 +196,6 @@
        }
    }
    /**
     * Remove expired records of all caches
     */
@@ -214,7 +206,6 @@
        $db->query("DELETE FROM " . $db->table_name('cache_shared', true) . " WHERE `expires` < " . $db->now());
    }
    /**
     * Writes the cache back to the DB.
@@ -237,8 +228,10 @@
        if ($this->index_changed) {
            $this->write_index();
        }
    }
        // reset internal cache index, thanks to this we can force index reload
        $this->index = null;
    }
    /**
     * Reads cache entry.
@@ -324,7 +317,6 @@
        return $this->cache[$key];
    }
    /**
     * Writes single cache record into DB.
     *
@@ -349,8 +341,16 @@
            $result = $this->add_record($this->ckey($key), $data);
            // make sure index will be updated
            if ($result && !array_key_exists($key, $this->cache_sums)) {
                $this->cache_sums[$key] = true;
            if ($result) {
                if (!array_key_exists($key, $this->cache_sums)) {
                    $this->cache_sums[$key] = true;
                }
                $this->load_index();
                if (!$this->index_changed && !in_array($key, $this->index)) {
                    $this->index_changed = true;
                }
            }
            return $result;
@@ -389,7 +389,6 @@
        return $this->db->affected_rows($result);
    }
    /**
     * Deletes the cache record(s).
     *
@@ -409,22 +408,29 @@
            // Remove all keys
            if ($key === null) {
                foreach ($this->index as $key) {
                    $this->delete_record($key, false);
                    $this->delete_record($this->ckey($key));
                }
                $this->index = array();
            }
            // Remove keys by name prefix
            else if ($prefix_mode) {
                foreach ($this->index as $k) {
                foreach ($this->index as $idx => $k) {
                    if (strpos($k, $key) === 0) {
                        $this->delete_record($k);
                        $this->delete_record($this->ckey($k));
                        unset($this->index[$idx]);
                    }
                }
            }
            // Remove one key by name
            else {
                $this->delete_record($key);
                $this->delete_record($this->ckey($key));
                if (($idx = array_search($key, $this->index)) !== false) {
                    unset($this->index[$idx]);
                }
            }
            $this->index_changed = true;
            return;
        }
@@ -445,11 +451,10 @@
        $this->db->query("DELETE FROM " . $this->table . $where);
    }
    /**
     * Adds entry into memcache/apc DB.
     *
     * @param string $key  Cache key name
     * @param string $key  Cache internal key name
     * @param mixed  $data Serialized cache data
     *
     * @param boolean True on success, False on failure
@@ -475,44 +480,32 @@
            $this->debug('set', $key, $data, $result);
        }
        if ($result) {
            $this->index_changed = true;
        return $result;
    }
    /**
     * Deletes entry from memcache/apc DB.
     *
     * @param string $key Cache internal key name
     *
     * @param boolean True on success, False on failure
     */
    private function delete_record($key)
    {
        if ($this->type == 'memcache') {
            // #1488592: use 2nd argument
            $result = $this->db->delete($key, 0);
        }
        else {
            $result = apc_delete($key);
        }
        if ($this->debug) {
            $this->debug('delete', $key, null, $result);
        }
        return $result;
    }
    /**
     * Deletes entry from memcache/apc DB.
     */
    private function delete_record($key, $index=true)
    {
        $ckey = $this->ckey($key);
        if ($this->type == 'memcache') {
            // #1488592: use 2nd argument
            $result = $this->db->delete($ckey, 0);
        }
        else {
            $result = apc_delete($ckey);
        }
        if ($this->debug) {
            $this->debug('delete', $ckey, null, $result);
        }
        if ($result) {
            $this->index_changed = true;
        }
        if ($index) {
            if (($idx = array_search($key, $this->index)) !== false) {
                unset($this->index[$idx]);
            }
        }
    }
    /**
     * Writes the index entry into memcache/apc DB.
@@ -543,7 +536,6 @@
        $this->add_record($this->ikey(), $data);
    }
    /**
     * Gets the index entry from memcache/apc DB.
     */
@@ -573,7 +565,6 @@
        $this->index = $data ? unserialize($data) : array();
    }
    /**
     * Creates cache key name (for memcache and apc)
     *
@@ -585,7 +576,6 @@
    {
        return $this->prefix . ':' . $key;
    }
    /**
     * Creates index cache key name (for memcache and apc)