embed-privacy icon indicating copy to clipboard operation
embed-privacy copied to clipboard

Add possibility to cache embeds

Open MatzeKitt opened this issue 2 years ago • 1 comments

What feature are you requesting?

Replacing content may be expensive, thus the result should be cacheable so that it won’t happen on every page request.

Cache key by crc32 for performance.

Why is this feature necessary?

Caching improves performance.

Are you expecing side-effects?

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

MatzeKitt avatar May 18 '23 16:05 MatzeKitt

Fragment Caching

Base:

<?php
namespace epiphyt\Embed_Privacy\data;

/**
 * Embed cache related functionality.
 * 
 * @author	Epiphyt
 * @license	GPL2
 * @package	epiphyt\Embed_Privacy
 * @since	1.11.0
 */
final class Embed_Cache {
	/**
	 * Get an embed cache entry.
	 * 
	 * @param	string	$url Embed URL
	 * @return	mixed Embed cache entry
	 */
	public static function get( $url ) {
		return \get_transient( self::get_key( $url ) );
	}
	
	/**
	 * Get an embed cache key.
	 * 
	 * @param	string	$url Embed URL
	 * @return	string Embed cache key
	 */
	public static function get_key( $url ) {
		return 'embed_privacy_embed_' . \md5( $url );
	}
	
	/**
	 * Set an embed cache entry.
	 * 
	 * @param	string	$url Embed URL
	 * @param	mixed	$data Data to cache
	 * @return	bool Whether the value was set
	 */
	public static function set( $url, $data ) {
		return \set_transient( self::get_key( $url ), $data, \WEEK_IN_SECONDS );
	}
}

MatzeKitt avatar May 09 '25 20:05 MatzeKitt