first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
{
"name": "wpdesk\/wp-cache",
"authors": [
{
"name": "Krzysiek",
"email": "krzysiek@wpdesk.pl"
}
],
"require": {
"php": ">=5.5",
"psr\/simple-cache": "^1.0"
},
"require-dev": {
"phpunit\/phpunit": "<7",
"wp-coding-standards\/wpcs": "^0.14.1",
"squizlabs\/php_codesniffer": "^3.0.2",
"mockery\/mockery": "*",
"10up\/wp_mock": "*"
},
"autoload": {
"psr-4": {
"DPDVendor\\WPDesk\\Cache\\": "src\/"
}
},
"autoload-dev": {},
"scripts": {
"phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-text --colors=never",
"phpunit-unit-fast": "phpunit --configuration phpunit-unit.xml --no-coverage",
"phpunit-integration": "phpunit --configuration phpunit-integration.xml --coverage-text --colors=never",
"phpunit-integration-fast": "phpunit --configuration phpunit-integration.xml --no-coverage"
}
}

View File

@@ -0,0 +1,105 @@
<?php
namespace DPDVendor\WPDesk\Cache;
use DPDVendor\Psr\SimpleCache\CacheInterface;
class CacheDispatcher
{
/**
* Cache info resolvers.
*
* @var CacheInfoResolver[]
*/
private $resolvers;
/**
* Cache.
*
* @var CacheInterface
*/
private $cache;
/**
* CacheDispatcher constructor.
*
* @param CacheInterface $cache
* @param CacheInfoResolver[] $resolvers
*/
public function __construct($cache, $resolvers)
{
$this->cache = $cache;
$this->resolvers = $resolvers;
}
/**
* Dispatch.
*
* @param object $object
* @param CacheItemCreator $cacheItemCreator
* @param CacheItemVerifier $cacheItemVerifier
*
* @return mixed
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function dispatch($object, $cacheItemCreator, $cacheItemVerifier)
{
foreach ($this->resolvers as $resolver) {
if ($resolver->isSupported($object)) {
return $this->getOrCreateItem($resolver, $object, $cacheItemCreator, $cacheItemVerifier);
}
}
return $cacheItemCreator->createCacheItem($object);
}
/**
* Get item from cache.
*
* @param string $cacheKey Cache key.
* @param CacheItemVerifier $cacheItemVerifier
*
* @return mixed|null
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
private function getItemFromCache($cacheKey, $cacheItemVerifier)
{
try {
$responseSerialized = $this->cache->get($cacheKey);
} catch (\Exception $e) {
$responseSerialized = null;
}
if ($responseSerialized !== null) {
$response = $cacheItemVerifier->getVerifiedItemOrNull(\unserialize($responseSerialized));
if (null !== $response) {
return $response;
}
}
return null;
}
/**
* Get or create item.
*
* @param CacheInfoResolver $resolver
* @param object $object
* @param CacheItemCreator $cacheItemCreator
* @param CacheItemVerifier $cacheItemVerifier
*
* @return mixed
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
private function getOrCreateItem($resolver, $object, $cacheItemCreator, $cacheItemVerifier)
{
$howToCache = $resolver->prepareHowToCache($object);
if ($resolver->shouldCache($object)) {
$item = $this->getItemFromCache($howToCache->getCacheKey(), $cacheItemVerifier);
if (null != $item) {
return $item;
}
}
$item = $cacheItemCreator->createCacheItem($object);
if ($resolver->shouldCache($object)) {
$this->cache->set($howToCache->getCacheKey(), \serialize($item), $howToCache->getCacheTtl());
}
if ($resolver->shouldClearCache($object, $item)) {
$this->cache->clear();
} else {
$this->cache->deleteMultiple($resolver->shouldClearKeys($object, $item));
}
return $item;
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace DPDVendor\WPDesk\Cache;
interface CacheInfoResolver
{
/**
* Is supported.
*
* @param object $object
*
* @return bool
*/
public function isSupported($object);
/**
* Should be cached.
*
* @param object $object
*
* @return bool
*/
public function shouldCache($object);
/**
* Prepare how to cache.
*
* @param object $object
*
* @return HowToCache
*/
public function prepareHowToCache($object);
/**
* Should clear cache.
*
* @param object $object
* @param mixed $item
*
* @return bool
*/
public function shouldClearCache($object, $item);
/**
* Should clear keys.
*
* @param object $object
* @param mixed $item
*
* @return string[]
*/
public function shouldClearKeys($object, $item);
}

View File

@@ -0,0 +1,19 @@
<?php
namespace DPDVendor\WPDesk\Cache;
/**
* Cache item creator.
*
* Interface CacheItemCreator
* @package WPDesk\Cache
*/
interface CacheInfoResolverCreator
{
/**
* Create resolvers.
*
* @return CacheInfoResolver[]
*/
public function createResolvers();
}

View File

@@ -0,0 +1,20 @@
<?php
namespace DPDVendor\WPDesk\Cache;
/**
* Cache item creator.
*
* Interface CacheItemCreator
* @package WPDesk\Cache
*/
interface CacheItemCreator
{
/**
* Create item to cache.
*
* @param object $object
* @return mixed
*/
public function createCacheItem($object);
}

View File

