Skip to content

Commit eeae2da

Browse files
committed
Constant cache size
1 parent 2ed11f6 commit eeae2da

8 files changed

+106
-29
lines changed

src/Cache/Cache.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@ public function __construct(CacheStorage $storage)
1717
* @param string $key
1818
* @return mixed|null
1919
*/
20-
public function load(string $key)
20+
public function load(string $key, string $variableKey)
2121
{
22-
return $this->storage->load($key);
22+
return $this->storage->load($key, $variableKey);
2323
}
2424

2525
/**
2626
* @param string $key
27+
* @param string $variableKey
2728
* @param mixed $data
2829
* @return void
2930
*/
30-
public function save(string $key, $data): void
31+
public function save(string $key, string $variableKey, $data): void
3132
{
32-
$this->storage->save($key, $data);
33+
$this->storage->save($key, $variableKey, $data);
3334
}
3435

3536
}

src/Cache/CacheItem.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Cache;
4+
5+
class CacheItem
6+
{
7+
8+
/** @var string */
9+
private $variableKey;
10+
11+
/** @var mixed */
12+
private $data;
13+
14+
/**
15+
* @param string $variableKey
16+
* @param mixed $data
17+
*/
18+
public function __construct(string $variableKey, $data)
19+
{
20+
$this->variableKey = $variableKey;
21+
$this->data = $data;
22+
}
23+
24+
public function isVariableKeyValid(string $variableKey): bool
25+
{
26+
return $this->variableKey === $variableKey;
27+
}
28+
29+
/**
30+
* @return mixed
31+
*/
32+
public function getData()
33+
{
34+
return $this->data;
35+
}
36+
37+
/**
38+
* @param mixed[] $properties
39+
* @return self
40+
*/
41+
public static function __set_state(array $properties): self
42+
{
43+
return new self($properties['variableKey'], $properties['data']);
44+
}
45+
46+
}

src/Cache/CacheStorage.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ interface CacheStorage
77

88
/**
99
* @param string $key
10+
* @param string $variableKey
1011
* @return mixed|null
1112
*/
12-
public function load(string $key);
13+
public function load(string $key, string $variableKey);
1314

1415
/**
1516
* @param string $key
17+
* @param string $variableKey
1618
* @param mixed $data
1719
* @return void
1820
*/
19-
public function save(string $key, $data): void;
21+
public function save(string $key, string $variableKey, $data): void;
2022

2123
}

src/Cache/FileCacheStorage.php

+20-6
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,44 @@ private function makeDir(string $directory): void
2323

2424
/**
2525
* @param string $key
26+
* @param string $variableKey
2627
* @return mixed|null
2728
*/
28-
public function load(string $key)
29+
public function load(string $key, string $variableKey)
2930
{
30-
return (function (string $key) {
31+
return (function (string $key, string $variableKey) {
3132
$filePath = $this->getFilePath($key);
32-
return is_file($filePath) ? require $filePath : null;
33-
})($key);
33+
if (!is_file($filePath)) {
34+
return null;
35+
}
36+
37+
$cacheItem = require $filePath;
38+
if (!$cacheItem instanceof CacheItem) {
39+
return null;
40+
}
41+
if (!$cacheItem->isVariableKeyValid($variableKey)) {
42+
return null;
43+
}
44+
45+
return $cacheItem->getData();
46+
})($key, $variableKey);
3447
}
3548

