Skip to content

Commit 490abe8

Browse files
committed
Apply review recommendations
1 parent 01f8b4b commit 490abe8

File tree

5 files changed

+17
-19
lines changed

5 files changed

+17
-19
lines changed

system/Cache/Handlers/BaseHandler.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ abstract class BaseHandler implements CacheInterface
2323
{
2424
/**
2525
* Reserved characters that cannot be used in a key or tag.
26-
*
27-
* @see https://github.com/symfony/cache-contracts/blob/c0446463729b89dd4fa62e9aeecc80287323615d/ItemInterface.php#L43
26+
* From https://github.com/symfony/cache-contracts/blob/c0446463729b89dd4fa62e9aeecc80287323615d/ItemInterface.php#L43
2827
*/
2928
public const RESERVED_CHARACTERS = '{}()/\@:';
3029

@@ -44,13 +43,12 @@ abstract class BaseHandler implements CacheInterface
4443
/**
4544
* Validates a cache key according to PSR-6.
4645
* Keys that exceed MAX_KEY_LENGTH are hashed.
46+
* From https://github.com/symfony/cache/blob/7b024c6726af21fd4984ac8d1eae2b9f3d90de88/CacheItem.php#L158
4747
*
4848
* @param string $key The key to validate
4949
* @param string $prefix Optional prefix to include in length calculations
5050
*
5151
* @throws InvalidArgumentException When $key is not valid
52-
*
53-
* @see https://github.com/symfony/cache/blob/7b024c6726af21fd4984ac8d1eae2b9f3d90de88/CacheItem.php#L158
5452
*/
5553
public static function validateKey($key, $prefix = ''): string
5654
{

system/Cache/Handlers/FileHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class FileHandler extends BaseHandler
2323
/**
2424
* Maximum key length.
2525
*/
26-
public const MAX_KEY_LENGTH = 255;
26+
public const MAX_KEY_LENGTH = PHP_MAXPATHLEN;
2727

2828
/**
2929
* Where to store cached files on the disk.

system/Cache/Handlers/WincacheHandler.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public function initialize()
5151
*/
5252
public function get(string $key)
5353
{
54-
$key = static::validateKey($key);
54+
$key = static::validateKey($key, $this->prefix);
5555
$success = false;
5656

57-
$data = wincache_ucache_get($this->prefix . $key, $success);
57+
$data = wincache_ucache_get($key, $success);
5858

5959
// Success returned by reference from wincache_ucache_get()
6060
return $success ? $data : null;
@@ -73,9 +73,9 @@ public function get(string $key)
7373
*/
7474
public function save(string $key, $value, int $ttl = 60)
7575
{
76-
$key = static::validateKey($key);
76+
$key = static::validateKey($key, $this->prefix);
7777

78-
return wincache_ucache_set($this->prefix . $key, $value, $ttl);
78+
return wincache_ucache_set($key, $value, $ttl);
7979
}
8080

8181
//--------------------------------------------------------------------
@@ -89,9 +89,9 @@ public function save(string $key, $value, int $ttl = 60)
8989
*/
9090
public function delete(string $key)
9191
{
92-
$key = static::validateKey($key);
92+
$key = static::validateKey($key, $this->prefix);
9393

94-
return wincache_ucache_delete($this->prefix . $key);
94+
return wincache_ucache_delete($key);
9595
}
9696

9797
//--------------------------------------------------------------------
@@ -120,9 +120,9 @@ public function deleteMatching(string $pattern)
120120
*/
121121
public function increment(string $key, int $offset = 1)
122122
{
123-
$key = static::validateKey($key);
123+
$key = static::validateKey($key, $this->prefix);
124124

125-
return wincache_ucache_inc($this->prefix . $key, $offset);
125+
return wincache_ucache_inc($key, $offset);
126126
}
127127

128128
//--------------------------------------------------------------------
@@ -137,9 +137,9 @@ public function increment(string $key, int $offset = 1)
137137
*/
138138
public function decrement(string $key, int $offset = 1)
139139
{
140-
$key = static::validateKey($key);
140+
$key = static::validateKey($key, $this->prefix);
141141

142-
return wincache_ucache_dec($this->prefix . $key, $offset);
142+
return wincache_ucache_dec($key, $offset);
143143
}
144144

145145
//--------------------------------------------------------------------
@@ -183,9 +183,9 @@ public function getCacheInfo()
183183
*/
184184
public function getMetaData(string $key)
185185
{
186-
$key = static::validateKey($key);
186+
$key = static::validateKey($key, $this->prefix);
187187

188-
if ($stored = wincache_ucache_info(false, $this->prefix . $key))
188+
if ($stored = wincache_ucache_info(false, $key))
189189
{
190190
$age = $stored['ucache_entries'][1]['age_seconds'];
191191
$ttl = $stored['ucache_entries'][1]['ttl_seconds'];

tests/system/Cache/Handlers/BaseHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testValidateKeyInvalidType($input)
1414
$this->expectException('InvalidArgumentException');
1515
$this->expectExceptionMessage('Cache key must be a string');
1616

17-
BaseHandler::validateKey(false);
17+
BaseHandler::validateKey($input);
1818
}
1919

2020
public function invalidTypeProvider(): array

tests/system/Cache/Handlers/FileHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function testSave()
129129

130130
public function testSaveExcessiveKeyLength()
131131
{
132-
$key = str_repeat('a', 300);
132+
$key = str_repeat('a', PHP_MAXPATHLEN + 10);
133133
$file = $this->config->file['storePath'] . DIRECTORY_SEPARATOR . md5($key);
134134

135135
$this->assertTrue($this->fileHandler->save($key, 'value'));

0 commit comments

Comments
 (0)