# wp-parsely/3.2.0/src/RemoteAPI/class-cached-proxy.php

Parse.ly, version 3.2.0. 65 lines.

- Page: https://pluginprobe.com/plugins/wp-parsely/3.2.0/code/src/RemoteAPI/class-cached-proxy.php
- Raw: https://pluginprobe.com/plugins/wp-parsely/3.2.0/raw/src/RemoteAPI/class-cached-proxy.php
- Modified: 2022-03-29T08:06:20+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wp-parsely/3.2.0/code/src/RemoteAPI/class-cached-proxy.php#L10-L20`.

```php
<?php
/**
 * Parsely Related REST API Caching Decorator
 *
 * @package Parsely
 * @since 3.2.0
 */

declare(strict_types=1);

namespace Parsely\RemoteAPI;

use WP_Error;

/**
 * Caching Decorator for the remote /related endpoint.
 */
class Cached_Proxy implements Proxy {
	private const CACHE_GROUP      = 'wp-parsely';
	private const OBJECT_CACHE_TTL = 5 * MINUTE_IN_SECONDS;

	/**
	 * The Proxy instance this will cache.
	 *
	 * @var Proxy
	 */
	private $proxy;

	/**
	 * A wrapped object that's compatible with the Cache Interface.
	 *
	 * @var Cache
	 */
	private $cache;

	/**
	 * Constructor.
	 *
	 * @param Proxy $proxy The Proxy object to cache.
	 * @param Cache $cache An object cache instance.
	 */
	public function __construct( Proxy $proxy, Cache $cache ) {
		$this->proxy = $proxy;
		$this->cache = $cache;
	}

	/**
	 * Implements caching for the proxy interface.
	 *
	 * @param array<string, mixed> $query The query arguments to send to the remote API.
	 * @return array<string, mixed>|false The response from the remote API, or false if the response is empty.
	 */
	public function get_items( array $query ) {
		$cache_key = 'parsely_api_' . wp_hash( (string) wp_json_encode( $this->proxy ) ) . '_' . wp_hash( (string) wp_json_encode( $query ) );
		$items     = $this->cache->get( $cache_key, self::CACHE_GROUP );

		if ( false === $items ) {
			$items = $this->proxy->get_items( $query );
			$this->cache->set( $cache_key, $items, self::CACHE_GROUP, self::OBJECT_CACHE_TTL );
		}

		return $items;
	}
}

```
