| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* an extremely simple non persistent cache system |
| 5 |
* not as fast as using directly an array but more readable |
| 6 |
* |
| 7 |
* @since 1.7 |
| 8 |
*/ |
| 9 |
class PLL_Cache { |
| 10 |
protected $blog_id, $cache; |
| 11 |
|
| 12 |
/** |
| 13 |
* constructor |
| 14 |
* |
| 15 |
* @since 1.7 |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
$this->blog_id = get_current_blog_id(); |
| 19 |
add_action( 'switch_blog', array( $this, 'switch_blog' ) ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* called when switching blog |
| 24 |
* |
| 25 |
* @since 1.7 |
| 26 |
* |
| 27 |
* @param int $new_blog |
| 28 |
*/ |
| 29 |
public function switch_blog( $new_blog ) { |
| 30 |
$this->blog_id = $new_blog; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* add a value in cache |
| 35 |
* |
| 36 |
* @since 1.7 |
| 37 |
* |
| 38 |
* @param string $key |
| 39 |
* @param mixed $data |
| 40 |
*/ |
| 41 |
public function set( $key, $data ) { |
| 42 |
$this->cache[ $this->blog_id ][ $key ] = $data; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* get value from cache |
| 47 |
* |
| 48 |
* @since 1.7 |
| 49 |
* |
| 50 |
* @param string $key |
| 51 |
* @return mixed $data |
| 52 |
*/ |
| 53 |
public function get( $key ) { |
| 54 |
return isset( $this->cache[ $this->blog_id ][ $key ] ) ? $this->cache[ $this->blog_id ][ $key ] : false; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* clean the cache (for this blog only) |
| 59 |
* |
| 60 |
* @since 1.7 |
| 61 |
*/ |
| 62 |
public function clean( $key = '' ) { |
| 63 |
if ( empty( $key ) ) { |
| 64 |
unset( $this->cache[ $this->blog_id ] ); |
| 65 |
} |
| 66 |
else { |
| 67 |
unset( $this->cache[ $this->blog_id ][ $key ] ); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|