File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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 /**
Original file line number Diff line number Diff line change 22
33namespace hisorange \BrowserDetect ;
44
5+ use DeviceDetector \Cache \CacheInterface ;
56use hisorange \BrowserDetect \Contracts \ParserInterface ;
67use hisorange \BrowserDetect \Contracts \ResultInterface ;
78use 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
Original file line number Diff line number Diff line change 22
33namespace hisorange \BrowserDetect \Stages ;
44
5+ use DeviceDetector \Cache \CacheInterface ;
56use DeviceDetector \Parser \Device \AbstractDeviceParser ;
67use hisorange \BrowserDetect \Contracts \PayloadInterface ;
78use 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 ());
Original file line number Diff line number Diff line change 22
33namespace hisorange \BrowserDetect \Test \Stages ;
44
5+ use DeviceDetector \Cache \LaravelCache ;
56use hisorange \BrowserDetect \Payload ;
67use hisorange \BrowserDetect \Stages \DeviceDetector ;
78use 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
You can’t perform that action at this time.
0 commit comments