Skip to content

Commit 3227bc6

Browse files
committed
wip
1 parent bdacaca commit 3227bc6

File tree

4 files changed

+59
-40
lines changed

4 files changed

+59
-40
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Redis Object Cache for WordPress
22

3+
Update Banner: Fast load times; Less Database load; screenshots
4+
Add: SpinupWP, BlueHost, LiquidWeb and other platforms that use it to the banner maybe also to the readme
5+
36
A persistent object cache backend powered by Redis®¹. Supports [Predis](https://github.com/predis/predis/), [PhpRedis (PECL)](https://github.com/phpredis/phpredis), [Relay](https://relaycache.com), replication, sentinels, clustering and [WP-CLI](http://wp-cli.org/).
47

58
[![Redis Object Cache screenshots](/.wordpress-org/collage-sm.jpg?raw=true)](/.wordpress-org/collage.png?raw=true)
@@ -41,7 +44,8 @@ The Redis Object Cache plugin comes with vast set of configuration options. If y
4144
| `WP_REDIS_MAXTTL` | `0` | The maximum time-to-live of cache keys |
4245
| `WP_REDIS_CLIENT` | | The client used to communicate with Redis. Defaults to `phpredis` when installed, otherwise `predis`. Supports `phpredis`, `predis`, `relay` |
4346
| `WP_REDIS_TIMEOUT` | `1` | The connection timeout in seconds |
44-
| `WP_REDIS_READ_TIMEOUT` | `1` | The timeout in seconds when reading/writing |
47+
| `WP_REDIS_READ_TIMEOUT` | `1` | The timeout in seconds when reading/writing |
48+
| `WP_REDIS_FLUSH_TIMEOUT` | `5` | The timeout in seconds when flushing |
4549
| `WP_REDIS_IGNORED_GROUPS` | `[]` | Groups that should not be cached between requests in Redis |
4650

4751
<details>

includes/class-plugin.php

+2-14
Original file line numberDiff line numberDiff line change
@@ -946,13 +946,7 @@ public function do_admin_actions() {
946946
);
947947

948948
if ( $result ) {
949-
try {
950-
$predis = new Predis();
951-
$predis->flush();
952-
} catch ( Exception $exception ) {
953-
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
954-
error_log( $exception );
955-
}
949+
(new Predis)->flush();
956950
}
957951

958952
/**
@@ -982,13 +976,7 @@ public function do_admin_actions() {
982976
$result = $wp_filesystem->delete( WP_CONTENT_DIR . '/object-cache.php' );
983977

984978
if ( $result ) {
985-
try {
986-
$predis = new Predis();
987-
$predis->flush();
988-
} catch ( Exception $exception ) {
989-
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
990-
error_log( $exception );
991-
}
979+
(new Predis)->flush();
992980
}
993981

994982
/**

includes/class-predis.php

+49-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Predis {
2424
*
2525
* @return void
2626
*/
27-
public function connect() {
27+
public function connect( $read_timeout = null ) {
2828
// Load bundled Predis library.
2929
if ( ! class_exists( '\Predis\Client' ) ) {
3030
require_once WP_REDIS_PLUGIN_PATH . '/dependencies/predis/predis/autoload.php';
@@ -39,7 +39,7 @@ public function connect() {
3939
'port' => 6379,
4040
'database' => 0,
4141
'timeout' => 1,
42-
'read_timeout' => 1,
42+
'read_timeout' => $read_timeout ?? 1,
4343
];
4444

4545
$settings = [
@@ -128,13 +128,26 @@ public function connect() {
128128
}
129129

130130
/**
131-
* Invalidate all items in the cache.
131+
* Flushes the entire Redis database using the `WP_REDIS_FLUSH_TIMEOUT`.
132132
*
133-
* @return bool True on success, false on failure.
133+
* @param bool $throw_exception Whether to throw exception on error.
134+
* @return bool
134135
*/
135-
public function flush() {
136+
public function flush( $throw_exception = false ) {
137+
$flush_timeout = defined( 'WP_REDIS_FLUSH_TIMEOUT' )
138+
? intval(WP_REDIS_FLUSH_TIMEOUT)
139+
: 5;
140+
136141
if ( is_null( $this->redis ) ) {
137-
$this->connect();
142+
try {
143+
$this->connect( $flush_timeout );
144+
} catch ( Exception $exception ) {
145+
if ( $throw_exception ) {
146+
throw $exception;
147+
}
148+
149+
return false;
150+
}
138151
}
139152

140153
if ( defined( 'WP_REDIS_CLUSTER' ) ) {
@@ -143,23 +156,46 @@ public function flush() {
143156
$this->redis->flushdb( $master );
144157
}
145158
} catch ( Exception $exception ) {
159+
if ( $throw_exception ) {
160+
throw $exception;
161+
}
162+
146163
return false;
147164
}
148-
} else {
149-
try {
150-
$this->redis->flushdb();
151-
} catch ( Exception $exception ) {
152-
return false;
165+
166+
return true;
167+
}
168+
169+
try {
170+
// TODO: test Predis return value...
171+
172+
var_dump($this->redis->flushdb());
173+
exit;
174+
175+
return $this->redis->flushdb();
176+
} catch ( Exception $exception ) {
177+
if ( $throw_exception ) {
178+
throw $exception;
153179
}
180+
181+
return false;
154182
}
183+
}
155184

156-
return true;
185+
/**
186+
* Flushes the entire Redis database using the `WP_REDIS_FLUSH_TIMEOUT`
187+
* and will throw an exception if anything goes wrong.
188+
*
189+
* @return bool
190+
*/
191+
public function flushOrFail() {
192+
return $this->flush( true );
157193
}
158194

159195
/**
160196
* Builds a clean connection array out of redis clusters array.
161197
*
162-
* @return array
198+
* @return array
163199
*/
164200
protected function build_cluster_connection_array() {
165201
$cluster = array_values( WP_REDIS_CLUSTER );

includes/cli/class-commands.php

+3-12
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,11 @@ public function update_dropin() {
173173
*/
174174
protected function flush_redis() {
175175
try {
176-
$predis = new Predis();
177-
$predis->connect();
176+
return (new Predis)->flushOrFail();
178177
} catch ( Exception $exception ) {
179-
return $exception->getMessage();
180-
}
178+
error_log( $exception ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
181179

182-
try {
183-
$predis->flush();
184-
} catch ( Exception $exception ) {
185-
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
186-
error_log( $exception );
180+
return $exception->getMessage();
187181
}
188-
189-
return true;
190182
}
191-
192183
}

0 commit comments

Comments
 (0)