| 1 |
<?php |
| 2 |
|
| 3 |
namespace BerqWP; |
| 4 |
|
| 5 |
use BerqWP\RateLimiter; |
| 6 |
use BerqWP_Deps\GuzzleHttp\Pool; |
| 7 |
use BerqWP_Deps\GuzzleHttp\Psr7\Request; |
| 8 |
use BerqWP_Deps\GuzzleHttp\Exception\RequestException; |
| 9 |
class Cache |
| 10 |
{ |
| 11 |
protected $cache_directory = null; |
| 12 |
protected $client = null; |
| 13 |
protected $storage_dir = null; |
| 14 |
function __construct($client = null, $cache_directory = null, $storage_dir = null) |
| 15 |
{ |
| 16 |
$this->cache_directory = $cache_directory; |
| 17 |
$this->client = $client; |
| 18 |
$this->storage_dir = $storage_dir; |
| 19 |
} |
| 20 |
function request_cache($post_data, $timeout = 30) |
| 21 |
{ |
| 22 |
$rateLimiter = new RateLimiter(5, 60, $this->storage_dir); |
| 23 |
$clientIdentifier = gethostname(); |
| 24 |
if ($rateLimiter->isRateLimited($clientIdentifier)) { |
| 25 |
return \false; |
| 26 |
} |
| 27 |
try { |
| 28 |
$response = $this->client->post('cache/request', ['timeout' => $timeout, 'form_params' => $post_data]); |
| 29 |
if ($response->getStatusCode() === 200) { |
| 30 |
return \true; |
| 31 |
} |
| 32 |
} catch (RequestException $e) { |
| 33 |
} catch (\Throwable $e) { |
| 34 |
} |
| 35 |
return \false; |
| 36 |
} |
| 37 |
function queue_count($site_id, $timeout = 30) |
| 38 |
{ |
| 39 |
try { |
| 40 |
$response = $this->client->post('status/queue-count', ['timeout' => $timeout, 'form_params' => ['site_id' => $site_id]]); |
| 41 |
$body = $response->getBody(); |
| 42 |
$json = json_decode($body, \true); |
| 43 |
if (!empty($json) && $json['status'] == 'success') { |
| 44 |
return (int) $json['count']; |
| 45 |
} |
| 46 |
} catch (RequestException $e) { |
| 47 |
} catch (\Throwable $e) { |
| 48 |
} |
| 49 |
return \false; |
| 50 |
} |
| 51 |
function request_multi_cache($post_data_arr) |
| 52 |
{ |
| 53 |
if (empty($post_data_arr)) { |
| 54 |
return; |
| 55 |
} |
| 56 |
$rateLimiter = new RateLimiter(5, 60, $this->storage_dir); |
| 57 |
$clientIdentifier = gethostname(); |
| 58 |
if ($rateLimiter->isRateLimited($clientIdentifier)) { |
| 59 |
return \false; |
| 60 |
} |
| 61 |
$endpoint = ''; |
| 62 |
try { |
| 63 |
$requests = function ($post_data_arr) use ($endpoint) { |
| 64 |
foreach ($post_data_arr as $post_data) { |
| 65 |
yield new Request('POST', $endpoint, ['Content-Type' => 'application/x-www-form-urlencoded'], http_build_query($post_data)); |
| 66 |
} |
| 67 |
}; |
| 68 |
$results = []; |
| 69 |
$errors = []; |
| 70 |
$pool = new Pool($this->client, $requests($post_data_arr), [ |
| 71 |
'concurrency' => 5, |
| 72 |
// Adjust as needed |
| 73 |
'fulfilled' => function ($response, $index) use (&$results, $post_data_arr) { |
| 74 |
$results[$index] = $response->getBody()->getContents(); |
| 75 |
}, |
| 76 |
'rejected' => function ($reason, $index) use (&$errors, $post_data_arr) { |
| 77 |
$errors[$index] = $reason; |
| 78 |
}, |
| 79 |
]); |
| 80 |
$promise = $pool->promise(); |
| 81 |
$promise->wait(); |
| 82 |
} catch (RequestException $e) { |
| 83 |
} catch (\Throwable $e) { |
| 84 |
} |
| 85 |
} |
| 86 |
function store_cache($page_url, $html) |
| 87 |
{ |
| 88 |
// Create the cache directory if it doesn't exist |
| 89 |
if (!file_exists($this->cache_directory)) { |
| 90 |
mkdir($this->cache_directory, 0755, \true); |
| 91 |
} |
| 92 |
// $cache_file = $this->cache_directory . md5($page_url) . '.html'; |
| 93 |
// file_put_contents($cache_file, $html); |
| 94 |
$cache_path = $this->cache_directory . bwp_build_cache_path($page_url); |
| 95 |
if (!is_dir($cache_path)) { |
| 96 |
wp_mkdir_p($cache_path); |
| 97 |
} |
| 98 |
$cache_file = $cache_path . '/index.html.gz'; |
| 99 |
$html = gzencode($html, 9); |
| 100 |
file_put_contents($cache_file, $html); |
| 101 |
} |
| 102 |
function request_cache_warmup($post_data, $async = \false) |
| 103 |
{ |
| 104 |
try { |
| 105 |
$response = $this->client->post('cache/warmup', ['timeout' => $async ? 5 : 30, 'form_params' => $post_data]); |
| 106 |
return $response; |
| 107 |
} catch (RequestException $e) { |
| 108 |
} catch (\Throwable $e) { |
| 109 |
} |
| 110 |
} |
| 111 |
function clear_queue($site_url, $site_id, $license_key) |
| 112 |
{ |
| 113 |
try { |
| 114 |
$post_data = ['site_url' => $site_url, 'site_id' => $site_id, 'clear_queue' => \true, 'license_key' => $license_key]; |
| 115 |
$response = $this->client->post('cache/discard-queue', ['form_params' => $post_data]); |
| 116 |
return $response; |
| 117 |
} catch (RequestException $e) { |
| 118 |
} catch (\Throwable $e) { |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|