-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathMslsSqlCacher.php
100 lines (86 loc) · 2.17 KB
/
MslsSqlCacher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php declare( strict_types=1 );
namespace lloc\Msls;
/**
* Wrapper to avoid direct SQL without caching
*
* @example https://gist.githubusercontent.com/lloc/2c232cef3f910acf692f/raw/91e5fe9ada922a82a32b83eaabad1e2a2ee50338/MslsSqlCacher.php
*
* @method mixed get_var( string $sql )
* @method mixed[] get_results( string $sql )
* @method string prepare( string $sql, mixed $a, $b = '', $c = '' )
* @method mixed query( string $sql )
* @property string $posts
* @property string $options
* @property string $blogs
* @property int $blogid
* @property int $siteid
*
* @package Msls
*/
class MslsSqlCacher {
/**
* Cache group
*/
const CACHE_GROUP = 'msls-cache-group';
/**
* Database object
*/
protected \wpdb $db;
/**
* Key for the cached result-set
*/
protected string $cache_key;
/**
* Expiration time for the cache in seconds
*/
protected int $expire;
/**
* Constructor
*/
public function __construct( \wpdb $db, string $cache_key, int $expire = 0 ) {
$this->db = $db;
$this->cache_key = $cache_key;
$this->expire = $expire;
}
/**
* Factory
*
* @param string $caller
* @param mixed $params
* @param int $expire
*/
public static function create( string $caller, $params, int $expire = 0 ): self {
global $wpdb;
if ( is_array( $params ) ) {
$params = implode( '_', $params );
}
return new self( $wpdb, esc_attr( $caller . '_' . $params ), $expire );
}
/**
* Magic __get
*
* @return mixed
*/
public function __get( string $name ) {
return $this->db->$name ?? null;
}
/**
* Call a method of the db-object with the needed args and cache the result
*
* @param string $method
* @param array<int|string> $args
*
* @return mixed
*/
public function __call( string $method, array $args ) {
if ( 'get_' != substr( $method, 0, 4 ) ) {
return call_user_func_array( array( $this->db, $method ), $args );
}
$result = wp_cache_get( $this->cache_key, self::CACHE_GROUP );
if ( false === $result ) {
$result = call_user_func_array( array( $this->db, $method ), $args );
wp_cache_set( $this->cache_key, $result, self::CACHE_GROUP, $this->expire );
}
return $result;
}
}