Файловый менеджер - Редактировать - /home/tuudkjt/globeasy/wp-includes/ID3/Caching.tar
Назад
CacheNameSpaceTrait.php 0000777 00000004100 15251736434 0011055 0 ustar 00 <?php namespace Automattic\WooCommerce\Caching; /** * Implements namespacing algorithm to simulate grouping and namespacing for wp_cache, memcache and other caching engines that don't support grouping natively. * * See the algorithm details here: https://github.com/memcached/memcached/wiki/ProgrammingTricks#namespacing. * * To use the namespacing algorithm in the CacheEngine class: * 1. Use a group string to identify all objects of a type. * 2. Before setting cache, prefix the cache key by using the `get_cache_prefix`. * 3. Use `invalidate_cache_group` function to invalidate all caches in entire group at once. */ trait CacheNameSpaceTrait { /** * Get prefix for use with wp_cache_set. Allows all cache in a group to be invalidated at once. * * @param string $group Group of cache to get. * @return string Prefix. */ public static function get_cache_prefix( $group ) { // Get cache key - uses cache key wc_orders_cache_prefix to invalidate when needed. $prefix = wp_cache_get( 'wc_' . $group . '_cache_prefix', $group ); if ( false === $prefix ) { $prefix = microtime(); wp_cache_set( 'wc_' . $group . '_cache_prefix', $prefix, $group ); } return 'wc_cache_' . $prefix . '_'; } /** * Increment group cache prefix (invalidates cache). * * @param string $group Group of cache to clear. */ public static function incr_cache_prefix( $group ) { wc_deprecated_function( 'WC_Cache_Helper::incr_cache_prefix', '3.9.0', 'WC_Cache_Helper::invalidate_cache_group' ); self::invalidate_cache_group( $group ); } /** * Invalidate cache group. * * @param string $group Group of cache to clear. * @since 3.9.0 */ public static function invalidate_cache_group( $group ) { return wp_cache_set( 'wc_' . $group . '_cache_prefix', microtime(), $group ); } /** * Helper method to get prefixed key. * * @param string $key Key to prefix. * @param string $group Group of cache to get. * * @return string Prefixed key. */ public static function get_prefixed_key( $key, $group ) { return self::get_cache_prefix( $group ) . $key; } } CacheException.php 0000777 00000004321 15251736434 0010160 0 ustar 00 <?php namespace Automattic\WooCommerce\Caching; /** * Exception thrown by classes derived from ObjectCache. */ class CacheException extends \Exception { /** * Error messages. * * @var array */ private $errors; /** * The object that threw the exception. * * @var ObjectCache */ private $thrower; /** * The id of the cached object, if available. * * @var int|string|null */ private $cached_id; /** * Creates a new instance of the class. * * @param string $message The exception message. * @param ObjectCache $thrower The object that is throwing the exception. * @param int|string|null $cached_id The involved cached object id, if available. * @param array|null $errors An array of error messages, if available. * @param mixed $code An error code, if available. * @param \Throwable|null $previous The previous exception, if available. */ public function __construct( string $message, ObjectCache $thrower, $cached_id = null, ?array $errors = null, $code = 0, ?\Throwable $previous = null ) { $this->errors = $errors ?? array(); $this->thrower = $thrower; $this->cached_id = $cached_id; parent::__construct( $message, $code, $previous ); } /** * Get a string representation of the exception object. * * @return string String representation of the exception object. */ public function __toString(): string { $cached_id_part = $this->cached_id ? ", id: {$this->cached_id}" : ''; return "CacheException: [{$this->thrower->get_object_type()}{$cached_id_part}]: {$this->message}"; } /** * Gets the array of error messages passed to the exception constructor. * * @return array Error messages passed to the exception constructor. */ public function get_errors(): array { return $this->errors; } /** * Gets the object that threw the exception as passed to the exception constructor. * * @return object The object that threw the exception. */ public function get_thrower(): object { return $this->thrower; } /** * Gets the id of the cached object as passed to the exception constructor. * * @return int|string|null The id of the cached object. */ public function get_cached_id() { return $this->cached_id; } } ObjectCache.php 0000777 00000025727 15251736434 0007445 0 ustar 00 <?php namespace Automattic\WooCommerce\Caching; /** * Base class for caching objects (or associative arrays) that have a unique identifier. * At the very least, derived classes need to implement the 'get_object_type' method, * but usually it will be convenient to override some of the other protected members. * * The actual caching is delegated to an instance of CacheEngine. By default WpCacheEngine is used, * but a different engine can be used by either overriding the get_cache_engine_instance method * or capturing the wc_object_cache_get_engine filter. * * Objects are identified by ids that are either integers or strings. The actual cache keys passed * to the cache engine will be prefixed with the object type and a random string. The 'flush' operation * just forces the generation a new prefix and lets the old cached objects expire. */ abstract class ObjectCache { /** * Expiration value to be passed to 'set' to use the value of $default_expiration. */ public const DEFAULT_EXPIRATION = -1; /** * Maximum expiration time value, in seconds, that can be passed to 'set'. */ public const MAX_EXPIRATION = MONTH_IN_SECONDS; /** * This needs to be set in each derived class. * * @var string */ private $object_type; /** * Default value for the duration of the objects in the cache, in seconds * (may not be used depending on the cache engine used WordPress cache implementation). * * @var int */ protected $default_expiration = HOUR_IN_SECONDS; /** * Temporarily used when retrieving data in 'get'. * * @var array */ private $last_cached_data; /** * The cache engine to use. * * @var ?CacheEngine */ private $cache_engine = null; /** * Gets an identifier for the types of objects cached by this class. * This identifier will be used to compose the keys passed to the cache engine. * It must be unique for each class inheriting from ObjectCache. * * @return string */ abstract public function get_object_type(): string; /** * Creates a new instance of the class. * * @throws CacheException If get_object_type returns null or an empty string. */ public function __construct() { $this->object_type = $this->get_object_type(); if ( empty( $this->object_type ) ) { throw new CacheException( 'Class ' . get_class( $this ) . ' returns an empty value for get_object_type', $this ); } } /** * Get the default expiration time for cached objects, in seconds. * * @return int */ public function get_default_expiration_value(): int { return $this->default_expiration; } /** * Get the cache engine to use and cache it internally. * * @return CacheEngine */ private function get_cache_engine(): CacheEngine { if ( null === $this->cache_engine ) { $engine = $this->get_cache_engine_instance(); /** * Filters the underlying cache engine to be used by an instance of ObjectCache. * * @since 7.4.0 * * @param CacheEngine $engine The cache engine to be used by default. * @param ObjectCache $cache_instance The instance of ObjectCache that will use the cache engine. * @returns CacheEngine The actual cache engine that will be used. */ $this->cache_engine = apply_filters( 'wc_object_cache_get_engine', $engine, $this ); } return $this->cache_engine; } /** * Add an object to the cache, or update an already cached object. * * @param object|array $object The object to be cached. * @param int|string|null $id Id of the object to be cached, if null, get_object_id will be used to get it. * @param int $expiration Expiration of the cached data in seconds from the current time, or DEFAULT_EXPIRATION to use the default value. * @return bool True on success, false on error. * @throws CacheException Invalid parameter, or null id was passed and get_object_id returns null too. */ public function set( $object, $id = null, int $expiration = self::DEFAULT_EXPIRATION ): bool { if ( null === $object ) { throw new CacheException( "Can't cache a null value", $this, $id ); } if ( ! is_array( $object ) && ! is_object( $object ) ) { throw new CacheException( "Can't cache a non-object, non-array value", $this, $id ); } if ( ! is_string( $id ) && ! is_int( $id ) && ! is_null( $id ) ) { throw new CacheException( "Object id must be an int, a string, or null for 'set'", $this, $id ); } $this->verify_expiration_value( $expiration ); $errors = $this->validate( $object ); if ( ! is_null( $errors ) ) { try { $id = $this->get_id_from_object_if_null( $object, $id ); } catch ( \Throwable $ex ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch // Nothing else to do, we won't be able to add any significant object id to the CacheException and that's it. } if ( count( $errors ) === 1 ) { throw new CacheException( 'Object validation/serialization failed: ' . $errors[0], $this, $id, $errors ); } elseif ( ! empty( $errors ) ) { throw new CacheException( 'Object validation/serialization failed', $this, $id, $errors ); } } $id = $this->get_id_from_object_if_null( $object, $id ); $this->last_cached_data = $object; return $this->get_cache_engine()->cache_object( $id, $object, self::DEFAULT_EXPIRATION === $expiration ? $this->default_expiration : $expiration, $this->get_object_type() ); } /** * Update an object in the cache, but only if an object is already cached with the same id. * * @param object|array $object The new object that will replace the already cached one. * @param int|string|null $id Id of the object to be cached, if null, get_object_id will be used to get it. * @param int $expiration Expiration of the cached data in seconds from the current time, or DEFAULT_EXPIRATION to use the default value. * @return bool True on success, false on error or if no object with the supplied id was cached. * @throws CacheException Invalid parameter, or null id was passed and get_object_id returns null too. */ public function update_if_cached( $object, $id = null, int $expiration = self::DEFAULT_EXPIRATION ): bool { $id = $this->get_id_from_object_if_null( $object, $id ); if ( ! $this->is_cached( $id ) ) { return false; } return $this->set( $object, $id, $expiration ); } /** * Get the id from an object if the id itself is null. * * @param object|array $object The object to get the id from. * @param int|string|null $id An object id or null. * * @return int|string|null Passed $id if it wasn't null, otherwise id obtained from $object using get_object_id. * * @throws CacheException Passed $id is null and get_object_id returned null too. */ private function get_id_from_object_if_null( $object, $id ) { if ( null === $id ) { $id = $this->get_object_id( $object ); if ( null === $id ) { throw new CacheException( "Null id supplied and the cache class doesn't implement get_object_id", $this ); } } return $id; } /** * Check if the given expiration time value is valid, throw an exception if not. * * @param int $expiration Expiration time to check. * @return void * @throws CacheException Expiration time is negative or higher than MAX_EXPIRATION. */ private function verify_expiration_value( int $expiration ): void { if ( self::DEFAULT_EXPIRATION !== $expiration && ( ( $expiration < 1 ) || ( $expiration > self::MAX_EXPIRATION ) ) ) { throw new CacheException( 'Invalid expiration value, must be ObjectCache::DEFAULT_EXPIRATION or a value between 1 and ObjectCache::MAX_EXPIRATION', $this ); } } /** * Retrieve a cached object, and if no object is cached with the given id, * try to get one via get_from_datastore method or by supplying a callback and then cache it. * * If you want to provide a callable but still use the default expiration value, * pass "ObjectCache::DEFAULT_EXPIRATION" as the second parameter. * * @param int|string $id The id of the object to retrieve. * @param int $expiration Expiration of the cached data in seconds from the current time, used if an object is retrieved from datastore and cached. * @param callable|null $get_from_datastore_callback Optional callback to get the object if it's not cached, it must return an object/array or null. * @return object|array|null Cached object, or null if it's not cached and can't be retrieved from datastore or via callback. * @throws CacheException Invalid id parameter. */ public function get( $id, int $expiration = self::DEFAULT_EXPIRATION, ?callable $get_from_datastore_callback = null ) { if ( ! is_string( $id ) && ! is_int( $id ) ) { throw new CacheException( "Object id must be an int or a string for 'get'", $this ); } $this->verify_expiration_value( $expiration ); $data = $this->get_cache_engine()->get_cached_object( $id, $this->get_object_type() ); if ( null === $data ) { $object = null; if ( $get_from_datastore_callback ) { $object = $get_from_datastore_callback( $id ); } if ( null === $object ) { return null; } $this->set( $object, $id, $expiration ); $data = $this->last_cached_data; } return $data; } /** * Remove an object from the cache. * * @param int|string $id The id of the object to remove. * @return bool True if the object is removed from the cache successfully, false otherwise (because the object wasn't cached or for other reason). */ public function remove( $id ): bool { return $this->get_cache_engine()->delete_cached_object( $id, $this->get_object_type() ); } /** * Remove all the objects from the cache. * * @return bool True on success, false on error. */ public function flush(): bool { return $this->get_cache_engine()->delete_cache_group( $this->get_object_type() ); } /** * Is a given object cached? * * @param int|string $id The id of the object to check. * @return bool True if there's a cached object with the specified id. */ public function is_cached( $id ): bool { return $this->get_cache_engine()->is_cached( $id, $this->get_object_type() ); } /** * Get the id of an object. This is used by 'set' when a null id is passed. * If the object id can't be determined the method must return null. * * @param array|object $object The object to get the id for. * @return int|string|null */ abstract protected function get_object_id( $object ); /** * Validate an object before it's cached. * * @param array|object $object Object to validate. * @return array|null An array with validation error messages, null or an empty array if there are no errors. */ abstract protected function validate( $object ): ?array; /** * Get the instance of the cache engine to use. * * @return CacheEngine */ protected function get_cache_engine_instance(): CacheEngine { return wc_get_container()->get( WPCacheEngine::class ); } /** * Get a random string to be used to compose the cache key prefix. * It should return a different string each time. * * @return string */ protected function get_random_string(): string { return dechex( microtime( true ) * 1000 ) . bin2hex( random_bytes( 8 ) ); } } CacheEngine.php 0000777 00000004225 15251736434 0007432 0 ustar 00 <?php namespace Automattic\WooCommerce\Caching; /** * Interface for cache engines used by objects inheriting from ObjectCache. * Here "object" means either an array or an actual PHP object. */ interface CacheEngine { /** * Retrieves an object cached under a given key. * * @param string $key They key under which the object to retrieve is cached. * @param string $group The group under which the object is cached. * * @return array|object|null The cached object, or null if there's no object cached under the passed key. */ public function get_cached_object( string $key, string $group = '' ); /** * Caches an object under a given key, and with a given expiration. * * @param string $key The key under which the object will be cached. * @param array|object $object The object to cache. * @param int $expiration Expiration for the cached object, in seconds. * @param string $group The group under which the object will be cached. * * @return bool True if the object is cached successfully, false otherwise. */ public function cache_object( string $key, $object, int $expiration, string $group = '' ): bool; /** * Removes a cached object from the cache. * * @param string $key They key under which the object is cached. * @param string $group The group under which the object is cached. * * @return bool True if the object is removed from the cache successfully, false otherwise (because the object wasn't cached or for other reason). */ public function delete_cached_object( string $key, string $group = '' ): bool; /** * Checks if an object is cached under a given key. * * @param string $key The key to verify. * @param string $group The group under which the object is cached. * * @return bool True if there's an object cached under the given key, false otherwise. */ public function is_cached( string $key, string $group = '' ): bool; /** * Deletes all cached objects under a given group. * * @param string $group The group to delete. * * @return bool True if the group is deleted successfully, false otherwise. */ public function delete_cache_group( string $group = '' ): bool; } WPCacheEngine.php 0000777 00000011033 15251736434 0007674 0 ustar 00 <?php namespace Automattic\WooCommerce\Caching; /** * Implementation of CacheEngine that uses the built-in WordPress cache. */ class WPCacheEngine implements CacheEngine { use CacheNameSpaceTrait; /** * Retrieves an object cached under a given key. * * @param string $key The key under which the object to retrieve is cached. * @param string $group The group under which the object is cached. * * @return array|object|null The cached object, or null if there's no object cached under the passed key. */ public function get_cached_object( string $key, string $group = '' ) { $prefixed_key = self::get_prefixed_key( $key, $group ); $value = wp_cache_get( $prefixed_key, $group ); return false === $value ? null : $value; } /** * Retrieves a set of objects cached under the given keys. * * @param string[] $keys The keys under which the object to retrieve is cached. * @param string $group The group under which the object is cached. * * @return array The cached array of objects keyed by the given keys, values will be null for any non-cached keys. */ public function get_cached_objects( array $keys, string $group = '' ) { $prefix = self::get_cache_prefix( $group ); $key_map = array_combine( $keys, array_map( function ( $key ) use ( $prefix ) { return $prefix . $key; }, $keys ) ); $cached_values = wp_cache_get_multiple( array_values( $key_map ), $group ); $return_values = array(); foreach ( $key_map as $key => $prefixed_key ) { if ( isset( $cached_values[ $prefixed_key ] ) && false !== $cached_values[ $prefixed_key ] ) { $return_values[ $key ] = $cached_values[ $prefixed_key ]; } else { $return_values[ $key ] = null; } } return $return_values; } /** * Caches an object under a given key, and with a given expiration. * * @param string $key The key under which the object will be cached. * @param array|object $object The object to cache. * @param int $expiration Expiration for the cached object, in seconds. * @param string $group The group under which the object will be cached. * * @return bool True if the object is cached successfully, false otherwise. */ public function cache_object( string $key, $object, int $expiration, string $group = '' ): bool { $prefixed_key = self::get_prefixed_key( $key, $group ); return false !== wp_cache_set( $prefixed_key, $object, $group, $expiration ); } /** * Caches an object under a given key, and with a given expiration. * * @param array $objects The objects to cache keyed by the key to cache under. * @param int $expiration Expiration for the cached object, in seconds. * @param string $group The group under which the object will be cached. * * @return array Array of return values, grouped by key. Each value is either * true on success, or false on failure */ public function cache_objects( array $objects, int $expiration, string $group = '' ): array { $prefix = self::get_cache_prefix( $group ); $objects = array_combine( array_map( function ( $key ) use ( $prefix ) { return $prefix . $key; }, array_keys( $objects ) ), $objects, ); return wp_cache_set_multiple( $objects, $group, $expiration ); } /** * Removes a cached object from the cache. * * @param string $key They key under which the object is cached. * @param string $group The group under which the object is cached. * * @return bool True if the object is removed from the cache successfully, false otherwise (because the object wasn't cached or for other reason). */ public function delete_cached_object( string $key, string $group = '' ): bool { $prefixed_key = self::get_prefixed_key( $key, $group ); return false !== wp_cache_delete( $prefixed_key, $group ); } /** * Checks if an object is cached under a given key. * * @param string $key The key to verify. * @param string $group The group under which the object is cached. * * @return bool True if there's an object cached under the given key, false otherwise. */ public function is_cached( string $key, string $group = '' ): bool { $prefixed_key = self::get_prefixed_key( $key, $group ); return false !== wp_cache_get( $prefixed_key, $group ); } /** * Deletes all cached objects under a given group. * * @param string $group The group to delete. * * @return bool True if the group is deleted successfully, false otherwise. */ public function delete_cache_group( string $group = '' ): bool { return false !== self::invalidate_cache_group( $group ); } }
| ver. 1.6 |
Github
|
.
| PHP 8.1.34 | Генерация страницы: 0 |
proxy
|
phpinfo
|
Настройка