Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common interface for the Cachify storage classes #298

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ node_modules/
composer.lock
package-lock.json
phpunit.*.xml
tests/coverage/
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_APC', 'Cachify_DB', 'Cachify_HDD', 'Cachify_MEMCACHED', 'Cachify_REDIS', 'Cachify_CLI' ), true ) ) {
if ( in_array( $class_name, array( 'Cachify', 'Cachify_APC', 'Cachify_Backend', 'Cachify_CLI', 'Cachify_DB', 'Cachify_HDD', 'Cachify_MEMCACHED', 'Cachify_REDIS' ), true ) ) {
require_once sprintf(
'%s/inc/class-%s.php',
CACHIFY_DIR,
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
],
"test": [
"phpunit"
],
"coverage": [
"php -d xdebug.mode=coverage vendor/bin/phpunit --coverage-html tests/coverage"
stklcode marked this conversation as resolved.
Show resolved Hide resolved
]
},
"config": {
Expand Down
7 changes: 5 additions & 2 deletions inc/class-cachify-apc.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Cachify_APC
*/
final class Cachify_APC {
final class Cachify_APC implements Cachify_Backend {

/**
* Availability check
Expand Down Expand Up @@ -102,9 +102,12 @@ public static function clear_cache() {
/**
* Print the cache
*
* @param bool $sig_detail Show details in signature.
* @param string $cache Cached content.
*
* @since 2.0
*/
public static function print_cache() {
public static function print_cache( $sig_detail, $cache ) {
// Not supported.
}

Expand Down
69 changes: 69 additions & 0 deletions inc/class-cachify-backend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Interface for caching storage classes.
*
* @package Cachify
*/

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

/**
* Interface Cachify Backend
*/
interface Cachify_Backend {

/**
* Availability check
*
* @return bool TRUE if
* available
*/
public static function is_available();

/**
* Caching method as string
*
* @return string Caching method
*/
public static function stringify_method();

/**
* 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 );

/**
* Read item from cache
*
* @param string $hash Hash of the entry.
* @return mixed Content of the entry.
*/
public static function get_item( $hash );

/**
* Delete item from cache
*
* @param string $hash Hash of the entry.
* @param string $url URL of the entry.
*/
public static function delete_item( $hash, $url );

/**
* Clear the cache
*/
public static function clear_cache();

/**
* Print the cache
*
* @param bool $sig_detail Show details in signature.
* @param string $cache Cached content.
*/
public static function print_cache( $sig_detail, $cache );
}
5 changes: 3 additions & 2 deletions inc/class-cachify-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Cachify_DB
*/
final class Cachify_DB {
final class Cachify_DB implements Cachify_Backend {

/**
* Availability check
Expand Down Expand Up @@ -41,10 +41,11 @@ public static function stringify_method() {
* @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.
*
* @since 2.0
*/
public static function store_item( $hash, $data, $lifetime ) {
public static function store_item( $hash, $data, $lifetime, $sig_detail ) {
/* Do not store empty data. */
if ( empty( $data ) ) {
trigger_error( __METHOD__ . ': Empty input.', E_USER_WARNING );
Expand Down
10 changes: 7 additions & 3 deletions inc/class-cachify-hdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Cachify_HDD
*/
final class Cachify_HDD {
final class Cachify_HDD implements Cachify_Backend {

/**
* Availability check
Expand Down Expand Up @@ -79,11 +79,12 @@ public static function store_item( $hash, $data, $lifetime, $sig_detail ) {
/**
* Read item from cache
*
* @param string $hash Hash of the entry.
* @return bool True if cache is present.
*
* @since 2.0
*/
public static function get_item() {
public static function get_item( $hash ) {
return is_readable(
self::_file_html()
);
Expand Down Expand Up @@ -118,9 +119,12 @@ public static function clear_cache() {
/**
* Print the cache
*
* @param bool $sig_detail Show details in signature.
* @param array $cache Array of cache values.
*
* @since 2.0
*/
public static function print_cache() {
public static function print_cache( $sig_detail, $cache ) {
$filename = self::_file_html();
$size = is_readable( $filename ) ? readfile( $filename ) : false;

Expand Down
7 changes: 5 additions & 2 deletions inc/class-cachify-memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Cachify_MEMCACHED
*/
final class Cachify_MEMCACHED {
final class Cachify_MEMCACHED implements Cachify_Backend {

/**
* Memcached-Object
Expand Down Expand Up @@ -140,9 +140,12 @@ public static function clear_cache() {
/**
* Print the cache
*
* @param bool $sig_detail Show details in signature.
* @param array $cache Array of cache values.
*
* @since 2.0.7
*/
public static function print_cache() {
public static function print_cache( $sig_detail, $cache ) {
// Not supported.
}

Expand Down
2 changes: 1 addition & 1 deletion inc/class-cachify-redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @since 2.4
*/
final class Cachify_REDIS {
final class Cachify_REDIS implements Cachify_Backend {

/**
* Redis-Object
Expand Down
6 changes: 6 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
<file>cachify.php</file>
<directory suffix=".php">inc</directory>
</include>
<exclude>
<directory suffix=".php">inc/setup/</directory>
<file>inc/cachify.settings.php</file>
<file>inc/cachify.settings-footer.php</file>
</exclude>

<report>
<clover outputFile="phpunit.coverage.xml"/>
</report>
Expand Down
6 changes: 4 additions & 2 deletions tests/test-cachify-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function test_caching() {
Cachify_DB::store_item(
'965b4abf2414e45036ab90c9d3f8dbc7',
'<html><head><title>Test Me</title></head><body><p>Test Content.</p></body></html>',
3600
3600,
false
);

$cached = Cachify_DB::get_item( '965b4abf2414e45036ab90c9d3f8dbc7' );
Expand All @@ -53,7 +54,8 @@ public function test_caching() {
Cachify_DB::store_item(
'ef7e4a0540f6cde19e6eb658c69b0064',
'<html><head><title>Test 2</title></head><body><p>Test Content #2.</p></body></html>',
3600
3600,
false
);
self::assertIsArray( Cachify_DB::get_item( 'ef7e4a0540f6cde19e6eb658c69b0064' ), 'second item was not stored' );

Expand Down
8 changes: 4 additions & 4 deletions tests/test-cachify-hdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function ( $original ) use ( &$capture ) {
* Test the actual caching.
*/
public function test_caching() {
self::assertFalse( Cachify_HDD::get_item() );
self::assertFalse( Cachify_HDD::get_item( '965b4abf2414e45036ab90c9d3f8dbc7' ) );

self::go_to( '/testme/' );
Cachify_HDD::store_item(
Expand All @@ -61,7 +61,7 @@ public function test_caching() {
3600, // Ignored.
false
);
self::assertTrue( Cachify_HDD::get_item() );
self::assertTrue( Cachify_HDD::get_item( '965b4abf2414e45036ab90c9d3f8dbc7' ) );
self::assertTrue( is_file( CACHIFY_CACHE_DIR . DIRECTORY_SEPARATOR . 'example.org/testme/index.html' ) );
$cached = file_get_contents( CACHIFY_CACHE_DIR . DIRECTORY_SEPARATOR . 'example.org/testme/index.html' );
self::assertStringStartsWith(
Expand All @@ -81,7 +81,7 @@ public function test_caching() {
3600, // Ignored.
false
);
self::assertTrue( Cachify_HDD::get_item() );
self::assertTrue( Cachify_HDD::get_item( '965b4abf2414e45036ab90c9d3f8dbc7' ) );
self::assertTrue( is_file( CACHIFY_CACHE_DIR . DIRECTORY_SEPARATOR . 'example.org/testme/sub/index.html' ) );

// Another item.
Expand Down Expand Up @@ -118,6 +118,6 @@ public function test_caching() {
Cachify_HDD::clear_cache();
self::assertFalse( is_dir( CACHIFY_CACHE_DIR . DIRECTORY_SEPARATOR . 'example.org/testme' ), 'empty directory was not deleted' );
self::assertFalse( is_dir( CACHIFY_CACHE_DIR . DIRECTORY_SEPARATOR . 'example.org/test2' ), 'second test page was not deleted' );
self::assertFalse( Cachify_HDD::get_item() );
self::assertFalse( Cachify_HDD::get_item( '965b4abf2414e45036ab90c9d3f8dbc7' ) );
}
}
27 changes: 27 additions & 0 deletions tests/test-cachify-redis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Cachify Redis tests.
*
* @package Cachify
*/

/**
* Class Test_Cachify_REDIS.
*
* Tests for the Cachify Redis backend.
*/
class Test_Cachify_REDIS extends WP_UnitTestCase {
/**
* Test backend availability.
*/
public function test_is_available() {
self::assertEquals( class_exists( 'Redis' ), Cachify_REDIS::is_available() );
}

/**
* Test backend availability.
*/
public function test_stringify_method() {
self::assertEquals( 'Redis', Cachify_REDIS::stringify_method() );
}
}