-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclass-wp-predis-decorator.php
93 lines (74 loc) · 2.39 KB
/
class-wp-predis-decorator.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
<?php
namespace WP_Predis;
use Predis\Client as PredisClient;
class Decorator {
// allows the client to be called directly
public $client;
// methods to coerce the return value to a boolean
public $toBool = array(
'set',
'setex',
'exists',
'hexists',
);
// methods to coerce the return value from null to a boolean
public $nullToBool = array(
'get',
'hget',
);
// track time spend waiting for redis responses.
public $time_spent = 0;
public function __construct( PredisClient $client ) {
$this->client = $client;
}
public function info( $section = null ) {
$info = $this->client->info();
return $this->transform_info( $info );
}
public function close() {
return $this->client->disconnect();
}
public function transform_info( $info ) {
// let's pop this off because special formatting is required`
$keyspace = $info['Keyspace'];
unset( $info['Keyspace'] );
$newInfo = array_reduce( $info, function( $carry, $item ) {
return array_merge( $carry, $item );
}, array());
foreach ( $keyspace as $db => $values ) {
$newInfo[ $db ] = str_replace( '&', ',', http_build_query( $values ) );
}
return $newInfo;
}
public function __call( $method_name, $args ) {
// Optionally prevent flushing on non-cli.
if ( in_array( strtolower( $method_name ), [ 'flushdb', 'flushall' ], true ) ) {
$trace = wp_debug_backtrace_summary();
error_log( sprintf( 'wp_cache_flush() requested from ' . $trace ) );
/**
* Filter whether to allow flushing. By default, only allowed on the CLI.
*
* @param bool True to permit flushing, false to disallow it and return immediately.
*/
$allowed = apply_filters( 'wp_cache_flush_allowed', 'cli' === php_sapi_name() );
if ( ! $allowed ) {
trigger_error( sprintf( 'wp_cache_flush() is only allowed via WP CLI. Called from %s', $trace ), E_USER_WARNING );
return false;
}
}
// TODO perhaps we wrap this in a try/catch and return false when
// there's an exception?
$start = microtime( true );
$value = call_user_func_array( array( $this->client, $method_name ), $args );
$this->time_spent += microtime( true ) - $start;
$returns = $value;
$lowered = strtolower( $method_name );
if ( in_array( $lowered, $this->toBool, true ) ) {
$returns = (bool) $value;
}
if ( in_array( $lowered, $this->nullToBool, true ) ) {
$returns = null === $value ? false : $value;
}
return $returns;
}
}