Skip to content

Commit ba57923

Browse files
authored
Merge pull request #552 from skyverge/mwc-2477/add-cacheable-request-trait
Add cacheable request trait
2 parents ad8dde4 + 7ad5989 commit ba57923

File tree

3 files changed

+249
-0
lines changed

3 files changed

+249
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace API;
4+
5+
use SkyVerge\WooCommerce\PluginFramework\v5_10_10\API\Traits\Cacheable_Request_Trait;
6+
7+
if ( ! defined( 'ABSPATH' ) ) {
8+
define( 'ABSPATH', true );
9+
}
10+
11+
class CacheableRequestTraitTest extends \Codeception\Test\Unit
12+
{
13+
14+
15+
/**
16+
* Runs before each test.
17+
*/
18+
protected function _before()
19+
{
20+
21+
require_once('woocommerce/api/interface-sv-wc-api-request.php');
22+
require_once('woocommerce/api/abstract-sv-wc-api-json-request.php');
23+
require_once('woocommerce/api/traits/Cacheable_Request_Trait.php');
24+
}
25+
26+
/** @see CacheableRequest::get_cache_lifetime() */
27+
public function test_get_cache_lifetime() {
28+
$request = $this->get_test_request_instance();
29+
30+
$this->assertSame( 86400, $request->get_cache_lifetime() );
31+
}
32+
33+
/** @see CacheableRequest::set_cache_lifetime() */
34+
public function test_set_cache_lifetime() {
35+
$request = $this->get_test_request_instance();
36+
37+
$this->assertSame( $request, $request->set_cache_lifetime( 1000 ) );
38+
$this->assertSame( 1000, $request->get_cache_lifetime() );
39+
}
40+
41+
/** @see CacheableRequest::should_refresh() */
42+
public function test_should_refresh() {
43+
$request = $this->get_test_request_instance();
44+
45+
$this->assertFalse( $request->should_refresh() );
46+
}
47+
48+
/** @see CacheableRequest::set_force_refresh() */
49+
public function test_set_force_refresh() {
50+
$request = $this->get_test_request_instance();
51+
52+
$this->assertSame( $request, $request->set_force_refresh( true ) );
53+
$this->assertTrue( $request->should_refresh() );
54+
}
55+
56+
/** @see CacheableRequest::should_refresh() */
57+
public function test_should_cache() {
58+
$request = $this->get_test_request_instance();
59+
60+
$this->assertTrue( $request->should_cache() );
61+
}
62+
63+
/** @see CacheableRequest::set_should_cache() */
64+
public function test_set_should_cache() {
65+
$request = $this->get_test_request_instance();
66+
67+
$this->assertSame( $request, $request->set_should_cache( false ) );
68+
$this->assertFalse( $request->should_cache() );
69+
}
70+
71+
/** @see CacheableRequest::bypass_cache() */
72+
public function bypass_cache() {
73+
$request = $this->get_test_request_instance();
74+
75+
$this->assertSame( $request, $request->bypass_cache() );
76+
$this->assertTrue( $request->should_refresh() );
77+
$this->assertFalse( $request->should_cache() );
78+
}
79+
80+
/**
81+
* Gets a test request instance using the CacheableRequestTrait.
82+
*/
83+
protected function get_test_request_instance() {
84+
return $this->getMockForTrait(Cacheable_Request_Trait::class);
85+
}
86+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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;

woocommerce/class-sv-wc-plugin.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ private function includes() {
451451
require_once( $framework_path . '/api/abstract-sv-wc-api-json-request.php' );
452452
require_once( $framework_path . '/api/abstract-sv-wc-api-json-response.php' );
453453

454+
// Cacheable API Requests
455+
require_once( $framework_path . '/api/traits/Cacheable_Request_Trait.php' );
456+
454457
// REST API Controllers
455458
require_once( $framework_path . '/rest-api/Controllers/Settings.php' );
456459

0 commit comments

Comments
 (0)