Skip to content

Commit d26ca59

Browse files
committed
feat: add dynamodb object cache
1 parent ed0d103 commit d26ca59

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+4224
-222
lines changed

bootstrap.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir WordPress plugin.
7+
*
8+
* (c) Carl Alexander <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
if (version_compare(PHP_VERSION, '7.2', '<')) {
15+
exit(sprintf('Ymir requires PHP 7.2 or higher. Your WordPress site is using PHP %s.', PHP_VERSION));
16+
}
17+
18+
// Setup class autoloader
19+
require_once dirname(__FILE__).'/src/Autoloader.php';
20+
\Ymir\Plugin\Autoloader::register();
21+
22+
global $ymir;
23+
24+
$ymir = new \Ymir\Plugin\Plugin(__DIR__.'/ymir.php');

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
],
1414
"require": {
1515
"php": "^7.2 || ^8.0",
16+
"ext-curl": "*",
1617
"ext-json": "*"
1718
},
1819
"require-dev": {

grumphp.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ grumphp:
3535
- 'src/Configuration'
3636
- 'src/CloudStorage/CloudStorageStreamWrapper.php'
3737
- 'src/Email/Email.php'
38-
- 'src/EventManagement/EventManager.php'
38+
- 'src/ObjectCache/AbstractPersistentObjectCache.php'
39+
- 'src/Support/Collection.php'
3940
- 'tests'
4041
phpunit:
4142
always_execute: true

object-cache-api.php

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir WordPress plugin.
7+
*
8+
* (c) Carl Alexander <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
require_once __DIR__.'/bootstrap.php';
15+
16+
use Ymir\Plugin\ObjectCache\ObjectCacheInterface;
17+
use Ymir\Plugin\ObjectCache\PersistentObjectCacheInterface;
18+
use Ymir\Plugin\ObjectCache\PreloadedObjectCacheInterface;
19+
20+
/**
21+
* Ymir object cache API.
22+
*
23+
* @link https://developer.wordpress.org/reference/classes/wp_object_cache
24+
*/
25+
26+
/**
27+
* Adds data to the cache, if the cache key doesn't already exist.
28+
*
29+
* @link https://developer.wordpress.org/reference/functions/wp_cache_add
30+
*/
31+
function wp_cache_add($key, $data,$group = '', $expire = 0): bool
32+
{
33+
global $wp_object_cache;
34+
35+
return $wp_object_cache->add(trim((string) $group) ?: 'default', (string) $key, $data, (int) $expire);
36+
}
37+
38+
/**
39+
* Adds a group or set of groups to the list of global groups.
40+
*
41+
* @link https://developer.wordpress.org/reference/functions/wp_cache_add_global_groups
42+
*/
43+
function wp_cache_add_global_groups($groups)
44+
{
45+
global $wp_object_cache;
46+
47+
$wp_object_cache->addGlobalGroups((array) $groups);
48+
}
49+
50+
/**
51+
* Adds a group or set of groups to the list of non-persistent groups.
52+
*
53+
* @link https://developer.wordpress.org/reference/functions/wp_cache_add_non_persistent_groups
54+
*/
55+
function wp_cache_add_non_persistent_groups($groups)
56+
{
57+
global $wp_object_cache;
58+
59+
$wp_object_cache->addNonPersistentGroups((array) $groups);
60+
}
61+
62+
/**
63+
* Closes the cache.
64+
*
65+
* @link https://developer.wordpress.org/reference/functions/wp_cache_close
66+
*/
67+
function wp_cache_close(): bool
68+
{
69+
global $wp_object_cache;
70+
71+
return $wp_object_cache->close();
72+
}
73+
74+
/**
75+
* Decrements numeric cache item's value.
76+
*
77+
* @link https://developer.wordpress.org/reference/functions/wp_cache_decr
78+
*/
79+
function wp_cache_decr($key, $offset = 1, $group = '')
80+
{
81+
global $wp_object_cache;
82+
83+
return $wp_object_cache->decrement(trim((string) $group) ?: 'default', (string) $key, (int) $offset);
84+
}
85+
86+
/**
87+
* Removes the cache contents matching key and group.
88+
*
89+
* @link https://developer.wordpress.org/reference/functions/wp_cache_delete
90+
*/
91+
function wp_cache_delete($key, $group = ''): bool
92+
{
93+
global $wp_object_cache;
94+
95+
return $wp_object_cache->delete(trim((string) $group) ?: 'default', (string) $key);
96+
}
97+
98+
/**
99+
* Removes all cache items.
100+
*
101+
* @link https://developer.wordpress.org/reference/functions/wp_cache_flush
102+
*/
103+
function wp_cache_flush(): bool
104+
{
105+
global $wp_object_cache;
106+
107+
return $wp_object_cache->flush();
108+
}
109+
110+
/**
111+
* Retrieves the cache contents from the cache by key and group.
112+
*
113+
* @link https://developer.wordpress.org/reference/functions/wp_cache_get
114+
*/
115+
function wp_cache_get($key, $group = '', $force = false, &$found = null)
116+
{
117+
global $wp_object_cache;
118+
119+
return $wp_object_cache->get(trim((string) $group) ?: 'default', (string) $key, (bool) $force, $found);
120+
}
121+
122+
/**
123+
* Retrieves multiple values from the cache in one call.
124+
*
125+
* @link https://developer.wordpress.org/reference/functions/wp_cache_get_multiple
126+
*/
127+
function wp_cache_get_multiple($keys, $group = '', $force = false): array
128+
{
129+
global $wp_object_cache;
130+
131+
return $wp_object_cache->getMultiple(trim((string) $group) ?: 'default', (array) $keys, (bool) $force);
132+
}
133+
134+
/**
135+
* Increment numeric cache item's value.
136+
*
137+
* @link https://developer.wordpress.org/reference/functions/wp_cache_incr
138+
*/
139+
function wp_cache_incr($key, $offset = 1, $group = '')
140+
{
141+
global $wp_object_cache;
142+
143+
return $wp_object_cache->increment(trim((string) $group) ?: 'default', (string) $key, (int) $offset);
144+
}
145+
146+
/**
147+
* Sets up Object Cache Global and assigns it.
148+
*
149+
* @link https://developer.wordpress.org/reference/functions/wp_cache_init
150+
*/
151+
function wp_cache_init()
152+
{
153+
global $wp_object_cache, $ymir;
154+
155+
try {
156+
$objectCache = $ymir->getContainer()->get('ymir_object_cache');
157+
158+
if (!$objectCache instanceof ObjectCacheInterface) {
159+
throw new RuntimeException('Object cache needs to implement ObjectCacheInterface');
160+
} elseif ($objectCache instanceof PersistentObjectCacheInterface && !$objectCache->isAvailable()) {
161+
throw new RuntimeException('Persistent object cache is unavailable');
162+
}
163+
164+
$wp_object_cache = $objectCache;
165+
166+
if ($objectCache instanceof PreloadedObjectCacheInterface) {
167+
$objectCache->load();
168+
}
169+
} catch (Exception $exception) {
170+
$wp_object_cache = $ymir->getContainer()->get('wordpress_object_cache');
171+
}
172+
173+
}
174+
175+
/**
176+
* Replaces the contents of the cache with new data.
177+
*
178+
* @link https://developer.wordpress.org/reference/functions/wp_cache_replace
179+
*/
180+
function wp_cache_replace($key, $data, $group = '', $expire = 0): bool
181+
{
182+
global $wp_object_cache;
183+
184+
return $wp_object_cache->replace(trim((string) $group) ?: 'default', (string) $key, $data, (int) $expire);
185+
}
186+
187+
/**
188+
* Reset internal cache keys and structures.
189+
*
190+
* If the cache back end uses global blog or site IDs as part of its cache keys,
191+
* this function instructs the back end to reset those keys and perform any cleanup
192+
* since blog or site IDs have changed since cache init.
193+
*
194+
* This function is deprecated. Use wp_cache_switch_to_blog() instead of this
195+
* function when preparing the cache for a blog switch. For clearing the cache
196+
* during unit tests, consider using wp_cache_init(). wp_cache_init() is not
197+
* recommended outside of unit tests as the performance penalty for using it is
198+
* high.
199+
*
200+
* @link https://developer.wordpress.org/reference/functions/wp_cache_reset
201+
*/
202+
function wp_cache_reset() {
203+
_deprecated_function(__FUNCTION__, '3.5.0', 'WP_Object_Cache::reset()');
204+
}
205+
206+
/**
207+
* Saves the data to the cache.
208+
*
209+
* Differs from wp_cache_add() and wp_cache_replace() in that it will always write data.
210+
*
211+
* @link https://developer.wordpress.org/reference/functions/wp_cache_save
212+
*/
213+
function wp_cache_set($key, $data, $group = '', $expire = 0): bool
214+
{
215+
global $wp_object_cache;
216+
217+
return $wp_object_cache->set(trim((string) $group) ?: 'default', (string) $key, $data, (int) $expire);
218+
}
219+
220+
/**
221+
* Switches the internal blog ID.
222+
*
223+
* This changes the blog id used to create keys in blog specific groups.
224+
*
225+
* @link https://developer.wordpress.org/reference/functions/wp_cache_switch_to_blog
226+
*/
227+
function wp_cache_switch_to_blog($blogId)
228+
{
229+
global $wp_object_cache;
230+
231+
$wp_object_cache->switchToBlog((int) $blogId);
232+
}

src/CloudProvider/Aws/AbstractClient.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,20 @@
1313

1414
namespace Ymir\Plugin\CloudProvider\Aws;
1515

16+
use Ymir\Plugin\Http\Client;
17+
1618
/**
1719
* Base AWS client.
1820
*/
1921
abstract class AbstractClient
2022
{
23+
/**
24+
* The Ymir HTTP client.
25+
*
26+
* @var Client
27+
*/
28+
private $client;
29+
2130
/**
2231
* The AWS API key.
2332
*
@@ -46,19 +55,12 @@ abstract class AbstractClient
4655
*/
4756
private $securityToken;
4857

49-
/**
50-
* The WordPress HTTP transport.
51-
*
52-
* @var \WP_Http
53-
*/
54-
private $transport;
55-
5658
/**
5759
* Constructor.
5860
*/
59-
public function __construct(\WP_Http $transport, string $key, string $region, string $secret)
61+
public function __construct(Client $client, string $key, string $region, string $secret)
6062
{
61-
$this->transport = $transport;
63+
$this->client = $client;
6264
$this->key = $key;
6365
$this->region = $region;
6466
$this->secret = $secret;
@@ -142,15 +144,7 @@ protected function request(string $method, string $uri, ?string $body = null, ar
142144
$arguments['body'] = $body;
143145
}
144146

145-
$response = $this->transport->request($this->createRequestUrl($uri), $arguments);
146-
147-
if ($response instanceof \WP_Error) {
148-
throw new \RuntimeException($response->get_error_message());
149-
} elseif (!is_array($response)) {
150-
throw new \RuntimeException('Response must be an array');
151-
}
152-
153-
return $response;
147+
return $this->client->request($this->createRequestUrl($uri), $arguments);
154148
}
155149

156150
/**

0 commit comments

Comments
 (0)