Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/OPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ PhpFastCache has some options that you may want to know before using them, here'
* **defaultFileNameHashFunction** _| string (default: 'md5')_ `[>=V7]` This option will allow you to define a custom hash function for every I/O method that ends up to write an hashed filename on the disk.
* **preventCacheSlams** _| bool (default: false)_ `[>=V6]` This option will allow you to prevent cache slams when making use of heavy cache items
* **cacheSlamsTimeout** _| int (default: 15)_ `[>=V6]` This option defines the cache slams timeout in seconds
* **useStaticItemCaching** _| bool(default: true)_ `[>=V8.0.3]` This option will allow you to disable the internal static storage of cache items. Can be used for cron script that loop indefinitely on cache items to avoid calling `detachAllItems()`/`detachItem($item)` from the cache pool.

### Host/Authenticating options *
* **host** _| string (default: null)_ The hostname
Expand Down
22 changes: 22 additions & 0 deletions lib/Phpfastcache/Config/ConfigurationOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class ConfigurationOption extends ArrayObject implements ConfigurationOptionInte
*/
protected $cacheSlamsTimeout = 15;

/**
* @var bool
*/
protected $useStaticItemCaching = true;

/**
* @param $args
Expand Down Expand Up @@ -364,4 +368,22 @@ public function setCacheSlamsTimeout(int $cacheSlamsTimeout): self
$this->cacheSlamsTimeout = $cacheSlamsTimeout;
return $this;
}

/**
* @return bool
*/
public function isUseStaticItemCaching(): bool
{
return $this->useStaticItemCaching;
}