@@ -0,0 +1,21 @@
<?php
namespace DPDVendor\WPDesk\Cache;
/**
* Cache item verifier.
*
* Interface CacheItemVerifier
* @package WPDesk\Cache
*/
interface CacheItemVerifier
{
/**
* Get verified item or null.
* Verifies item and returns it or null when item is not valid.
*
* @param $object
* @return null|object
*/
public function getVerifiedItemOrNull($object);
}

View File

@@ -0,0 +1,64 @@
<?php
namespace DPDVendor\WPDesk\Cache;
/**
* How to cache item.
*
* Class HowToCache
* @package WPDesk\Cache
*/
class HowToCache
{
/**
* Cache key.
*
* @var string
*/
public $cacheKey;
/**
* Cache TTL.
*
* @var int
*/
public $cacheTtl;
/**
* HowToCache constructor.
*
* @param string $cacheKey Cache key.
* @param int $cacheTtl Cache TTL.
*/
public function __construct($cacheKey, $cacheTtl = 3600)
{
$this->cacheKey = $cacheKey;
$this->cacheTtl = $cacheTtl;
}
/**
* @return string
*/
public function getCacheKey()
{
return $this->cacheKey;
}
/**
* @param string $cacheKey
*/
public function setCacheKey($cacheKey)
{
$this->cacheKey = $cacheKey;
}
/**
* @return int
*/
public function getCacheTtl()
{
return $this->cacheTtl;
}
/**
* @param int $cacheTtl
*/
public function setCacheTtl($cacheTtl)
{
$this->cacheTtl = $cacheTtl;
}
}

View File

@@ -0,0 +1,150 @@
<?php
namespace DPDVendor\WPDesk\Cache;
use DPDVendor\Psr\SimpleCache\CacheInterface;
/**
* Class WordpressCache
*
* @package WPDesk\Cache
*/
class WordpressCache implements \DPDVendor\Psr\SimpleCache\CacheInterface
{
const TRANSIENT_NAME_PREFIX = 'wspc_';
/**
* Fetches a value from the cache.
*
* @param string $key The unique key of this item in the cache.
* @param mixed $default Default value to return if the key does not exist.
*
* @return mixed The value of the item from the cache, or $default in case of cache miss.
*/
public function get($key, $default = null)
{
$value = \get_transient($this->prapareTransientNameForKey($key));
if (\false === $value) {
return $default;
}
return $value;
}
/**
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
*
* @param string $key The key of the item to store.
* @param mixed $value The value of the item to store, must be serializable.
* @param null|int $ttl Optional. Time until expiration in seconds. Default 0 (no expiration).
*
* @return bool True on success and false on failure.
*/
public function set($key, $value, $ttl = 0)
{
return \set_transient($this->prapareTransientNameForKey($key), $value, $ttl);
}
/**
* Delete an item from the cache by its unique key.
*
* @param string $key The unique cache key of the item to delete.
*
* @return bool True if the item was successfully removed. False if there was an error.
*/
public function delete($key)
{
return \delete_transient($this->prapareTransientNameForKey($key));
}
/**
* Wipes clean the entire cache's keys.
*
* @return bool True on success and false on failure.
*/
public function clear()
{
global $wpdb;
$transient_prefix = self::TRANSIENT_NAME_PREFIX;
$transients = $wpdb->get_results("SELECT option_name AS name FROM {$wpdb->options} WHERE option_name LIKE '_transient_{$transient_prefix}%'");
foreach ($transients as $transient) {
$key = \substr($transient->name, \strlen("_transient_{$transient_prefix}"));
\delete_transient($this->prapareTransientNameForKey($key));
}
return \true;
}
/**
* Obtains multiple cache items by their unique keys.
*
* @param iterable $keys A list of keys that can obtained in a single operation.
* @param mixed $default Default value to return for keys that do not exist.
*
* @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as
* value.
*/
public function getMultiple($keys, $default = null)
{
$multiple_values = array();
foreach ($keys as $key) {
$multiple_values[$key] = $this->get($key, $default);
}
return $multiple_values;
}
/**
* Persists a set of key => value pairs in the cache, with an optional TTL.
*
* @param iterable $values A list of key => value pairs for a multiple-set operation.
* @param null|int $ttl Optional. The TTL value of this item. If no value is sent and
* the driver supports TTL then the library may set a default value
* for it or let the driver take care of that.
*
* @return bool True on success and false on failure.
*/
public function setMultiple($values, $ttl = 0)
{
foreach ($values as $key => $value) {
$set = $this->set($key, $value, $ttl);
if (\false === $set) {
return \false;
}
}
return \true;
}
/**
* Deletes multiple cache items in a single operation.
*
* @param iterable $keys A list of string-based keys to be deleted.
*
* @return bool True if the items were successfully removed. False if there was an error.
*/
public function deleteMultiple($keys)
{
$delete_multiple = \true;
foreach ($keys as $key) {
$delete_multiple = $delete_multiple && $this->delete($key);
}
return $delete_multiple;
}
/**
* Determines whether an item is present in the cache.
*
* NOTE: It is recommended that has() is only to be used for cache warming type purposes
* and not to be used within your live applications operations for get/set, as this method
* is subject to a race condition where your has() will return true and immediately after,
* another script can remove it making the state of your app out of date.
*
* @param string $key The cache item key.
*
* @return bool
*
*/
public function has($key)
{
return !empty($this->get($key));
}
/**
* Prepare transient name for given key.
*
* @param string $key Key.
*
* @return string
*/
private function prapareTransientNameForKey($key)
{
return self::TRANSIENT_NAME_PREFIX . $key;
}
}