|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* MslsSqlCacher |
|
4
|
|
|
* |
|
5
|
|
|
* @author Dennis Ploetner <[email protected]> |
|
6
|
|
|
* @since 1.0 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace lloc\Msls; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Wrapper to avoid direct SQL without caching |
|
13
|
|
|
* |
|
14
|
|
|
* @example https://gist.githubusercontent.com/lloc/2c232cef3f910acf692f/raw/91e5fe9ada922a82a32b83eaabad1e2a2ee50338/MslsSqlCacher.php |
|
15
|
|
|
* |
|
16
|
|
|
* @method mixed get_var( string $sql ) |
|
17
|
|
|
* @method array get_results( string $sql ) |
|
18
|
|
|
* @method string prepare( string $sql, mixed $a, $b = '', $c = '' ) |
|
19
|
|
|
* @method mixed query( string $sql ) |
|
20
|
|
|
* @property string $posts |
|
21
|
|
|
* @property string $options |
|
22
|
|
|
* @property string $blogs |
|
23
|
|
|
* @property int $blogid |
|
24
|
|
|
* @property int $siteid |
|
25
|
|
|
* @package Msls |
|
26
|
|
|
*/ |
|
27
|
|
|
class MslsSqlCacher { |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Database object |
|
31
|
|
|
* |
|
32
|
|
|
* @var object $db |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $db; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Name of the object which created this object |
|
38
|
|
|
* |
|
39
|
|
|
* @var string $caller |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $caller; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Parameters are used to create the key for the cached resultset |
|
45
|
|
|
* |
|
46
|
|
|
* @var mixed $params |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $params; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Constructor |
|
52
|
|
|
* |
|
53
|
|
|
* @param \WPDB $db |
|
54
|
|
|
* @param $caller |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct( \WPDB $db, $caller ) { |
|
57
|
|
|
$this->db = $db; |
|
58
|
|
|
$this->caller = $caller; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Factory |
|
63
|
|
|
* |
|
64
|
|
|
* @uses \WPDB $wpdb |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $caller |
|
67
|
|
|
* |
|
68
|
|
|
* @return MslsSqlCacher |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function init( $caller ) { |
|
71
|
|
|
global $wpdb; |
|
72
|
|
|
|
|
73
|
|
|
return new self( $wpdb, $caller ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Set params |
|
78
|
|
|
* |
|
79
|
|
|
* @param mixed $params |
|
80
|
|
|
* |
|
81
|
|
|
* @return MslsSqlCacher |
|
82
|
|
|
*/ |
|
83
|
|
|
public function set_params( $params ) { |
|
84
|
|
|
$this->params = $params; |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get the name of the key which is in use for the cached object |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function get_key() { |
|
95
|
|
|
$params = ( |
|
96
|
|
|
is_array( $this->params ) ? |
|
97
|
|
|
implode( '_', $this->params ) : |
|
98
|
|
|
$this->params |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
return $this->caller . '_' . $params; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Magic __get |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $key |
|
108
|
|
|
* |
|
109
|
|
|
* @return mixed |
|
110
|
|
|
*/ |
|
111
|
|
|
public function __get( $key ) { |
|
112
|
|
|
return ( isset( $this->db->$key ) ? $this->db->$key : null ); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Call a method of the db-object with the needed args and cache the result |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $method |
|
119
|
|
|
* @param array $args |
|
120
|
|
|
* |
|
121
|
|
|
* @return mixed |
|
122
|
|
|
*/ |
|
123
|
|
|
public function __call( $method, $args ) { |
|
124
|
|
|
if ( 'get_' != substr( $method, 0, 4 ) ) { |
|
125
|
|
|
$result = call_user_func_array( [ $this->db, $method ], $args ); |
|
126
|
|
|
} else { |
|
127
|
|
|
$key = $this->get_key(); |
|
128
|
|
|
$result = wp_cache_get( $key ); |
|
129
|
|
|
if ( false === $result ) { |
|
130
|
|
|
$result = call_user_func_array( [ $this->db, $method ], $args ); |
|
131
|
|
|
wp_cache_set( $key, $result ); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $result; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|