Skip to content

Commit

Permalink
introduce NOOP backend and admin notice for unavailable backend (#305)
Browse files Browse the repository at this point in the history
We should not silently ignore misconfiguration and fall back to
DB caching. We now check for availability and disable caching
via a "no operation" backend. We can also add a notice for APC
now which is no longer supported.
  • Loading branch information
stklcode committed Aug 6, 2024
1 parent 6175b6a commit f45e3f9
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cachify.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
* @param string $class_name the class name.
*/
function cachify_autoload( $class_name ) {
if ( in_array( $class_name, array( 'Cachify', 'Cachify_Backend', 'Cachify_CLI', 'Cachify_DB', 'Cachify_HDD', 'Cachify_MEMCACHED', 'Cachify_REDIS' ), true ) ) {
if ( in_array( $class_name, array( 'Cachify', 'Cachify_Backend', 'Cachify_CLI', 'Cachify_DB', 'Cachify_HDD', 'Cachify_MEMCACHED', 'Cachify_NOOP', 'Cachify_REDIS' ), true ) ) {
require_once sprintf(
'%s/inc/class-%s.php',
CACHIFY_DIR,
Expand Down
8 changes: 4 additions & 4 deletions images/symbols.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
112 changes: 112 additions & 0 deletions inc/class-cachify-noop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Class for NO-OP caching.
*
* @package Cachify
*/

/* Quit */
defined( 'ABSPATH' ) || exit;

/**
* Cachify_NOOP
*
* No-Op backend which does simply nothing.
*
* @since 2.4.0
*/
final class Cachify_NOOP implements Cachify_Backend {

/**
* Name of the unavailable caching method, i.e. the reason why we chose no-op.
*
* @var string
*/
public $unavailable_method;

/**
* Constructor with name of the unavailable method.
*
* @param string $unavailable_method Name of the unavailable method.
*/
public function __construct( $unavailable_method = '' ) {
$this->unavailable_method = $unavailable_method;
}

/**
* Availability check
*
* @return bool TRUE when installed
*/
public static function is_available() {
return true;
}

/**
* Caching method as string
*
* @return string Caching method
*/
public static function stringify_method() {
return 'NOOP';
}

/**
* Store item in cache
*
* @param string $hash Hash of the entry.
* @param string $data Content of the entry.
* @param int $lifetime Lifetime of the entry.
* @param bool $sig_detail Show details in signature.
*/
public static function store_item( $hash, $data, $lifetime, $sig_detail ) {
// NOOP.
}

/**
* Read item from cache
*
* @param string $hash Hash of the entry.
*
* @return false No content
*/
public static function get_item( $hash ) {
return false;
}

/**
* Delete item from cache
*
* @param string $hash Hash of the entry.
* @param string $url URL of the entry [optional].
*/
public static function delete_item( $hash, $url = '' ) {
// NOOP.
}

/**
* Clear the cache
*/
public static function clear_cache() {
// NOOP.
}

/**
* Print the cache
*
* @param bool $sig_detail Show details in signature.
* @param array $cache Array of cache values.
*/
public static function print_cache( $sig_detail, $cache ) {
// NOOP.
}

/**
* Get the cache size
*
* @return int Column size
*/
public static function get_stats() {
return 0;
}
}
Loading

0 comments on commit f45e3f9

Please sign in to comment.