代码学习之wordpress 的缓存处理类
<?php class WP_Object_Cache { var $cache = array (); var $non_existent_objects = array (); var $cache_hits = 0; var $cache_misses = 0; var $global_groups = array(); function add( $id, $data, $group = 'default', $expire = '' ) { if ( empty ($group) ) $group = 'default'; if (false !== $this->get($id, $group)) return false; return $this->set($id, $data, $group, $expire); } function add_global_groups( $groups ) { $groups = (array) $groups; $this->global_groups = array_merge($this->global_groups, $groups); $this->global_groups = array_unique($this->global_groups); } function delete($id, $group = 'default', $force = false) { if (empty ($group)) $group = 'default'; if (!$force && false === $this->get($id, $group)) return false; unset ($this->cache[$group][$id]); $this->non_existent_objects[$group][$id] = true; return true; } function flush() { $this->cache = array (); return true; } function get($id, $group = 'default') { if ( empty ($group) ) $group = 'default'; if ( isset ($this->cache[$group][$id]) ) { $this->cache_hits += 1; if ( is_object($this->cache[$group][$id]) ) return wp_clone($this->cache[$group][$id]); else return $this->cache[$group][$id]; } if ( isset ($this->non_existent_objects[$group][$id]) ) return false; $this->non_existent_objects[$group][$id] = true; $this->cache_misses += 1; return false; } function replace($id, $data, $group = 'default', $expire = '') { if (empty ($group)) $group = 'default'; if ( false === $this->get($id, $group) ) return false; return $this->set($id, $data, $group, $expire); } function reset() { // Clear out non-global caches since the blog ID has changed. foreach ( array_keys($this->cache) as $group ) { if ( !in_array($group, $this->global_groups) ) unset($this->cache[$group]); } } function set($id, $data, $group = 'default', $expire = '') { if ( empty ($group) ) $group = 'default'; if ( NULL === $data ) $data = ''; if ( is_object($data) ) $data = wp_clone($data); $this->cache[$group][$id] = $data; if ( isset($this->non_existent_objects[$group][$id]) ) unset ($this->non_existent_objects[$group][$id]); return true; } function stats() { echo "<p>"; echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />"; echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />"; echo "</p>"; foreach ($this->cache as $group => $cache) { echo "<p>"; echo "<strong>Group:</strong> $group<br />"; echo "<strong>Cache:</strong>"; echo "<pre>"; print_r($cache); echo "</pre>"; } } function WP_Object_Cache() { return $this->__construct(); } function __construct() { register_shutdown_function(array(&$this, "__destruct")); } function __destruct() { return true; } } ?>
主要是学习缓存处理的思路,代码方面,其实个人用,就不必去向下兼容了