Skip to content

Commit ac40cb1

Browse files
committed
return result for cache update methods
1 parent 53d2c38 commit ac40cb1

File tree

4 files changed

+193
-16
lines changed

4 files changed

+193
-16
lines changed

docs/api/ApiCache.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The cache mecanism to store every decisions from LAPI/CAPI. Symfony Cache compon
1616
|[get](#apicacheget)|Request the cache for the specified IP.|
1717
|[prune](#apicacheprune)||
1818
|[pullUpdates](#apicachepullupdates)|Used in stream mode only.|
19+
|[warmUp](#apicachewarmup)|Used in stream mode only.|
1920

2021

2122

@@ -163,7 +164,36 @@ TODO P2 test for overlapping decisions strategy (ex: max expires)
163164

164165
**Return Values**
165166

166-
`void`
167+
`array`
168+
169+
> number of deleted and new decisions.
170+
171+
172+
<hr />
173+
174+
175+
### ApiCache::warmUp
176+
177+
**Description**
178+
179+
```php
180+
public warmUp (void)
181+
```
182+
183+
Used in stream mode only.
184+
185+
Warm the cache up.
186+
Used when the stream mode has just been activated.
187+
188+
**Parameters**
189+
190+
`This function has no parameters.`
191+
192+
**Return Values**
193+
194+
`int`
195+
196+
> number of decisions added.
167197
168198

169199
<hr />

docs/api/Bouncer.md

Lines changed: 133 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@ The main Class of this package. This is the first entry point of any PHP Bouncer
1111
| Name | Description |
1212
|------|-------------|
1313
|[__construct](#bouncer__construct)||
14+
|[buildCaptchaCouple](#bouncerbuildcaptchacouple)||
15+
|[checkCaptcha](#bouncercheckcaptcha)||
1416
|[clearCache](#bouncerclearcache)|This method clear the full data in cache.|
1517
|[configure](#bouncerconfigure)|Configure this instance.|
16-
|[getAccessForbiddenHtmlTemplate](#getAccessForbiddenHtmlTemplate)|Returns a default "CrowdSec 403" HTML template to display to a web browser using a banned IP.|
18+
|[getAccessForbiddenHtmlTemplate](#bouncergetaccessforbiddenhtmltemplate)|Returns a default "CrowdSec 403" HTML template to display to a web browser using a banned IP.|
19+
|[getCaptchaHtmlTemplate](#bouncergetcaptchahtmltemplate)|Returns a default "CrowdSec Captcha" HTML template to display to a web browser using a captchable IP.|
20+
|[getLogger](#bouncergetlogger)|Returns the logger instance.|
1721
|[getRemediationForIp](#bouncergetremediationforip)|Get the remediation for the specified IP. This method use the cache layer.|
1822
|[loadPaginatedBlocklistFromCache](#bouncerloadpaginatedblocklistfromcache)|Browse the remediations cache.|
1923
|[loadPaginatedLogs](#bouncerloadpaginatedlogs)|Browse the bouncer technical logs.|
2024
|[pruneCache](#bouncerprunecache)|This method prune the cache: it removes all the expired cache items.|
2125
|[refreshBlocklistCache](#bouncerrefreshblocklistcache)|Used in stream mode only.|
26+
|[warmBlocklistCacheUp](#bouncerwarmblocklistcacheup)|Used in stream mode only.|
2227

2328

2429

@@ -35,6 +40,54 @@ The main Class of this package. This is the first entry point of any PHP Bouncer
3540

3641

3742

43+
**Parameters**
44+
45+
`This function has no parameters.`
46+
47+
**Return Values**
48+
49+
`void`
50+
51+
52+
<hr />
53+
54+
55+
### Bouncer::buildCaptchaCouple
56+
57+
**Description**
58+
59+
```php
60+
buildCaptchaCouple (void)
61+
```
62+
63+
64+
65+
66+
67+
**Parameters**
68+
69+
`This function has no parameters.`
70+
71+
**Return Values**
72+
73+
`void`
74+
75+
76+
<hr />
77+
78+
79+
### Bouncer::checkCaptcha
80+
81+
**Description**
82+
83+
```php
84+
checkCaptcha (void)
85+
```
86+
87+
88+
89+
90+
3891
**Parameters**
3992

4093
`This function has no parameters.`
@@ -95,18 +148,66 @@ Configure this instance.
95148
<hr />
96149

97150

98-
### Bouncer::get403Template
151+
### Bouncer::getAccessForbiddenHtmlTemplate
99152

100153
**Description**
101154

102155
```php
103-
public getAccessForbiddenHtmlTemplate (void)
156+
public static getAccessForbiddenHtmlTemplate (void)
104157
```
105158

106159
Returns a default "CrowdSec 403" HTML template to display to a web browser using a banned IP.
107160

108161

109162

163+
**Parameters**
164+
165+
`This function has no parameters.`
166+
167+
**Return Values**
168+
169+
`void`
170+
171+
172+
<hr />
173+
174+
175+
### Bouncer::getCaptchaHtmlTemplate
176+
177+
**Description**
178+
179+
```php
180+
public static getCaptchaHtmlTemplate (void)
181+
```
182+
183+
Returns a default "CrowdSec Captcha" HTML template to display to a web browser using a captchable IP.
184+
185+
186+
187+
**Parameters**
188+
189+
`This function has no parameters.`
190+
191+
**Return Values**
192+
193+
`void`
194+
195+
196+
<hr />
197+
198+
199+
### Bouncer::getLogger
200+
201+
**Description**
202+
203+
```php
204+
public getLogger (void)
205+
```
206+
207+
Returns the logger instance.
208+
209+
210+
110211
**Parameters**
111212

112213
`This function has no parameters.`
@@ -236,7 +337,35 @@ This method should be called periodically (ex: crontab) in a asynchronous way to
236337

237338
**Return Values**
238339

239-
`void`
340+
`array`
341+
342+
> number of deleted and new decisions.
343+
344+
345+
<hr />
346+
347+
348+
### Bouncer::warmBlocklistCacheUp
349+
350+
**Description**
351+
352+
```php
353+
public warmBlocklistCacheUp (void)
354+
```
355+
356+
Used in stream mode only.
357+
358+
This method should be called only to force a cache warm up.
359+
360+
**Parameters**
361+
362+
`This function has no parameters.`
363+
364+
**Return Values**
365+
366+
`int`
367+
368+
> number of decisions added.
240369
241370

242371
<hr />

src/ApiCache.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ public function clear(): bool
313313
{
314314
$cleared = $this->adapter->clear();
315315
$this->warmedUp = false;
316+
$this->defferUpdateCacheConfig(['warmed_up' => $this->warmedUp]);
317+
$this->adapter->commit();
316318
$this->logger->info(null, ['type' => 'CACHE_CLEARED']);
317319
return $cleared;
318320
}
@@ -321,10 +323,15 @@ public function clear(): bool
321323
* Used in stream mode only.
322324
* Warm the cache up.
323325
* Used when the stream mode has just been activated.
326+
*
327+
* @return int number of decisions added.
324328
*
325329
*/
326-
public function warmUp(): void
330+
public function warmUp(): int
327331
{
332+
if ($this->warmedUp) {
333+
$this->clear();
334+
}
328335
$this->logger->debug(null, ['type' => 'START_CACHE_WARMUP']);
329336
$startup = true;
330337
$decisionsDiff = $this->apiClient->getStreamedDecisions($startup);
@@ -333,16 +340,20 @@ public function warmUp(): void
333340
$nbNew = 0;
334341
if ($newDecisions) {
335342
$this->warmedUp = $this->saveRemediations($newDecisions);
343+
$this->defferUpdateCacheConfig(['warmed_up' => $this->warmedUp]);
344+
$this->adapter->commit();
336345
if (!$this->warmedUp) {
337346
throw new BouncerException("Unable to warm the cache up");
338347
}
339348
$nbNew = count($newDecisions);
340349
}
341350

342351
// Store the fact that the cache has been warmed up.
343-
$this->logger->info(null, ['type' => 'CACHE_WARMED_UP', 'added_decisions' => $nbNew]);
344352
$this->defferUpdateCacheConfig(['warmed_up' => true]);
353+
345354
$this->adapter->commit();
355+
$this->logger->info(null, ['type' => 'CACHE_WARMED_UP', 'added_decisions' => $nbNew]);
356+
return $nbNew;
346357
}
347358

348359
/**
@@ -351,12 +362,14 @@ public function warmUp(): void
351362
* Used for the stream mode when we have to update the remediations list.
352363
*
353364
* TODO P2 test for overlapping decisions strategy (ex: max expires)
365+
*
366+
* @return array number of deleted and new decisions.
367+
*
354368
*/
355-
public function pullUpdates(): void
369+
public function pullUpdates(): array
356370
{
357371
if (!$this->warmedUp) {
358-
$this->warmUp();
359-
return;
372+
return ['deleted' => 0, 'new' => $this->warmUp()];
360373
}
361374

362375
$this->logger->debug(null, ['type' => 'START_CACHE_UPDATE']);
@@ -376,7 +389,8 @@ public function pullUpdates(): void
376389
$nbNew = count($newDecisions);
377390
}
378391

379-
$this->logger->debug(null, ['type' => 'CACHE_UPDATED', 'old' => $nbDeleted, 'new' => $nbNew]);
392+
$this->logger->debug(null, ['type' => 'CACHE_UPDATED', 'deleted' => $nbDeleted, 'new' => $nbNew]);
393+
return ['deleted' => $nbDeleted, 'new' => $nbNew];
380394
}
381395

382396
/**

src/Bouncer.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,23 @@ public static function getCaptchaHtmlTemplate(bool $error, string $captchaImageS
142142
/**
143143
* Used in stream mode only.
144144
* This method should be called only to force a cache warm up.
145+
*
146+
* @return int number of decisions added.
145147
*/
146-
public function warmBlocklistCacheUp(): void
148+
public function warmBlocklistCacheUp(): int
147149
{
148-
$this->apiCache->warmUp();
150+
return $this->apiCache->warmUp();
149151
}
150152

151153
/**
152154
* Used in stream mode only.
153155
* This method should be called periodically (ex: crontab) in a asynchronous way to update the bouncer cache.
156+
*
157+
* @return array number of deleted and new decisions.
154158
*/
155-
public function refreshBlocklistCache(): void
159+
public function refreshBlocklistCache(): array
156160
{
157-
$this->apiCache->pullUpdates();
161+
return $this->apiCache->pullUpdates();
158162
}
159163

160164
/**
@@ -170,7 +174,7 @@ public function clearCache(): bool
170174
*/
171175
public function pruneCache(): bool
172176
{
173-
return $this->apiCache->clear();
177+
return $this->apiCache->prune();
174178
}
175179

176180
/**

0 commit comments

Comments
 (0)