Changeset 1201

Show
Ignore:
Timestamp:
03/10/08 18:05:28 (9 months ago)
Author:
donncha
Message:

Hopefully fix filesystem caching, #557

Files:

Legend:

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

    r1187 r1201  
    8080        } 
    8181 
    82         function key($key, $group) { 
    83                 global $blog_id
     82        function maybe_localize_group( $group ) { 
     83                global $wpdb
    8484 
    8585                if ( empty($group) ) 
    8686                        $group = 'default'; 
    8787 
    88                 if (false !== array_search($group, $this->global_groups)) 
    89                         $prefix = ''; 
    90                 else 
    91                         $prefix = $blog_id . '-'; 
    92  
    93                 if( '' != $key ) 
    94                         $key = '-' . $key; 
    95  
    96                 return "$prefix$group$key"; 
    97         } 
    98  
     88                $pre_test = substr( $group, 0, strpos( $group, '-' ) ); 
     89                if( $pre_test == intval( $pre_test ) ) 
     90                        return $group; // already localized. 
     91                $prefix = ''; 
     92                if (false === array_search($group, $this->global_groups)) 
     93                        $prefix = $wpdb->blogid . '-'; 
     94 
     95                return "$prefix$group"; 
     96        } 
    9997 
    10098        function add($id, $data, $group = 'default', $expire = '') { 
     
    109107 
    110108        function delete($id, $group = 'default', $force = false) { 
    111                 $hash = $this->key($id, $group); 
    112109                if (empty ($group)) 
    113110                        $group = 'default'; 
     111                $group = $this->maybe_localize_group( $group ); 
    114112 
    115113                if (!$force && false === $this->get($id, $group, false)) 
    116114                        return false; 
    117115 
    118                 unset ($this->cache[$hash]); 
    119                 $this->non_existant_objects[$hash] = true; 
    120                 $this->dirty_objects[$this->key( '', $group )][] = $id; 
     116                unset ($this->cache[$group][$id]); 
     117                $this->non_existant_objects[$group][$id] = true; 
     118                $this->dirty_objects[$group][] = $id; 
    121119                return true; 
    122120        } 
     
    142140                if (empty ($group)) 
    143141                        $group = 'default'; 
    144  
    145                 $group_key = $this->key( '', $group ); 
    146                 $hash = $this->key($id, $group_key); 
    147  
    148                 if (isset ($this->cache[$hash])) { 
     142                $group = $this->maybe_localize_group( $group ); 
     143 
     144                if (isset ($this->cache[$group][$id])) { 
    149145                        if ($count_hits) 
    150146                                $this->warm_cache_hits += 1; 
    151                         return $this->cache[$hash]; 
    152                 } 
    153  
    154                 if (isset ($this->non_existant_objects[$hash])) 
     147                        return $this->cache[$group][$id]; 
     148                } 
     149 
     150                if (isset ($this->non_existant_objects[$group][$id])) 
    155151                        return false; 
    156152 
    157153                //  If caching is not enabled, we have to fall back to pulling from the DB. 
    158154                if (!$this->cache_enabled) { 
    159                         if (!isset ($this->cache[$group_key])) 
    160                                 $this->load_group_from_db($group_key); 
    161  
    162                         if (isset ($this->cache[$hash])) { 
     155                        if (!isset ($this->cache[$group])) 
     156                                $this->load_group_from_db($group); 
     157 
     158                        if (isset ($this->cache[$group][$id])) { 
    163159                                $this->cold_cache_hits += 1; 
    164                                 return $this->cache[$hash]; 
    165                         } 
    166  
    167                         $this->non_existant_objects[$hash] = true; 
     160                                return $this->cache[$group][$id]; 
     161                        } 
     162 
     163                        $this->non_existant_objects[$group][$id] = true; 
    168164                        $this->cache_misses += 1; 
    169165                        return false; 
    170166                } 
    171167 
    172                 $cache_file = $this->cache_dir.$this->get_group_dir($group_key)."/".$this->hash($hash).'.php'; 
     168                $cache_file = $this->cache_dir.$this->get_group_dir($group)."/".$this->hash($id).'.php'; 
    173169                if (!file_exists($cache_file)) { 
    174                         $this->non_existant_objects[$hash] = true; 
     170                        $this->non_existant_objects[$group][$id] = true; 
    175171                        $this->cache_misses += 1; 
    176172                        return false; 
     
    186182                } 
    187183 
    188                 $this->cache[$hash] = unserialize(base64_decode(substr(@ file_get_contents($cache_file), strlen(CACHE_SERIAL_HEADER), -strlen(CACHE_SERIAL_FOOTER)))); 
    189                 if (false === $this->cache[$hash]) 
    190                         $this->cache[$hash] = ''; 
     184                $this->cache[$group][$id] = unserialize(base64_decode(substr(@ file_get_contents($cache_file), strlen(CACHE_SERIAL_HEADER), -strlen(CACHE_SERIAL_FOOTER)))); 
     185                if (false === $this->cache[$group][$id]) 
     186                        $this->cache[$group][$id] = ''; 
    191187 
    192188                $this->cold_cache_hits += 1; 
    193                 return $this->cache[$hash]; 
     189                return $this->cache[$group][$id]; 
    194190        } 
    195191 
     
    211207        function load_group_from_db($group) { 
    212208                return; 
     209                global $wpdb; 
     210 
     211                if ('category' == $group) { 
     212                        $this->cache['category'] = array (); 
     213                        if ($dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories")) { 
     214                                foreach ($dogs as $catt) 
     215                                        $this->cache['category'][$catt->cat_ID] = $catt; 
     216                        } 
     217                } 
     218 
    213219        } 
    214220 
     
    287293 
    288294        function set($id, $data, $group = 'default', $expire = '') { 
    289                 $group_key = $this->key( '', $group ); 
    290                 $hash = $this->key($id, $group_key); 
    291295                if (empty ($group)) 
    292296                        $group = 'default'; 
    293  
    294                 if (NULL === $data) 
     297                $group = $this->maybe_localize_group( $group ); 
     298 
     299                if (NULL == $data) 
    295300                        $data = ''; 
    296301 
    297                 $this->cache[$hash] = $data; 
    298                 unset ($this->non_existant_objects[$hash]); 
    299                 $this->dirty_objects[$group_key][] = $id; 
     302                $this->cache[$group][$id] = $data; 
     303                unset ($this->non_existant_objects[$group][$id]); 
     304                $this->dirty_objects[$group][] = $id; 
    300305 
    301306                return true; 
     
    334339                $errors = 0; 
    335340                foreach ($this->dirty_objects as $group => $ids) { 
    336                         if ( in_array($group, $this->non_persistent_groups) ) 
    337                                 continue; 
    338  
    339341                        $group_dir = $this->make_group_dir($group, $dir_perms); 
    340342 
    341343                        $ids = array_unique($ids); 
    342344                        foreach ($ids as $id) { 
    343                                 $hash = $this->key($id, $group); 
    344                                 $cache_file = $group_dir.$this->hash($hash).'.php'; 
     345                                $cache_file = $group_dir.$this->hash($id).'.php'; 
    345346 
    346347                                // Remove the cache file if the key is not set. 
    347                                 if (!isset ($this->cache[$hash])) { 
     348                                if (!isset ($this->cache[$group][$id])) { 
    348349                                        if (file_exists($cache_file)) 
    349350                                                @ unlink($cache_file); 
     
    352353 
    353354                                $temp_file = tempnam($group_dir, 'tmp'); 
    354                                 $serial = CACHE_SERIAL_HEADER.base64_encode(serialize($this->cache[$hash])).CACHE_SERIAL_FOOTER; 
     355                                $serial = CACHE_SERIAL_HEADER.base64_encode(serialize($this->cache[$group][$id])).CACHE_SERIAL_FOOTER; 
    355356                                $fd = @fopen($temp_file, 'w'); 
    356357                                if ( false === $fd ) { 
     
    381382        function stats() { 
    382383                echo "<p>"; 
    383                 echo "<strong>Cold Cache Hits:</strong> {$this->cold_cache_hits}<br />"; 
    384                 echo "<strong>Warm Cache Hits:</strong> {$this->warm_cache_hits}<br />"; 
    385                 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />"; 
     384                echo "<strong>Cold Cache Hits:</strong> {$this->cold_cache_hits}<br/>"; 
     385                echo "<strong>Warm Cache Hits:</strong> {$this->warm_cache_hits}<br/>"; 
     386                echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br/>"; 
    386387                echo "</p>"; 
    387388 
    388389                foreach ($this->cache as $group => $cache) { 
    389390                        echo "<p>"; 
    390                         echo "<strong>Group:</strong> $group<br />"; 
     391                        echo "<strong>Group:</strong> $group<br/>"; 
    391392                        echo "<strong>Cache:</strong>"; 
    392393                        echo "<pre>";