| @@ -11,8 +11,10 @@ | ||
| 11 | 11 | namespace Parsely\Services; |
| 12 | 12 | |
| 13 | 13 | use WP_Error; |
| 14 | 14 | |
| 15 | +use const Parsely\PARSELY_CACHE_GROUP; | |
| 16 | + | |
| 15 | 17 | /** |
| 16 | 18 | * Cached Service Endpoint class. |
| 17 | 19 | * |
| 18 | 20 | * This class is a wrapper around a service endpoint that caches the response |
| @@ -23,17 +25,8 @@ | ||
| 23 | 25 | * @phpstan-import-type WP_HTTP_Request_Args from Base_Service_Endpoint |
| 24 | 26 | */ |
| 25 | 27 | class Cached_Service_Endpoint extends Base_Service_Endpoint { |
| 26 | 28 | /** |
| 27 | - * The cache group for the API requests. | |
| 28 | - * | |
| 29 | - * @since 3.17.0 | |
| 30 | - * | |
| 31 | - * @var string | |
| 32 | - */ | |
| 33 | - private const CACHE_GROUP = 'wp-parsely'; | |
| 34 | - | |
| 35 | - /** | |
| 36 | 29 | * The service endpoint object. |
| 37 | 30 | * |
| 38 | 31 | * @since 3.17.0 |
| 39 | 32 | * |
| @@ -74,15 +67,11 @@ | ||
| 74 | 67 | * @return string The cache key for the API request. |
| 75 | 68 | */ |
| 76 | 69 | private function get_cache_key( array $args ): string { |
| 77 | 70 | $api_service = $this->service_endpoint->api_service; |
| 71 | + $identifier = sprintf( '%s-%s-%s', $api_service->get_api_url(), $this->get_endpoint(), (string) wp_json_encode( $args ) ); | |
| 78 | 72 | |
| 79 | - $cache_key = 'parsely_api_' . | |
| 80 | - wp_hash( $api_service->get_api_url() ) . '_' . | |
| 81 | - wp_hash( $this->get_endpoint() ) . '_' . | |
| 82 | - wp_hash( (string) wp_json_encode( $args ) ); | |
| 83 | - | |
| 84 | - return $cache_key; | |
| 73 | + return sprintf( 'parsely-api-%s', hash( 'sha256', $identifier ) ); | |
| 85 | 74 | } |
| 86 | 75 | |
| 87 | 76 | /** |
| 88 | 77 | * Executes the API request, caching the response. |
| @@ -96,9 +85,9 @@ | ||
| 96 | 85 | * @return WP_Error|array<mixed> The response from the API. |
| 97 | 86 | */ |
| 98 | 87 | public function call( array $args = array() ) { |
| 99 | 88 | $cache_key = $this->get_cache_key( $args ); |
| 100 | - $cache = wp_cache_get( $cache_key, self::CACHE_GROUP ); | |
| 89 | + $cache = wp_cache_get( $cache_key, PARSELY_CACHE_GROUP ); | |
| 101 | 90 | |
| 102 | 91 | if ( false !== $cache ) { |
| 103 | 92 | // @phpstan-ignore-next-line |
| 104 | 93 | return $cache; |
| @@ -106,9 +95,9 @@ | ||
| 106 | 95 | |
| 107 | 96 | $response = $this->service_endpoint->call( $args ); |
| 108 | 97 | |
| 109 | 98 | if ( ! is_wp_error( $response ) ) { |
| 110 | - wp_cache_set( $cache_key, $response, self::CACHE_GROUP, $this->cache_ttl ); // phpcs:ignore | |
| 99 | + wp_cache_set( $cache_key, $response, PARSELY_CACHE_GROUP, $this->cache_ttl ); // phpcs:ignore | |
| 111 | 100 | } |
| 112 | 101 | |
| 113 | 102 | return $response; |
| 114 | 103 | } |