Skip to content

Commit

Permalink
feat: implement caching mechanism with Transient_Cache class and inte…
Browse files Browse the repository at this point in the history
…grate it into Updater
  • Loading branch information
Dmitry Kokorin committed Nov 12, 2024
1 parent d8114a9 commit c4bd61c
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bdev/wp-toolkit-builder",
"version": "1.0.1",
"version": "1.0.2",
"description": "Wordpress toolkit classes library",
"type": "library",
"license": "gpl-2.0-or-later",
Expand Down
96 changes: 96 additions & 0 deletions src/cache/class-transient-cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* Transient Cache Implementation
*
* This file contains the Transient_Cache class which implements caching using WordPress transients.
*
* @package Bdev
* @subpackage Cache
* @since 1.0.2
* @version 1.0.0
* @license GPL-2.0-or-later
* @author BuzzDeveloper
*/

namespace Bdev\Cache;

use Bdev\Cache\Interfaces\Cache_Interface;

/**
* Transient_Cache class
*
* Implements caching using WordPress transients.
*/
class Transient_Cache implements Cache_Interface {

/**
* Check whether data accociated with a key
*
* @param string $key The key associated with the cached data.
* @return boolean
*/
public function is_cached( string $key ): bool {
return get_transient( $key ) ? true : false;
}

/**
* Store data in the cache
*
* @param string $key The key associated with the cached data.
* @param mixed $data The data to be cached.
* @param int $expiration The expiration time in seconds.
* @return Cache_Interface
*/
public function store( string $key, mixed $data, int $expiration = 0 ): Cache_Interface {
set_transient( $key, $data, $expiration );
return $this;
}

/**
* Retrieve cached data by its key
*
* @param string $key The key associated with the cached data.
* @return mixed
*/
public function retrieve( string $key ): mixed {
return get_transient( $key );
}

/**
* Retrieve all cached data
*
* @return array<string, mixed>
*/
public function retrieve_all(): array {
return array();
}

/**
* Erase cached entry by its key
*
* @param string $key The key associated with the cached data.
* @return Cache_Interface
*/
public function erase( string $key ): Cache_Interface {
delete_transient( $key );
return $this;
}

/**
* Erase all expired entries
*
* @return int
*/
public function erase_expired(): int {
return 0;
}

/**
* Erase all cached entries
*
* @return Cache_Interface
*/
public function erase_all(): Cache_Interface {
return $this;
}
}
79 changes: 79 additions & 0 deletions src/cache/interfaces/interface-cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Interface Cache
*
* This interface defines the contract for caching mechanisms used in the application.
* Implementations of this interface should provide methods to store, retrieve, and manage cached data.
*
* @package Bdev
* @subpackage Cache
* @since 1.0.2
* @version 1.0.0
* @license GPL-2.0-or-later
* @author BuzzDeveloper
*/

namespace Bdev\Cache\Interfaces;

/**
* Interface for cache operations.
*
* @since 1.0.2
*/
interface Cache_Interface {

/**
* Check whether data accociated with a key
*
* @param string $key The key associated with the cached data.
* @return boolean
*/
public function is_cached( string $key ): bool;

/**
* Store data in the cache
*
* @param string $key The key associated with the cached data.
* @param mixed $data The data to be cached.
* @param int $expiration The expiration time in seconds.
* @return Cache_Interface
*/
public function store( string $key, mixed $data, int $expiration = 0 ): Cache_Interface;

/**
* Retrieve cached data by its key
*
* @param string $key The key associated with the cached data.
* @return mixed
*/
public function retrieve( string $key ): mixed;

/**
* Retrieve all cached data
*
* @return array<string, mixed>
*/
public function retrieve_all(): array;

/**
* Erase cached entry by its key
*
* @param string $key The key associated with the cached data.
* @return Cache_Interface
*/
public function erase( string $key ): Cache_Interface;

/**
* Erase all expired entries
*
* @return int
*/
public function erase_expired(): int;

/**
* Erase all cached entries
*
* @return Cache_Interface
*/
public function erase_all(): Cache_Interface;
}
40 changes: 37 additions & 3 deletions src/updater/class-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace Bdev\Updater;

use Bdev\Cache\Interfaces\Cache_Interface;
use Bdev\Updater\Interfaces\Update_Info_Interface;
use Bdev\Settings\Interfaces\WP_Info_Interface;
use Bdev\Updater\Interfaces\Updater_Interface;
Expand All @@ -41,17 +42,26 @@ class Updater implements Updater_Interface {
*/
private WP_Info_Interface $wp_info;

/**
* Cache instance.
*
* @var Cache_Interface
*/
private Cache_Interface $cache;

/**
* Updater constructor.
*
* Initializes the Updater with the provided data provider and update info instances.
* Initializes the Updater with the provided data provider, update info, and cache instances.
*
* @param Update_Info_Interface $update_info Update info instance.
* @param WP_Info_Interface $wp_info WordPress info instance.
* @param Cache_Interface $cache Cache instance.
*/
public function __construct( Update_Info_Interface $update_info, WP_Info_Interface $wp_info ) {
public function __construct( Update_Info_Interface $update_info, WP_Info_Interface $wp_info, Cache_Interface $cache ) {
$this->update_info = $update_info;
$this->wp_info = $wp_info;
$this->cache = $cache;
}

/**
Expand Down Expand Up @@ -90,6 +100,24 @@ public function get_wp_info_provider(): WP_Info_Interface {
return $this->wp_info;
}

/**
* Sets the cache instance.
*
* @param Cache_Interface $cache Cache instance.
*/
public function set_cache_provider( Cache_Interface $cache ): void {
$this->cache = $cache;
}

/**
* Gets the cache instance.
*
* @return Cache_Interface Cache instance.
*/
public function get_cache_provider(): Cache_Interface {
return $this->cache;
}

/**
* Prepares the update.
*
Expand Down Expand Up @@ -117,7 +145,13 @@ public function prepare_update( \stdClass $transient ): \stdClass {
*/
protected function check_for_updates(): array {
$update_info = array();
$update_data = $this->get_update_info_provider()->get_update_info();
$cache_key = $this->get_update_info_provider()->get_update_id() . $this->get_wp_info_provider()->get_version();
if ( $this->get_cache_provider()->is_cached( $cache_key ) ) {
$update_data = $this->get_cache_provider()->retrieve( $cache_key );
} else {
$update_data = $this->get_update_info_provider()->get_update_info();
$this->get_cache_provider()->store( $cache_key, $update_data, HOUR_IN_SECONDS );
}

if ( version_compare( (string) $update_data['new_version'], $this->get_wp_info_provider()->get_version(), '>' )
&& version_compare( $update_data['requires'] ?? '', $this->get_wp_info_provider()->get_requires(), '<' )
Expand Down

0 comments on commit c4bd61c

Please sign in to comment.