Changeset 3021

Show
Ignore:
Timestamp:
11/09/05 11:10:34 (3 years ago)
Author:
ryan
Message:

Add cache expiration. Cache requests for non-existant options.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-includes/cache.php

    r3019 r3021  
    5252    var $cache_dir; 
    5353    var $cache_enabled = false; 
     54    var $expiration_time = 86400; 
    5455    var $use_flock = false; 
    5556    var $flock_filename = 'wp_object_cache.lock'; 
     
    5859    var $cache = array (); 
    5960    var $dirty_objects = array (); 
     61    var $non_existant_objects = array(); 
    6062    var $global_groups = array('users', 'usermeta'); 
    6163    var $blog_id; 
     
    6870            $group = 'default'; 
    6971 
    70         if (isset ($this->cache[$group][$id])
     72        if ( false !== $this->get($id, $group, false)
    7173            return false; 
    7274 
     
    7880            $group = 'default'; 
    7981 
    80         if (!isset ($this->cache[$group][$id])
     82        if ( false === $this->get($id, $group, false)
    8183            return false; 
    8284 
    8385        unset ($this->cache[$group][$id]); 
     86        $this->non_existant_objects[$group][$id] = true; 
    8487        $this->dirty_objects[$group][] = $id; 
    8588        return true; 
    8689    } 
    8790 
    88     function get($id, $group = 'default') { 
     91    function get($id, $group = 'default', $count_hits = true) { 
    8992        if ( empty($group) ) 
    9093            $group = 'default'; 
    9194 
    9295        if (isset ($this->cache[$group][$id])) { 
    93             $this->warm_cache_hits += 1; 
     96            if ( $count_hits ) 
     97                $this->warm_cache_hits += 1; 
    9498            return $this->cache[$group][$id]; 
    9599        } 
     100 
     101        if ( isset($this->non_existant_objects[$group][$id]) ) 
     102            return false; 
    96103 
    97104        //  If caching is not enabled, we have to fall back to pulling from the DB. 
     
    105112            } 
    106113 
     114            $this->non_existant_objects[$group][$id] = true; 
    107115            $this->cache_misses += 1; 
    108116            return false; 
     
    111119        $cache_file = $this->cache_dir . $this->get_group_dir($group) . "/" . md5($id . DB_PASSWORD) . '.php'; 
    112120        if (!file_exists($cache_file)) { 
     121            $this->non_existant_objects[$group][$id] = true; 
    113122            $this->cache_misses += 1; 
    114123            return false; 
    115124        } 
     125 
     126        // If the object has expired, remove it from the cache and return false to force 
     127        // a refresh. 
     128        $now = time(); 
     129        if ( (filemtime($cache_file) + $this->expiration_time) <= $now ) { 
     130            $this->cache_misses += 1; 
     131            $this->delete($id, $group); 
     132            return false; 
     133        } 
     134 
    116135        $this->cache[$group][$id] = unserialize(substr(@ file_get_contents($cache_file), strlen(CACHE_SERIAL_HEADER), -strlen(CACHE_SERIAL_FOOTER))); 
    117136        if ( false === $this->cache[$group][$id]) 
    118137            $this->cache[$group][$id] = ''; 
     138 
    119139        $this->cold_cache_hits += 1; 
    120140        return $this->cache[$group][$id]; 
     
    183203            $group = 'default'; 
    184204 
    185         if (!isset ($this->cache[$group][$id])
     205        if ( false === $this->get($id, $group, false)
    186206            return false; 
    187207 
     
    193213            $group = 'default'; 
    194214 
     215        if ( NULL == $data) 
     216            $data = ''; 
     217 
    195218        $this->cache[$group][$id] = $data; 
     219        unset($this->non_existant_objects[$group][$id]); 
    196220        $this->dirty_objects[$group][] = $id; 
     221 
    197222        return true; 
    198223    } 
    199      
     224 
    200225    function save() { 
    201226        //$this->stats(); 
     
    239264            $ids = array_unique($ids); 
    240265            foreach ($ids as $id) { 
    241                 // TODO:  If the id is no longer in the cache, it was deleted and 
    242                 // the file should be removed. 
    243266                $cache_file = $group_dir . md5($id . DB_PASSWORD) . '.php'; 
     267 
     268                // Remove the cache file if the key is not set. 
     269                if ( ! isset($this->cache[$group][$id]) ) { 
     270                    echo "Deleting $group $id<br/>"; 
     271                    if ( file_exists($cache_file) ) 
     272                        unlink($cache_file); 
     273                    continue; 
     274                } 
     275 
    244276                $temp_file = tempnam($group_dir, 'tmp'); 
    245277                $serial = CACHE_SERIAL_HEADER . serialize($this->cache[$group][$id]) . CACHE_SERIAL_FOOTER; 
     
    303335            $this->cache_enabled = true; 
    304336        } 
    305          
     337 
     338        if ( defined('CACHE_EXPIRATION_TIME') ) 
     339            $this->expiration_time = CACHE_EXPIRATION_TIME; 
     340 
    306341        $this->blog_id = md5($blog_id); 
    307342    }