Skip to content

Commit

Permalink
Constant cache size
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 5, 2020
1 parent 2ed11f6 commit eeae2da
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 29 deletions.
9 changes: 5 additions & 4 deletions src/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ public function __construct(CacheStorage $storage)
* @param string $key
* @return mixed|null
*/
public function load(string $key)
public function load(string $key, string $variableKey)
{
return $this->storage->load($key);
return $this->storage->load($key, $variableKey);
}

/**
* @param string $key
* @param string $variableKey
* @param mixed $data
* @return void
*/
public function save(string $key, $data): void
public function save(string $key, string $variableKey, $data): void
{
$this->storage->save($key, $data);
$this->storage->save($key, $variableKey, $data);
}

}
46 changes: 46 additions & 0 deletions src/Cache/CacheItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types = 1);

namespace PHPStan\Cache;

class CacheItem
{

/** @var string */
private $variableKey;

/** @var mixed */
private $data;

/**
* @param string $variableKey
* @param mixed $data
*/
public function __construct(string $variableKey, $data)
{
$this->variableKey = $variableKey;
$this->data = $data;
}

public function isVariableKeyValid(string $variableKey): bool
{
return $this->variableKey === $variableKey;
}

/**
* @return mixed
*/
public function getData()
{
return $this->data;
}

/**
* @param mixed[] $properties
* @return self
*/
public static function __set_state(array $properties): self
{
return new self($properties['variableKey'], $properties['data']);
}

}
6 changes: 4 additions & 2 deletions src/Cache/CacheStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ interface CacheStorage

/**
* @param string $key
* @param string $variableKey
* @return mixed|null
*/
public function load(string $key);
public function load(string $key, string $variableKey);

/**
* @param string $key
* @param string $variableKey
* @param mixed $data
* @return void
*/
public function save(string $key, $data): void;
public function save(string $key, string $variableKey, $data): void;

}
26 changes: 20 additions & 6 deletions src/Cache/FileCacheStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,44 @@ private function makeDir(string $directory): void

/**
* @param string $key
* @param string $variableKey
* @return mixed|null
*/
public function load(string $key)
public function load(string $key, string $variableKey)
{
return (function (string $key) {
return (function (string $key, string $variableKey) {
$filePath = $this->getFilePath($key);
return is_file($filePath) ? require $filePath : null;
})($key);
if (!is_file($filePath)) {
return null;
}

$cacheItem = require $filePath;
if (!$cacheItem instanceof CacheItem) {
return null;
}
if (!$cacheItem->isVariableKeyValid($variableKey)) {
return null;
}

return $cacheItem->getData();
})($key, $variableKey);
}

/**
* @param string $key
* @param string $variableKey
* @param mixed $data
* @return void
*/
public function save(string $key, $data): void
public function save(string $key, string $variableKey, $data): void
{
$path = $this->getFilePath($key);
$this->makeDir(dirname($path));
$success = @file_put_contents(
$path,
sprintf(
"<?php declare(strict_types = 1);\n\nreturn %s;",
var_export($data, true)
var_export(new CacheItem($variableKey, $data), true)
)
);
if ($success === false) {
Expand Down
21 changes: 16 additions & 5 deletions src/Cache/MemoryCacheStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,37 @@
class MemoryCacheStorage implements CacheStorage
{

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

/**
* @param string $key
* @param string $variableKey
* @return mixed|null
*/
public function load(string $key)
public function load(string $key, string $variableKey)
{
return $this->storage[$key] ?? null;
if (!isset($this->storage[$key])) {
return null;
}

$item = $this->storage[$key];
if (!$item->isVariableKeyValid($variableKey)) {
return null;
}

return $item->getData();
}

/**
* @param string $key
* @param string $variableKey
* @param mixed $data
* @return void
*/
public function save(string $key, $data): void
public function save(string $key, string $variableKey, $data): void
{
$this->storage[$key] = $data;
$this->storage[$key] = new CacheItem($variableKey, $data);
}

}
7 changes: 4 additions & 3 deletions src/Reflection/Php/PhpFunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,13 @@ private function isVariadic(): bool
if ($modifiedTime === false) {
$modifiedTime = time();
}
$key = sprintf('variadic-function-%s-%s-%d-v1', $functionName, $fileName, $modifiedTime);
$cachedResult = $this->cache->load($key);
$variableCacheKey = sprintf('%d-v1', $modifiedTime);
$key = sprintf('variadic-function-%s-%s', $functionName, $fileName);
$cachedResult = $this->cache->load($key, $variableCacheKey);
if ($cachedResult === null) {
$nodes = $this->parser->parseFile($fileName);
$result = $this->callsFuncGetArgs($nodes);
$this->cache->save($key, $result);
$this->cache->save($key, $variableCacheKey, $result);
return $result;
}

Expand Down
7 changes: 4 additions & 3 deletions src/Reflection/Php/PhpMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,13 @@ private function isVariadic(): bool
if ($modifiedTime === false) {
$modifiedTime = time();
}
$key = sprintf('variadic-method-%s-%s-%s-%d-v2', $declaringClass->getName(), $this->reflection->getName(), $filename, $modifiedTime);
$cachedResult = $this->cache->load($key);
$key = sprintf('variadic-method-%s-%s-%s', $declaringClass->getName(), $this->reflection->getName(), $filename);
$variableCacheKey = sprintf('%d-v2', $modifiedTime);
$cachedResult = $this->cache->load($key, $variableCacheKey);
if ($cachedResult === null || !is_bool($cachedResult)) {
$nodes = $this->parser->parseFile($filename);
$result = $this->callsFuncGetArgs($declaringClass, $nodes);
$this->cache->save($key, $result);
$this->cache->save($key, $variableCacheKey, $result);
return $result;
}

Expand Down
13 changes: 7 additions & 6 deletions src/Type/FileTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ private function resolvePhpDocStringToDocNode(string $phpDocString): PhpDocNode
} catch (\OutOfBoundsException $e) {
// skip
}
$cacheKey = sprintf('phpdocstring-%s-%s', $phpDocString, $phpDocParserVersion);
$phpDocNodeSerializedString = $this->cache->load($cacheKey);
$cacheKey = sprintf('phpdocstring-%s', $phpDocString);
$phpDocNodeSerializedString = $this->cache->load($cacheKey, $phpDocParserVersion);
if ($phpDocNodeSerializedString !== null) {
return unserialize($phpDocNodeSerializedString);
}

$phpDocNode = $this->phpDocStringResolver->resolve($phpDocString);
if ($this->shouldPhpDocNodeBeCachedToDisk($phpDocNode)) {
$this->cache->save($cacheKey, serialize($phpDocNode));
$this->cache->save($cacheKey, $phpDocParserVersion, serialize($phpDocNode));
}

return $phpDocNode;
Expand Down Expand Up @@ -190,12 +190,13 @@ private function getResolvedPhpDocMap(string $fileName): array
if ($modifiedTime === false) {
$modifiedTime = time();
}
$cacheKey = sprintf('%s-phpdocstring-%d', $fileName, $modifiedTime);
$map = $this->cache->load($cacheKey);
$cacheKey = sprintf('%s-phpdocstring', $fileName);
$modifiedTimeString = sprintf('%d', $modifiedTime);
$map = $this->cache->load($cacheKey, $modifiedTimeString);

if ($map === null) {
$map = $this->createResolvedPhpDocMap($fileName);
$this->cache->save($cacheKey, $map);
$this->cache->save($cacheKey, $modifiedTimeString, $map);
}

$this->memoryCache[$fileName] = $map;
Expand Down

0 comments on commit eeae2da

Please sign in to comment.