| 1 |
<?php |
| 2 |
/** |
| 3 |
* Upstream cache. |
| 4 |
* |
| 5 |
* @package UpStream |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Upstream cache class. |
| 15 |
*/ |
| 16 |
class Upstream_Cache { |
| 17 |
|
| 18 |
/** |
| 19 |
* Instance class. |
| 20 |
* |
| 21 |
* @var object $instance |
| 22 |
*/ |
| 23 |
protected static $instance; |
| 24 |
|
| 25 |
/** |
| 26 |
* Cache data. |
| 27 |
* |
| 28 |
* @var array $cache |
| 29 |
*/ |
| 30 |
protected $cache = array(); |
| 31 |
|
| 32 |
/** |
| 33 |
* Cache setter. |
| 34 |
* |
| 35 |
* @param mixed $key Cache key. |
| 36 |
* @param mixed $value Cache value. |
| 37 |
*/ |
| 38 |
public function set( $key, $value ) { |
| 39 |
$this->cache[ $key ] = $value; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Cache getter. |
| 44 |
* |
| 45 |
* @param mixed $key Cache key. |
| 46 |
* @return mixed Cache value. |
| 47 |
*/ |
| 48 |
public function get( $key ) { |
| 49 |
if ( isset( $this->cache[ $key ] ) ) { |
| 50 |
return $this->cache[ $key ]; |
| 51 |
} |
| 52 |
|
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Cache reseter. |
| 58 |
*/ |
| 59 |
public function reset() { |
| 60 |
$this->cache = array(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Instance getter. |
| 65 |
*/ |
| 66 |
public static function get_instance() { |
| 67 |
if ( empty( static::$instance ) ) { |
| 68 |
$instance = new self(); |
| 69 |
static::$instance = $instance; |
| 70 |
} |
| 71 |
|
| 72 |
return static::$instance; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Cache metadata getter. |
| 78 |
*/ |
| 79 |
function upstream_cache_get_metadata() { |
| 80 |
$str = 'upstream_cache_get_metadata'; |
| 81 |
$args = func_get_args(); |
| 82 |
|
| 83 |
for ( $i = 0; $i < func_num_args(); $i++ ) { |
| 84 |
$str .= $args[ $i ]; |
| 85 |
} |
| 86 |
|
| 87 |
$cache = Upstream_Cache::get_instance(); |
| 88 |
$res = $cache->get( $str ); |
| 89 |
|
| 90 |
if ( false !== $res ) { |
| 91 |
return $res; |
| 92 |
} |
| 93 |
|
| 94 |
$cache->set( $str, $res ); |
| 95 |
|
| 96 |
return call_user_func_array( 'get_metadata', $args ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Cache post meta getter. |
| 101 |
*/ |
| 102 |
function upstream_cache_get_post_meta() { |
| 103 |
$str = 'upstream_cache_get_post_meta'; |
| 104 |
$args = func_get_args(); |
| 105 |
|
| 106 |
for ( $i = 0; $i < func_num_args(); $i++ ) { |
| 107 |
$str .= $args[ $i ]; |
| 108 |
} |
| 109 |
|
| 110 |
$cache = Upstream_Cache::get_instance(); |
| 111 |
$res = $cache->get( $str ); |
| 112 |
|
| 113 |
if ( false !== $res ) { |
| 114 |
return $res; |
| 115 |
} |
| 116 |
|
| 117 |
$cache->set( $str, $res ); |
| 118 |
|
| 119 |
return call_user_func_array( 'get_post_meta', $args ); |
| 120 |
} |
| 121 |
|