PluginProbe
Parse.ly / 3.2.0
Parse.ly v3.2.0
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / RemoteAPI / class-cached-proxy.php

class-cached-proxy.php in Parse.ly 3.2.0, at src/RemoteAPI/class-cached-proxy.php

65 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Parsely Related REST API Caching Decorator
4 *
5 * @package Parsely
6 * @since 3.2.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\RemoteAPI;
12
13 use WP_Error;
14
15 /**
16 * Caching Decorator for the remote /related endpoint.
17 */
18 class Cached_Proxy implements Proxy {
19 private const CACHE_GROUP = 'wp-parsely';
20 private const OBJECT_CACHE_TTL = 5 * MINUTE_IN_SECONDS;
21
22 /**
23 * The Proxy instance this will cache.
24 *
25 * @var Proxy
26 */
27 private $proxy;
28
29 /**
30 * A wrapped object that's compatible with the Cache Interface.
31 *
32 * @var Cache
33 */
34 private $cache;
35
36 /**
37 * Constructor.
38 *
39 * @param Proxy $proxy The Proxy object to cache.
40 * @param Cache $cache An object cache instance.
41 */
42 public function __construct( Proxy $proxy, Cache $cache ) {
43 $this->proxy = $proxy;
44 $this->cache = $cache;
45 }
46
47 /**
48 * Implements caching for the proxy interface.
49 *
50 * @param array<string, mixed> $query The query arguments to send to the remote API.
51 * @return array<string, mixed>|false The response from the remote API, or false if the response is empty.
52 */
53 public function get_items( array $query ) {
54 $cache_key = 'parsely_api_' . wp_hash( (string) wp_json_encode( $this->proxy ) ) . '_' . wp_hash( (string) wp_json_encode( $query ) );
55 $items = $this->cache->get( $cache_key, self::CACHE_GROUP );
56
57 if ( false === $items ) {
58 $items = $this->proxy->get_items( $query );
59 $this->cache->set( $cache_key, $items, self::CACHE_GROUP, self::OBJECT_CACHE_TTL );
60 }
61
62 return $items;
63 }
64 }
65