PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.1.16
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.1.16
4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 1.9.3 1.9.4 1.9.5 1.9.6 All 170 releases
searchpro / BerqWP / vendor-prefixed / src / Cache.php

Cache.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 4.1.16, at BerqWP/vendor-prefixed/src/Cache.php

122 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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