Skip to content

Commit 6780ec5

Browse files
committed
feat(cache): update device-detector cache configuration options
1 parent e20d7ed commit 6780ec5

5 files changed

Lines changed: 21 additions & 12 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ Available options:
170170
| :--------------------------- | :-------: | :----------------------------------------------------- |
171171
| `cache.interval` | `10080` | Cache TTL in seconds |
172172
| `cache.prefix` | `bd4_` | Cache key prefix |
173-
| `cache.device-detector` | `false` | Enable matomo/device-detector's internal Laravel cache |
173+
| `cache.device-detector` | `null` | Cache driver for device-detector's internal cache. See `config/browser-detect.php` for examples. |
174174
| `security.max-header-length` | `2048` | Max user agent length (DoS protection) |
175175

176176
## Quality

config/browser-detect.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
*/
1212
'prefix' => 'bd4_',
1313
/**
14-
* Enable the device-detector engine's own internal cache via Laravel's cache store.
15-
* When enabled, parsed YAML device definition data is cached by the underlying
16-
* matomo/device-detector library, reducing file reads on repeated parses.
17-
* Requires a Laravel application context — do not enable in standalone mode.
14+
* Cache driver class for the device-detector engine's internal cache. When null,
15+
* the library uses its built-in StaticCache. Override to swap in a different driver.
16+
*
17+
* Examples:
18+
* \DeviceDetector\Cache\StaticCache::class — in-process static cache (default)
19+
* \DeviceDetector\Cache\LaravelCache::class — persists via Laravel's cache store
1820
*/
19-
'device-detector' => false,
21+
'device-detector' => null,
2022
],
2123
'security' => [
2224
/**

src/Parser.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace hisorange\BrowserDetect;
44

5+
use DeviceDetector\Cache\CacheInterface;
56
use hisorange\BrowserDetect\Contracts\ParserInterface;
67
use hisorange\BrowserDetect\Contracts\ResultInterface;
78
use hisorange\BrowserDetect\Contracts\StageInterface;
@@ -202,11 +203,11 @@ protected function makeHashKey(string $agent): string
202203
}
203204

204205
/**
205-
* @return array{interval: int, prefix: string, device-detector: bool}
206+
* @return array{interval: int, prefix: string, device-detector: class-string<CacheInterface>|null}
206207
*/
207208
private function cacheConfig(): array
208209
{
209-
/** @var array{interval: int, prefix: string, device-detector: bool} */
210+
/** @var array{interval: int, prefix: string, device-detector: class-string<CacheInterface>|null} */
210211
return $this->config['cache'];
211212
}
212213

src/Stages/DeviceDetector.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace hisorange\BrowserDetect\Stages;
44

5+
use DeviceDetector\Cache\CacheInterface;
56
use DeviceDetector\Parser\Device\AbstractDeviceParser;
67
use hisorange\BrowserDetect\Contracts\PayloadInterface;
78
use hisorange\BrowserDetect\Contracts\StageInterface;
@@ -13,16 +14,20 @@ class DeviceDetector implements StageInterface
1314
{
1415
protected ?\DeviceDetector\DeviceDetector $detector = null;
1516

16-
public function __construct(protected bool $useDeviceDetectorCache = false) {}
17+
/**
18+
* @param class-string<CacheInterface>|null $deviceDetectorCache
19+
*/
20+
public function __construct(protected ?string $deviceDetectorCache = null) {}
1721

1822
public function __invoke(PayloadInterface $payload): PayloadInterface
1923
{
2024
if ($this->detector === null) {
2125
$this->detector = new \DeviceDetector\DeviceDetector;
2226
// Skip bot detection — CrawlerDetect handles that upstream.
2327
$this->detector->skipBotDetection(true);
24-
if ($this->useDeviceDetectorCache) {
25-
$this->detector->setCache(new \DeviceDetector\Cache\LaravelCache());
28+
if ($this->deviceDetectorCache !== null) {
29+
$cacheClass = $this->deviceDetectorCache;
30+
$this->detector->setCache(new $cacheClass);
2631
}
2732
}
2833
$this->detector->setUserAgent($payload->getAgent());

tests/Stages/DeviceDetectorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace hisorange\BrowserDetect\Test\Stages;
44

5+
use DeviceDetector\Cache\LaravelCache;
56
use hisorange\BrowserDetect\Payload;
67
use hisorange\BrowserDetect\Stages\DeviceDetector;
78
use hisorange\BrowserDetect\Test\TestCase;
@@ -87,7 +88,7 @@ public function test_invoke_with_device_detector_cache_enabled()
8788
// Must be called before the stage runs to intercept cache writes.
8889
Cache::spy();
8990

90-
$stage = new DeviceDetector(useDeviceDetectorCache: true);
91+
$stage = new DeviceDetector(deviceDetectorCache: LaravelCache::class);
9192
$payload = new Payload('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36');
9293
$stage($payload);
9394

0 commit comments

Comments
 (0)