Use wp_cache_* instead of get|set_transient (#218)

This commit is contained in:
Andy Fragen 2025-08-15 16:22:14 -07:00 committed by GitHub
parent 37c9285c28
commit 6ca4e6e73b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -15,6 +15,7 @@ use WP_Upgrader;
const SERVICE_ID = 'FairPackageManagementRepo'; const SERVICE_ID = 'FairPackageManagementRepo';
const CONTENT_TYPE = 'application/json+fair'; const CONTENT_TYPE = 'application/json+fair';
const CACHE_KEY = 'fair-packages-';
const CACHE_LIFETIME = 12 * HOUR_IN_SECONDS; const CACHE_LIFETIME = 12 * HOUR_IN_SECONDS;
const RELEASE_PACKAGES_CACHE_KEY = 'fair-release-packages'; const RELEASE_PACKAGES_CACHE_KEY = 'fair-release-packages';
@ -83,7 +84,7 @@ function get_did_hash( string $id ) {
* @return DIDDocument|WP_Error * @return DIDDocument|WP_Error
*/ */
function get_did_document( string $id ) { function get_did_document( string $id ) {
$cached = get_site_transient( $id ); $cached = wp_cache_get( $id, 'did-docs' );
if ( $cached ) { if ( $cached ) {
return $cached; return $cached;
} }
@ -98,7 +99,7 @@ function get_did_document( string $id ) {
if ( is_wp_error( $document ) ) { if ( is_wp_error( $document ) ) {
return $document; return $document;
} }
set_site_transient( $id, $document, CACHE_LIFETIME ); wp_cache_set( $id, $document, 'did-docs', CACHE_LIFETIME );
return $document; return $document;
} }
@ -142,8 +143,8 @@ function fetch_package_metadata( string $id ) {
* @return MetadataDocument|WP_Error * @return MetadataDocument|WP_Error
*/ */
function fetch_metadata_doc( string $url ) { function fetch_metadata_doc( string $url ) {
$cache_key = md5( $url ); $cache_key = CACHE_KEY . md5( $url );
$response = get_site_transient( $cache_key ); $response = wp_cache_get( $cache_key, 'metadata-docs' );
if ( ! $response ) { if ( ! $response ) {
$response = wp_remote_get( $url, [ $response = wp_remote_get( $url, [
@ -158,7 +159,7 @@ function fetch_metadata_doc( string $url ) {
} elseif ( $code !== 200 ) { } elseif ( $code !== 200 ) {
return new WP_Error( 'fair.packages.metadata.failure', __( 'HTTP error code received', 'fair' ) ); return new WP_Error( 'fair.packages.metadata.failure', __( 'HTTP error code received', 'fair' ) );
} }
set_site_transient( $cache_key, $response, CACHE_LIFETIME ); wp_cache_set( $cache_key, $response, 'metadata-docs', CACHE_LIFETIME );
} }
return MetadataDocument::from_response( $response ); return MetadataDocument::from_response( $response );