Skip to content

fix new cache instance cannot clean expired cache item #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 0 additions & 11 deletions lib/Phpfastcache/Extensions/Drivers/Ravendb/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,6 @@ public function isSerializeData(): bool
return $this->serializeData;
}

/**
* @param bool $serializeData
* @return self
* @throws PhpfastcacheInvalidArgumentException
* @throws PhpfastcacheLogicException
*/
public function setSerializeData(bool $serializeData): static
{
return $this->setProperty('serializeData', $serializeData);
}

public function getAuthOptions(): ?AuthOptions
{
return $this->authOptions;
Expand Down
1 change: 0 additions & 1 deletion lib/Phpfastcache/Extensions/Drivers/Ravendb/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ protected function getRavenDocument(ExtendedCacheItemInterface $item): ?RavenPro

if ($ravenDocument instanceof RavenProxy) {
$ravenDocument->setDetailedDate($this->getConfig()->isItemDetailedDate());
$ravenDocument->setSerializeData($this->getConfig()->isSerializeData());
return $ravenDocument;
}

Expand Down
18 changes: 7 additions & 11 deletions lib/Phpfastcache/Extensions/Drivers/Ravendb/RavenProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class RavenProxy

protected ?\DateTimeInterface $modificationDate = null;

public function __construct(?ExtendedCacheItemInterface $item = null, protected bool $serializeData = true, protected bool $detailedDate = false)
public function __construct(?ExtendedCacheItemInterface $item = null, protected bool $detailedDate = false)
{
if ($item) {
$this->fromCacheItem($item);
Expand Down Expand Up @@ -70,8 +70,7 @@ public function getData(): mixed

public function setData(mixed $data): self
{
$this->data = $this->serializeData ? serialize($data) : $data;

$this->data = $data;
return $this;
}

Expand Down Expand Up @@ -133,8 +132,10 @@ public function fromCacheItem(ExtendedCacheItemInterface $item): void
}

$this->setData($item->_getData())
->setExpirationDate($item->getExpirationDate())
->setTags($item->getTags());
->setExpirationDate($item->getExpirationDate())
->setCreationDate($item->getCreationDate())
->setModificationDate($item->getModificationDate())
->setTags($item->getTags());

if ($this->detailedDate) {
$this->setModificationDate($item->getModificationDate())
Expand All @@ -150,7 +151,7 @@ public function toDriverArray(): ?array
if ($this->key) {
return [
ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX => $this->key,
ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX => ($this->serializeData ? unserialize($this->data) : $this->data),
ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX => $this->data,
ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX => $this->expirationDate,
ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX => $this->creationDate,
ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX => $this->modificationDate,
Expand All @@ -160,11 +161,6 @@ public function toDriverArray(): ?array
return null;
}

public function setSerializeData(bool $serializeData): void
{
$this->serializeData = $serializeData;
}

public function setDetailedDate(bool $detailedDate): void
{
$this->detailedDate = $detailedDate;
Expand Down
1 change: 0 additions & 1 deletion tests/Configs/github-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use Phpfastcache\Drivers\Ravendb\Config as RavendbConfig;

return (new RavendbConfig())
->setSerializeData(false)
->setItemDetailedDate(true)
->setHost([getenv('RAVENDB_TEST_DATABASE_HOSTNAME') ?: 'http://127.0.0.1:8082'])
->setCollectionName('phpfastcache')
Expand Down
17 changes: 17 additions & 0 deletions tests/Ravendb.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@
$cacheInstance = CacheManager::getInstance('Ravendb', include $configFileName);
$testHelper->runCRUDTests($cacheInstance);
$testHelper->runGetAllItemsTests($cacheInstance);

$key = "product_page". bin2hex(random_bytes(8) . '_' . random_int(100, 999));
$your_product_data = 'First product';

$cachedString = $cacheInstance->getItem($key);
$cachedString->set($your_product_data)->expiresAfter(1);
$cacheInstance->save($cachedString);
sleep(2);

$newCacheInstance = CacheManager::getInstance('RavenDB', include $configFileName, "newInstance");
$cachedString = $newCacheInstance->getItem($key); // new cache instance got an expired cache item and should delete it from cache storage

if ($cachedString->isHit()) {
$testHelper->assertFail(sprintf('Item #%s is hit.', $cachedString->getKey()));
} else {
$testHelper->assertPass(sprintf('Item #%s is not hit.', $cachedString->getKey()));
}
} catch (PhpfastcacheDriverConnectException $e) {
$testHelper->assertSkip('Ravendb server unavailable: ' . $e->getMessage());
$testHelper->terminateTest();
Expand Down