Skip to content

Commit

Permalink
sha1` is now the default fn for encoding cache keys.
Browse files Browse the repository at this point in the history
Signed-off-by: Serban Ghita <[email protected]>
  • Loading branch information
serbanghita committed Dec 10, 2024
1 parent 604bb14 commit a06fe2e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Change log

# 4.8.09

- [x] `sha1` is now the default fn for encoding cache keys. Using `base64` [was causing problems](https://github.com/serbanghita/Mobile-Detect/issues/974#issuecomment-2531597903) in Laravel.

# 4.8.08

- [x] fix for missing psr/cache prod dependency
- [bug] latest 4.8.07 cause site error Call to a member function get() on false #974
- [x] fix for Docker build not installing dev dependencies

# 4.8.07 (broken in composer)
# 4.8.07 (broken in composer, please skip)

- [x] fix cache and generate short cache key (#971)
- [x] Added configuration cacheKeyFn which allows for using a custom cache key creation fn.
Expand Down
2 changes: 1 addition & 1 deletion MobileDetect.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "4.8.08",
"version": "4.8.09",
"headerMatch": {
"HTTP_ACCEPT": {
"matches": [
Expand Down
7 changes: 4 additions & 3 deletions src/MobileDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @author Nick Ilyin <[email protected]>
* @author: Victor Stanciu <[email protected]> (original author)
*
* @version 4.8.08
* @version 4.8.09
*/

declare(strict_types=1);
Expand Down Expand Up @@ -241,7 +241,7 @@ class MobileDetect
/**
* Stores the version number of the current release.
*/
protected string $VERSION = '4.8.08';
protected string $VERSION = '4.8.09';

protected array $config = [
// Auto-initialization on HTTP headers from $_SERVER['HTTP...']
Expand All @@ -253,7 +253,8 @@ class MobileDetect
// @var int
'maximumUserAgentLength' => 500,
// Function that creates the cache key. e.g. (base64, sha1, custom fn).
'cacheKeyFn' => 'base64_encode',
// Note: used to be base64 but we went with sha1 because of fixed length.
'cacheKeyFn' => 'sha1',
// Cache TTL
// @var null|int|\DateInterval
'cacheTtl' => 86400,
Expand Down
20 changes: 10 additions & 10 deletions tests/MobileDetectWithCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public function testDefaultCacheClassCreatesACacheRecord()
$this->assertTrue($isMobile);
$this->assertEquals(1, $detect->getCache()->count());
$this->assertSame(
"mobile:Some iPhone user agent:",
base64_decode($detect->getCache()->getKeys()[0])
sha1("mobile:Some iPhone user agent:"),
$detect->getCache()->getKeys()[0]
);
}

Expand Down Expand Up @@ -98,20 +98,20 @@ public function testDefaultCacheClassCreatesMultipleCacheRecordsForAllCalls()

$this->assertEquals(4, $detect->getCache()->count());
$this->assertSame(
"mobile:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:",
base64_decode($detect->getCache()->getKeys()[0])
sha1("mobile:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"),
$detect->getCache()->getKeys()[0]
);
$this->assertSame(
"tablet:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:",
base64_decode($detect->getCache()->getKeys()[1])
sha1("tablet:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"),
$detect->getCache()->getKeys()[1]
);
$this->assertSame(
"iPad:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:",
base64_decode($detect->getCache()->getKeys()[2])
sha1("iPad:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"),
$detect->getCache()->getKeys()[2]
);
$this->assertSame(
"iOS:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:",
base64_decode($detect->getCache()->getKeys()[3])
sha1("iOS:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"),
$detect->getCache()->getKeys()[3]
);
}

Expand Down

0 comments on commit a06fe2e

Please sign in to comment.