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