woocommerce-paypal-payments/modules/ppcp-api-client/src/Helper/Cache.php

78 lines
1.4 KiB
PHP
Raw Normal View History

2020-09-11 13:38:02 +03:00
<?php
/**
* Manages caching of values.
*
2020-09-11 14:11:10 +03:00
* @package WooCommerce\PayPalCommerce\ApiClient\Helper
2020-09-11 13:38:02 +03:00
*/
2020-09-11 14:24:08 +03:00
2020-09-11 13:38:02 +03:00
declare( strict_types=1 );
2020-09-11 14:11:10 +03:00
namespace WooCommerce\PayPalCommerce\ApiClient\Helper;
2020-09-11 13:38:02 +03:00
/**
* Class Cache
*/
class Cache {
/**
* The prefix for the value keys.
*
* @var string
*/
private $prefix;
/**
* Cache constructor.
*
2020-09-11 14:24:08 +03:00
* @param string $prefix The prefix for the value keys.
2020-09-11 13:38:02 +03:00
*/
2020-09-11 14:24:08 +03:00
public function __construct( string $prefix ) {
2020-09-11 13:38:02 +03:00
$this->prefix = $prefix;
}
/**
* Gets a value.
*
* @param string $key The key under which the value is stored.
*
* @return mixed
*/
2020-09-11 14:24:08 +03:00
public function get( string $key ) {
return get_transient( $this->prefix . $key );
2020-09-11 13:38:02 +03:00
}
2020-09-14 07:28:45 +03:00
/**
* Whether a value is stored or not.
*
* @param string $key The key for the value.
*
* @return bool
*/
public function has( string $key ): bool {
$value = $this->get( $key );
return false !== $value;
2020-09-14 07:28:45 +03:00
}
/**
* Deletes a cache.
*
* @param string $key The key.
*/
2024-11-06 09:14:44 +02:00
public function delete( string $key ): void {
2020-09-14 07:28:45 +03:00
delete_transient( $this->prefix . $key );
}
2020-09-11 13:38:02 +03:00
/**
* Caches a value.
*
* @param string $key The key under which the value should be cached.
2020-09-11 14:24:08 +03:00
* @param mixed $value The value to cache.
* @param int $expiration Time until expiration in seconds.
2020-09-11 13:38:02 +03:00
*
* @return bool
*/
public function set( string $key, $value, int $expiration = 0 ): bool {
return (bool) set_transient( $this->prefix . $key, $value, $expiration );
2020-09-11 13:38:02 +03:00
}
2020-09-11 14:24:08 +03:00
}