PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.1.19
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.1.19
4.1.19 4.1.18 4.1.17 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 All 173 releases
searchpro / BerqWP / src / Cache.php

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

174 lines 4.7 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 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 = 'cache/request';
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 // Create the cache directory if it doesn't exist
121 if (!file_exists($this->cache_directory)) {
122 mkdir($this->cache_directory, 0755, true);
123 }
124
125 // $cache_file = $this->cache_directory . md5($page_url) . '.html';
126
127 // file_put_contents($cache_file, $html);
128
129 $cache_path = $this->cache_directory . bwp_build_cache_path($page_url);
130
131 if (!is_dir($cache_path)) {
132 wp_mkdir_p($cache_path);
133 }
134
135 $cache_file = $cache_path . '/index.html.gz';
136 $html = gzencode($html, 9);
137 file_put_contents($cache_file, $html);
138
139 }
140
141 function request_cache_warmup($post_data, $async = false)
142 {
143
144 try {
145 $response = $this->client->post('cache/warmup', [
146 'timeout' => $async ? 5 : 30,
147 'form_params' => $post_data
148 ]);
149
150 return $response;
151
152 } catch (RequestException $e) {} catch (\Throwable $e) {}
153
154 }
155
156 function clear_queue($site_url, $site_id, $license_key) {
157 try {
158 $post_data = [
159 'site_url' => $site_url,
160 'site_id' => $site_id,
161 'clear_queue' => true,
162 'license_key' => $license_key,
163 ];
164 $response = $this->client->post('cache/discard-queue', [
165 'form_params' => $post_data
166 ]);
167
168 return $response;
169
170 } catch (RequestException $e) {} catch (\Throwable $e) {}
171 }
172
173 }
174