3649
/**
3750
* @param string $key
51+
* @param string $variableKey
3852
* @param mixed $data
3953
* @return void
4054
*/
41-
public function save(string $key, $data): void
55+
public function save(string $key, string $variableKey, $data): void
4256
{
4357
$path = $this->getFilePath($key);
4458
$this->makeDir(dirname($path));
4559
$success = @file_put_contents(
4660
$path,
4761
sprintf(
4862
"<?php declare(strict_types = 1);\n\nreturn %s;",
49-
var_export($data, true)
63+
var_export(new CacheItem($variableKey, $data), true)
5064
)
5165
);
5266
if ($success === false) {

src/Cache/MemoryCacheStorage.php

+16-5
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,37 @@
55
class MemoryCacheStorage implements CacheStorage
66
{
77

8-
/** @var mixed[] */
8+
/** @var array<string, \PHPStan\Cache\CacheItem> */
99
private $storage = [];
1010

1111
/**
1212
* @param string $key
13+
* @param string $variableKey
1314
* @return mixed|null
1415
*/
15-
public function load(string $key)
16+
public function load(string $key, string $variableKey)
1617
{
17-
return $this->storage[$key] ?? null;
18+
if (!isset($this->storage[$key])) {
19+
return null;
20+
}
21+
22+
$item = $this->storage[$key];
23+
if (!$item->isVariableKeyValid($variableKey)) {
24+
return null;
25+
}
26+
27+
return $item->getData();
1828
}
1929

2030
/**
2131
* @param string $key
32+
* @param string $variableKey
2233
* @param mixed $data
2334
* @return void
2435
*/
25-
public function save(string $key, $data): void
36+
public function save(string $key, string $variableKey, $data): void
2637
{
27-
$this->storage[$key] = $data;
38+
$this->storage[$key] = new CacheItem($variableKey, $data);
2839
}
2940

3041
}

src/Reflection/Php/PhpFunctionReflection.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,13 @@ private function isVariadic(): bool
171171
if ($modifiedTime === false) {
172172
$modifiedTime = time();
173173
}
174-
$key = sprintf('variadic-function-%s-%s-%d-v1', $functionName, $fileName, $modifiedTime);
175-
$cachedResult = $this->cache->load($key);
174+
$variableCacheKey = sprintf('%d-v1', $modifiedTime);
175+
$key = sprintf('variadic-function-%s-%s', $functionName, $fileName);
176+
$cachedResult = $this->cache->load($key, $variableCacheKey);
176177
if ($cachedResult === null) {
177178
$nodes = $this->parser->parseFile($fileName);
178179
$result = $this->callsFuncGetArgs($nodes);
179-
$this->cache->save($key, $result);
180+
$this->cache->save($key, $variableCacheKey, $result);
180181
return $result;
181182
}
182183

src/Reflection/Php/PhpMethodReflection.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,13 @@ private function isVariadic(): bool
281281
if ($modifiedTime === false) {
282282
$modifiedTime = time();
283283
}
284-
$key = sprintf('variadic-method-%s-%s-%s-%d-v2', $declaringClass->getName(), $this->reflection->getName(), $filename, $modifiedTime);
285-
$cachedResult = $this->cache->load($key);
284+
$key = sprintf('variadic-method-%s-%s-%s', $declaringClass->getName(), $this->reflection->getName(), $filename);
285+
$variableCacheKey = sprintf('%d-v2', $modifiedTime);
286+
$cachedResult = $this->cache->load($key, $variableCacheKey);
286287
if ($cachedResult === null || !is_bool($cachedResult)) {
287288
$nodes = $this->parser->parseFile($filename);
288289
$result = $this->callsFuncGetArgs($declaringClass, $nodes);
289-
$this->cache->save($key, $result);
290+
$this->cache->save($key, $variableCacheKey, $result);
290291
return $result;
291292
}
292293

src/Type/FileTypeMapper.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ private function resolvePhpDocStringToDocNode(string $phpDocString): PhpDocNode
152152
} catch (\OutOfBoundsException $e) {
153153
// skip
154154
}
155-
$cacheKey = sprintf('phpdocstring-%s-%s', $phpDocString, $phpDocParserVersion);
156-
$phpDocNodeSerializedString = $this->cache->load($cacheKey);
155+
$cacheKey = sprintf('phpdocstring-%s', $phpDocString);
156+
$phpDocNodeSerializedString = $this->cache->load($cacheKey, $phpDocParserVersion);
157157
if ($phpDocNodeSerializedString !== null) {
158158
return unserialize($phpDocNodeSerializedString);
159159
}
160160

161161
$phpDocNode = $this->phpDocStringResolver->resolve($phpDocString);
162162
if ($this->shouldPhpDocNodeBeCachedToDisk($phpDocNode)) {
163-
$this->cache->save($cacheKey, serialize($phpDocNode));
163+
$this->cache->save($cacheKey, $phpDocParserVersion, serialize($phpDocNode));
164164
}
165165

166166
return $phpDocNode;
@@ -190,12 +190,13 @@ private function getResolvedPhpDocMap(string $fileName): array
190190
if ($modifiedTime === false) {
191191
$modifiedTime = time();
192192
}
193-
$cacheKey = sprintf('%s-phpdocstring-%d', $fileName, $modifiedTime);
194-
$map = $this->cache->load($cacheKey);
193+
$cacheKey = sprintf('%s-phpdocstring', $fileName);
194+
$modifiedTimeString = sprintf('%d', $modifiedTime);
195+
$map = $this->cache->load($cacheKey, $modifiedTimeString);
195196

196197
if ($map === null) {
197198
$map = $this->createResolvedPhpDocMap($fileName);
198-
$this->cache->save($cacheKey, $map);
199+
$this->cache->save($cacheKey, $modifiedTimeString, $map);
199200
}
200201

201202
$this->memoryCache[$fileName] = $map;

0 commit comments

Comments
 (0)