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