/**
* @param bool $useStaticItemCaching
* @return ConfigurationOption
*/
public function setUseStaticItemCaching(bool $useStaticItemCaching): self
{
$this->useStaticItemCaching = $useStaticItemCaching;
return $this;
}
}
4 changes: 3 additions & 1 deletion lib/Phpfastcache/Core/Item/ItemExtendedTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public function __construct(ExtendedCacheItemPoolInterface $driver, $key)
if (\is_string($key)) {
$this->key = $key;
$this->driver = $driver;
$this->driver->setItem($this);
if($driver->getConfig()->isUseStaticItemCaching()){
$this->driver->setItem($this);
}
$this->expirationDate = new DateTime();
if ($this->driver->getConfig()->isItemDetailedDate()) {
$this->creationDate = new DateTime();
Expand Down
41 changes: 28 additions & 13 deletions lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ trait CacheItemPoolTrait
public function setItem(CacheItemInterface $item)
{
if ($this->getClassNamespace() . '\\Item' === \get_class($item)) {
if(!$this->getConfig()->isUseStaticItemCaching()){
throw new PhpfastcacheLogicException(
'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.'
);
}

$this->itemInstances[$item->getKey()] = $item;

return $this;
Expand Down Expand Up @@ -106,12 +112,14 @@ public function getItems(array $keys = [])
public function getItem($key)
{
if (\is_string($key)) {
$item = null;

/**
* Replace array_key_exists by isset
* due to performance issue on huge
* loop dispatching operations
*/
if (!isset($this->itemInstances[$key])) {
if (!isset($this->itemInstances[$key]) || !$this->getConfig()->isUseStaticItemCaching()) {
if (\preg_match('~([' . \preg_quote(self::$unsupportedKeyChars, '~') . ']+)~', $key, $matches)) {
throw new PhpfastcacheInvalidArgumentException(
'Unsupported key character detected: "' . $matches[1] . '". Please check: https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%5D-Unsupported-characters-in-key-identifiers'
Expand Down Expand Up @@ -219,21 +227,26 @@ public function getItem($key)
$item->expiresAfter(abs((int)$this->getConfig()['defaultTtl']));
}
}
}else{
$item = $this->itemInstances[$key];
}
} else {
throw new PhpfastcacheInvalidArgumentException(\sprintf('$key must be a string, got type "%s" instead.', \gettype($key)));
}

/**
* @eventName CacheGetItem
* @param $this ExtendedCacheItemPoolInterface
* @param $this ExtendedCacheItemInterface
*/
$this->eventManager->dispatch('CacheGetItem', $this, $this->itemInstances[$key]);

$this->itemInstances[$key]->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss();
if($item !== null){
/**
* @eventName CacheGetItem
* @param $this ExtendedCacheItemPoolInterface
* @param $this ExtendedCacheItemInterface
*/
$this->eventManager->dispatch('CacheGetItem', $this, $item);

return $this->itemInstances[$key];
$item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss();

return $item;
}
throw new PhpfastcacheInvalidArgumentException(\sprintf('Item %s was not build due to an unknown error', \gettype($key)));
}
throw new PhpfastcacheInvalidArgumentException(\sprintf('$key must be a string, got type "%s" instead.', \gettype($key)));
}

/**
Expand Down Expand Up @@ -388,7 +401,9 @@ public function save(CacheItemInterface $item)
* loop dispatching operations
*/
if (!isset($this->itemInstances[$item->getKey()])) {
$this->itemInstances[$item->getKey()] = $item;
if($this->getConfig()->isUseStaticItemCaching()){
$this->itemInstances[$item->getKey()] = $item;
}
} else {
if (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) {
throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.');
Expand Down
6 changes: 6 additions & 0 deletions lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public function attachItem(CacheItemInterface $item)
);
}

if(!$this->getConfig()->isUseStaticItemCaching()){
throw new PhpfastcacheLogicException(
'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.'
);
}

$this->itemInstances[$item->getKey()] = $item;
}

Expand Down
10 changes: 7 additions & 3 deletions lib/Phpfastcache/Core/Pool/IO/IOHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,20 @@ public function getStats(): DriverStatistic
if (!is_dir($path)) {
throw new PhpfastcacheIOException("Can't read PATH:" . $path);
}

$stat->setData(implode(', ', \array_keys($this->itemInstances)))
->setRawData(
$stat->setRawData(
[
'tmp' => $this->tmp,
]
)
->setSize(Directory::dirSize($path))
->setInfo('Number of files used to build the cache: ' . Directory::getFileCount($path));

if($this->getConfig()->isUseStaticItemCaching()){
$stat->setData(implode(', ', \array_keys($this->itemInstances)));
}else{
$stat->setData('No data available since static item caching option (useStaticItemCaching) is disabled.');
}

return $stat;
}

Expand Down
59 changes: 59 additions & 0 deletions tests/DisabledStaticItemCaching.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*/

use Phpfastcache\CacheManager;
use Phpfastcache\Config\ConfigurationOption;
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
use Phpfastcache\Entities\ItemBatch;
use Phpfastcache\EventManager;
use Phpfastcache\Helper\TestHelper;

chdir(__DIR__);
require_once __DIR__ . '/../vendor/autoload.php';
$testHelper = new TestHelper('Testing disabling static item caching');
$defaultDriver = (!empty($argv[ 1 ]) ? ucfirst($argv[ 1 ]) : 'Files');
$driverInstance = CacheManager::getInstance($defaultDriver, new ConfigurationOption([
'useStaticItemCaching' => false,
]));

if(!$testHelper->isHHVM()){
$testHelper->runSubProcess('DisabledStaticItemCaching');
/**
* Give some time to the
* subprocess to start
* just like a concurrent
* php request
*/
$item = $driverInstance->getItem('TestUseStaticItemCaching');
$item->set('654321-fedcba');
$driverInstance->save($item);
$testHelper->runSubProcess('DisabledStaticItemCaching');
usleep(random_int(250000, 800000));

// We dont want to clear cache instance since we disabled the static item caching
$item = $driverInstance->getItem('TestUseStaticItemCaching');

/**
* @see CacheSlamsProtection.subprocess.php:28
*/
if($item->isHit() && $item->get() === 'abcdef-123456'){
$testHelper->printPassText('The static item caching being disabled, the cache item has been fetched straight from backend.' . $item->get());
}else{
$testHelper->printFailText('The static item caching may not have been disabled since the cache item value does not match the expected value.');
}

/**
* Cleanup the driver
*/
$driverInstance->deleteItem($item->getKey());

}else{
$testHelper->printSkipText('Test ignored on HHVM builds due to sub-process issues with C.I.');
}

$testHelper->terminateTest();
25 changes: 25 additions & 0 deletions tests/subprocess/DisabledStaticItemCaching.subprocess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* @author Georges.L (Geolim4) <contact@geolim4.com>
*/

use Phpfastcache\CacheManager;
use Phpfastcache\Config\ConfigurationOption;
use Phpfastcache\Entities\ItemBatch;

chdir(__DIR__);
require_once __DIR__ . '/../../vendor/autoload.php';

$driverInstance = CacheManager::getInstance('Files', new ConfigurationOption([
'useStaticItemCaching' => false,
]));

/**
* Emulate an active ItemBatch
*/
$batchItem = $driverInstance->getItem('TestUseStaticItemCaching');
$batchItem->set('abcdef-123456');
$driverInstance->save($batchItem);

exit(0);