|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WooCommerce Plugin Framework |
| 4 | + * |
| 5 | + * This source file is subject to the GNU General Public License v3.0 |
| 6 | + * that is bundled with this package in the file license.txt. |
| 7 | + * It is also available through the world-wide-web at this URL: |
| 8 | + * http://www.gnu.org/licenses/gpl-3.0.html |
| 9 | + * If you did not receive a copy of the license and are unable to |
| 10 | + * obtain it through the world-wide-web, please send an email |
| 11 | + * to [email protected] so we can send you a copy immediately. |
| 12 | + * |
| 13 | + * DISCLAIMER |
| 14 | + * |
| 15 | + * Do not edit or add to this file if you wish to upgrade the plugin to newer |
| 16 | + * versions in the future. If you wish to customize the plugin for your |
| 17 | + * needs please refer to http://www.skyverge.com |
| 18 | + * |
| 19 | + * @package SkyVerge/WooCommerce/API/Response |
| 20 | + * @author SkyVerge |
| 21 | + * @copyright Copyright (c) 2013-2021, SkyVerge, Inc. |
| 22 | + * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 |
| 23 | + */ |
| 24 | + |
| 25 | +namespace SkyVerge\WooCommerce\PluginFramework\v5_10_10\API\Traits; |
| 26 | + |
| 27 | +defined( 'ABSPATH' ) or exit; |
| 28 | + |
| 29 | +if ( ! trait_exists( '\\SkyVerge\\WooCommerce\\PluginFramework\\v5_10_10\\API\\Traits\\Cacheable_Request_Trait' ) ) : |
| 30 | + |
| 31 | +/** |
| 32 | + * This trait can be used to add response caching support to API requests. |
| 33 | + * |
| 34 | + * It is intended to be used by a class implementing the SV_WC_API_Request interface. Caching itself is handled |
| 35 | + * by the SV_WC_API_Base class in the perform_request method. |
| 36 | + * |
| 37 | + * Simply adding `use Cacheable_Request_Trait;` to a request class will enable caching, but it's also possible to |
| 38 | + * customize the cache lifetime by setting it in the request constructor. |
| 39 | + */ |
| 40 | +trait Cacheable_Request_Trait { |
| 41 | + |
| 42 | + |
| 43 | + /** @var int the cache lifetime for the request, in seconds, defaults to 86400 (24 hours) */ |
| 44 | + protected $cache_lifetime = 86400; |
| 45 | + |
| 46 | + /** @var bool whether to force a fresh request regardless if a cached response is available */ |
| 47 | + protected $force_refresh = false; |
| 48 | + |
| 49 | + /** @var bool whether to the current request should be cached or not */ |
| 50 | + protected $should_cache = true; |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * Sets the cache lifetime for this request. |
| 55 | + * |
| 56 | + * @since 5.10.10 |
| 57 | + * |
| 58 | + * @param int $lifetime cache lifetime, in seconds. Set to 0 for unlimited |
| 59 | + * @return self |
| 60 | + */ |
| 61 | + public function set_cache_lifetime( int $lifetime ) { |
| 62 | + |
| 63 | + $this->cache_lifetime = $lifetime; |
| 64 | + |
| 65 | + return $this; |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + /** |
| 70 | + * Gets the cache lifetime for this request. |
| 71 | + * |
| 72 | + * @since 5.10.10 |
| 73 | + * |
| 74 | + * @return int |
| 75 | + */ |
| 76 | + public function get_cache_lifetime() : int { |
| 77 | + |
| 78 | + return $this->cache_lifetime; |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + /** |
| 83 | + * Sets whether a fresh request should be attempted, regardless if a cached response is available. |
| 84 | + * |
| 85 | + * @since 5.10.10 |
| 86 | + * |
| 87 | + * @param bool $value whether to force a fresh request, or not |
| 88 | + * @return self |
| 89 | + */ |
| 90 | + public function set_force_refresh( bool $value ) { |
| 91 | + |
| 92 | + $this->force_refresh = $value; |
| 93 | + |
| 94 | + return $this; |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + /** |
| 99 | + * Determines whether a fresh request should be attempted. |
| 100 | + * |
| 101 | + * @since 5.10.10 |
| 102 | + * |
| 103 | + * @return bool |
| 104 | + */ |
| 105 | + public function should_refresh() : bool { |
| 106 | + |
| 107 | + return $this->force_refresh; |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + /** |
| 112 | + * Sets whether the request's response should be stored in cache. |
| 113 | + * |
| 114 | + * @since 5.10.10 |
| 115 | + * |
| 116 | + * @param bool $value whether to cache the request, or not |
| 117 | + * @return self |
| 118 | + */ |
| 119 | + public function set_should_cache( bool $value ) { |
| 120 | + |
| 121 | + $this->should_cache = $value; |
| 122 | + |
| 123 | + return $this; |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + /** |
| 128 | + * Determines whether the request's response should be stored in cache. |
| 129 | + * |
| 130 | + * @since 5.10.10 |
| 131 | + * |
| 132 | + * @return bool |
| 133 | + */ |
| 134 | + public function should_cache() : bool { |
| 135 | + |
| 136 | + return $this->should_cache; |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + /** |
| 141 | + * Bypasses caching for this request completely. |
| 142 | + * |
| 143 | + * When called, sets the `force_refresh` flag to true and `should_cache` flag to false |
| 144 | + * |
| 145 | + * @since 5.10.10 |
| 146 | + * |
| 147 | + * @return self |
| 148 | + */ |
| 149 | + public function bypass_cache() { |
| 150 | + |
| 151 | + $this->set_force_refresh( true ); |
| 152 | + $this->set_should_cache( false ); |
| 153 | + |
| 154 | + return $this; |
| 155 | + } |
| 156 | + |
| 157 | +} |
| 158 | + |
| 159 | + |
| 160 | +endif; |
0 commit comments