Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tillkruss committed Nov 6, 2023
1 parent bdacaca commit 3227bc6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 40 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Redis Object Cache for WordPress

Update Banner: Fast load times; Less Database load; screenshots
Add: SpinupWP, BlueHost, LiquidWeb and other platforms that use it to the banner maybe also to the readme

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/).

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

<details>
Expand Down
16 changes: 2 additions & 14 deletions includes/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -946,13 +946,7 @@ public function do_admin_actions() {
);

if ( $result ) {
try {
$predis = new Predis();
$predis->flush();
} catch ( Exception $exception ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( $exception );
}
(new Predis)->flush();
}

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

if ( $result ) {
try {
$predis = new Predis();
$predis->flush();
} catch ( Exception $exception ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( $exception );
}
(new Predis)->flush();
}

/**
Expand Down
62 changes: 49 additions & 13 deletions includes/class-predis.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Predis {
*
* @return void
*/
public function connect() {
public function connect( $read_timeout = null ) {
// Load bundled Predis library.
if ( ! class_exists( '\Predis\Client' ) ) {
require_once WP_REDIS_PLUGIN_PATH . '/dependencies/predis/predis/autoload.php';
Expand All @@ -39,7 +39,7 @@ public function connect() {
'port' => 6379,
'database' => 0,
'timeout' => 1,
'read_timeout' => 1,
'read_timeout' => $read_timeout ?? 1,
];

$settings = [
Expand Down Expand Up @@ -128,13 +128,26 @@ public function connect() {
}

/**
* Invalidate all items in the cache.
* Flushes the entire Redis database using the `WP_REDIS_FLUSH_TIMEOUT`.
*
* @return bool True on success, false on failure.
* @param bool $throw_exception Whether to throw exception on error.
* @return bool
*/
public function flush() {
public function flush( $throw_exception = false ) {
$flush_timeout = defined( 'WP_REDIS_FLUSH_TIMEOUT' )
? intval(WP_REDIS_FLUSH_TIMEOUT)
: 5;

if ( is_null( $this->redis ) ) {
$this->connect();
try {
$this->connect( $flush_timeout );
} catch ( Exception $exception ) {
if ( $throw_exception ) {
throw $exception;
}

return false;
}
}

if ( defined( 'WP_REDIS_CLUSTER' ) ) {
Expand All @@ -143,23 +156,46 @@ public function flush() {
$this->redis->flushdb( $master );
}
} catch ( Exception $exception ) {
if ( $throw_exception ) {
throw $exception;
}

return false;
}
} else {
try {
$this->redis->flushdb();
} catch ( Exception $exception ) {
return false;

return true;
}

try {
// TODO: test Predis return value...

var_dump($this->redis->flushdb());
exit;

return $this->redis->flushdb();

Check failure on line 175 in includes/class-predis.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.2; Relay 0.5.1)

Unreachable statement - code above always terminates.

Check failure on line 175 in includes/class-predis.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4; Relay 0.5.1)

Unreachable statement - code above always terminates.

Check failure on line 175 in includes/class-predis.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.2; Relay 0.5.1)

Unreachable statement - code above always terminates.

Check failure on line 175 in includes/class-predis.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.2; Relay 0.5.1)

Unreachable statement - code above always terminates.

Check failure on line 175 in includes/class-predis.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4; Relay 0.5.1)

Unreachable statement - code above always terminates.

Check failure on line 175 in includes/class-predis.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.2; Relay 0.5.1)

Unreachable statement - code above always terminates.
} catch ( Exception $exception ) {
if ( $throw_exception ) {
throw $exception;
}

return false;
}
}

return true;
/**
* Flushes the entire Redis database using the `WP_REDIS_FLUSH_TIMEOUT`
* and will throw an exception if anything goes wrong.
*
* @return bool
*/
public function flushOrFail() {
return $this->flush( true );
}

/**
* Builds a clean connection array out of redis clusters array.
*
* @return array
* @return array
*/
protected function build_cluster_connection_array() {
$cluster = array_values( WP_REDIS_CLUSTER );
Expand Down
15 changes: 3 additions & 12 deletions includes/cli/class-commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,11 @@ public function update_dropin() {
*/
protected function flush_redis() {
try {
$predis = new Predis();
$predis->connect();
return (new Predis)->flushOrFail();
} catch ( Exception $exception ) {
return $exception->getMessage();
}
error_log( $exception ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log

try {
$predis->flush();
} catch ( Exception $exception ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( $exception );
return $exception->getMessage();
}

return true;
}

}

0 comments on commit 3227bc6

Please sign in to comment.