| Server IP : 103.88.176.108 / Your IP : 216.73.216.211 Web Server : Apache/2.4.41 (Ubuntu) System : Linux webserver 5.4.0-42-generic #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020 x86_64 User : www-data ( 33) PHP Version : 7.4.3-4ubuntu2.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/wp-content/plugins/optimole-wp/inc/ |
Upload File : |
<?php
/**
* Class Optml_Attachment_Cache.
*/
class Optml_Attachment_Cache {
const CACHE_GROUP = 'om_att';
/**
* Local cache map.
*
* @var array
*/
private static $cache_map = [];
/**
* Reset the memory cache.
*/
public static function reset() {
self::$cache_map = [];
}
/**
* Get the cached attachment ID.
*
* @param string $url the URL of the attachment.
*
* @return bool|mixed
*/
public static function get_cached_attachment_id( $url ) {
// We cache also in memory to avoid calling DB every time when not using Object Cache.
$cache_key = self::get_cache_key( $url );
if ( isset( self::$cache_map[ $cache_key ] ) ) {
return self::$cache_map[ $cache_key ];
}
$value = wp_using_ext_object_cache()
? wp_cache_get( $cache_key, self::CACHE_GROUP )
: get_transient( self::CACHE_GROUP . $cache_key );
self::$cache_map[ $cache_key ] = $value;
return $value;
}
/**
* Set the cached attachment ID.
*
* @param string $url the URL of the attachment.
* @param int $id the attachment ID.
*
* @return void
*/
public static function set_cached_attachment_id( $url, $id ) {
$cache_key = self::get_cache_key( $url );
// We cache also in memory to avoid calling DB every time when not using Object Cache.
self::$cache_map[ $cache_key ] = $id;
// If the ID is not found we cache for 10 minutes, otherwise for a week.
// We try to reduce the cache time when is not found to
// avoid caching for situation when this might be temporary.
$expiration = $id === 0 ? ( 10 * MINUTE_IN_SECONDS ) : WEEK_IN_SECONDS;
wp_using_ext_object_cache()
? wp_cache_set( $cache_key, $id, self::CACHE_GROUP, $expiration )
: set_transient( self::CACHE_GROUP . $cache_key, $id, $expiration );
}
/**
* Generate cache key for URL.
*
* @param string $url the URL to generate the cache key for.
*
* @return string
*/
private static function get_cache_key( $url ) {
$url = strtok( $url, '?' );
return 'id_' . crc32( $url );
}
}