Skip to content

Commit

Permalink
Merge pull request #863 from PHPSocialNetwork/v9
Browse files Browse the repository at this point in the history
Released 9.1.1
  • Loading branch information
Geolim4 committed Apr 15, 2022
2 parents c871f98 + 4de9fce commit 517741c
Show file tree
Hide file tree
Showing 21 changed files with 102 additions and 37 deletions.
14 changes: 11 additions & 3 deletions .scrutinizer.yml
Expand Up @@ -9,9 +9,10 @@
# @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
#

before_commands:
- "./bin/ci/scripts/install_dependencies.sh"
build:
dependencies:
override:
- "composer require -W --ignore-platform-reqs doctrine/couchdb:dev-master phpfastcache/phpssdb:~1.1 predis/predis:~1.1 mongodb/mongodb:~1.9 triagens/arangodb:~3.8 aws/aws-sdk-php:~3.2 google/cloud-firestore:~1.20 solarium/solarium:~6.1"
nodes:
analysis:
project_setup:
Expand All @@ -21,7 +22,14 @@ build:
environment:
php:
version: 8.0.0
checks:
ini:
date.timezone: 'Europe/Paris'
pecl_extensions:
# - couchbase
# - grpc
- redis
- memcache
checks:
php: true
coding_style:
php:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,11 @@
## 9.1.1
##### _Date to be defined_
- __Core__
- Fixed #860 // Cache item throw an error on reading with DateTimeImmutable date objects
- Fixed an issue with tags not properly reinitialized when a backend driver returns an expired cache item
- __Drivers__
- Fixed #862 // Multiple driver errors caused by invalid return type of `driverRead()` (reported by @ShockedPlot7560 and @aemla)

## 9.1.0
##### 04 april 2022
- __API__
Expand Down
9 changes: 8 additions & 1 deletion lib/Phpfastcache/Core/Item/CacheItemTrait.php
Expand Up @@ -102,7 +102,7 @@ public function expiresAt(?\DateTimeInterface $expiration): static
{
if ($expiration instanceof DateTimeInterface) {
$this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AT, $this, $expiration);
$this->expirationDate = $expiration;
$this->expirationDate = $this->demutateDatetime($expiration);
} else {
throw new PhpfastcacheInvalidArgumentException('$expiration must be an object implementing the DateTimeInterface got: ' . \gettype($expiration));
}
Expand Down Expand Up @@ -139,4 +139,11 @@ public function expiresAfter(int|\DateInterval|null $time): static

return $this;
}

protected function demutateDatetime(\DateTimeInterface $dateTime): \DateTimeInterface
{
return $dateTime instanceof \DateTimeImmutable
? \DateTime::createFromImmutable($dateTime)
: $dateTime;
}
}
4 changes: 2 additions & 2 deletions lib/Phpfastcache/Core/Item/ExtendedCacheItemTrait.php
Expand Up @@ -136,7 +136,7 @@ public function getCreationDate(): DateTimeInterface
public function setCreationDate(DateTimeInterface $date): ExtendedCacheItemInterface
{
if ($this->driver->getConfig()->isItemDetailedDate()) {
$this->creationDate = $date;
$this->creationDate = $this->demutateDatetime($date);
return $this;
}

Expand All @@ -163,7 +163,7 @@ public function getModificationDate(): DateTimeInterface
public function setModificationDate(DateTimeInterface $date): ExtendedCacheItemInterface
{
if ($this->driver->getConfig()->isItemDetailedDate()) {
$this->modificationDate = $date;
$this->modificationDate = $this->demutateDatetime($date);
return $this;
}

Expand Down
10 changes: 4 additions & 6 deletions lib/Phpfastcache/Core/Item/TaggableCacheItemTrait.php
Expand Up @@ -90,12 +90,10 @@ public function hasTags(array $tagNames, int $strategy = TaggableCacheItemInterf
*/
public function setTags(array $tags): ExtendedCacheItemInterface
{
if (\count($tags)) {
if (\array_filter($tags, 'is_string')) {
$this->tags = $tags;
} else {
throw new PhpfastcacheInvalidArgumentException('$tagName must be an array of string');
}
if ($tags === [] || \array_filter($tags, 'is_string')) {
$this->tags = $tags;
} else {
throw new PhpfastcacheInvalidArgumentException('$tagName must be an array of string');
}

return $this;
Expand Down
20 changes: 10 additions & 10 deletions lib/Phpfastcache/Core/Pool/DriverBaseTrait.php
Expand Up @@ -203,11 +203,11 @@ public function driverUnwrapData(array $wrapper): mixed

/**
* @param array $wrapper
* @return DateTime
* @return DateTimeInterface
*/
public function driverUnwrapEdate(array $wrapper): \DateTime
public function driverUnwrapEdate(array $wrapper): \DateTimeInterface
{
if ($wrapper[self::DRIVER_EDATE_WRAPPER_INDEX] instanceof \DateTime) {
if ($wrapper[self::DRIVER_EDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) {
return $wrapper[self::DRIVER_EDATE_WRAPPER_INDEX];
}

Expand All @@ -216,11 +216,11 @@ public function driverUnwrapEdate(array $wrapper): \DateTime

/**
* @param array $wrapper
* @return DateTime|null
* @return DateTimeInterface|null
*/
public function driverUnwrapCdate(array $wrapper): ?\DateTime
public function driverUnwrapCdate(array $wrapper): ?\DateTimeInterface
{
if ($wrapper[self::DRIVER_CDATE_WRAPPER_INDEX] instanceof \DateTime) {
if ($wrapper[self::DRIVER_CDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) {
return $wrapper[self::DRIVER_CDATE_WRAPPER_INDEX];
}

Expand All @@ -229,11 +229,11 @@ public function driverUnwrapCdate(array $wrapper): ?\DateTime

/**
* @param array $wrapper
* @return DateTime|null
* @return DateTimeInterface|null
*/
public function driverUnwrapMdate(array $wrapper): ?\DateTime
public function driverUnwrapMdate(array $wrapper): ?\DateTimeInterface
{
if ($wrapper[self::DRIVER_MDATE_WRAPPER_INDEX] instanceof \DateTime) {
if ($wrapper[self::DRIVER_MDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) {
return $wrapper[self::DRIVER_MDATE_WRAPPER_INDEX];
}

Expand Down Expand Up @@ -269,6 +269,6 @@ protected function encode($data): string
*/
protected function decode(?string $value): mixed
{
return \unserialize((string) $value, ['allowed_classes' => true]);
return $value ? \unserialize($value, ['allowed_classes' => true]) : null;
}
}
2 changes: 1 addition & 1 deletion lib/Phpfastcache/Drivers/Apcu/Driver.php
Expand Up @@ -92,7 +92,7 @@ protected function driverRead(ExtendedCacheItemInterface $item): ?array
{
$data = apcu_fetch($item->getKey(), $success);

if ($success === false) {
if ($success === false || !\is_array($data)) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Phpfastcache/Drivers/Arangodb/Driver.php
Expand Up @@ -131,7 +131,7 @@ protected function driverRead(ExtendedCacheItemInterface $item): ?array
return null;
}
throw new PhpfastcacheDriverException(
'Got unexpeced error from Arangodb: ' . $e->getMessage()
'Got unexpected error from Arangodb: ' . $e->getMessage()
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Phpfastcache/Drivers/Cassandra/Driver.php
Expand Up @@ -140,7 +140,7 @@ protected function driverRead(ExtendedCacheItemInterface $item): ?array
$results = $this->instance->execute(new Cassandra\SimpleStatement($query), $options);

if ($results instanceof Cassandra\Rows && $results->count() === 1) {
return $this->decode($results->first()['cache_data']);
return $this->decode($results->first()['cache_data']) ?: null;
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion lib/Phpfastcache/Drivers/Couchdb/Driver.php
Expand Up @@ -257,7 +257,7 @@ protected function encodeDocument(array $data): array
* @return mixed
* @throws \Exception
*/
protected function decode($value): mixed
protected function decode($value): array
{
$value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = \unserialize(
$value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX],
Expand Down
2 changes: 1 addition & 1 deletion lib/Phpfastcache/Drivers/Memcache/Driver.php
Expand Up @@ -149,7 +149,7 @@ protected function driverRead(ExtendedCacheItemInterface $item): ?array
{
$val = $this->instance->get($item->getKey());

if ($val === false) {
if (empty($val) || !\is_array($val)) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Phpfastcache/Drivers/Memcached/Driver.php
Expand Up @@ -147,7 +147,7 @@ protected function driverRead(ExtendedCacheItemInterface $item): ?array
{
$val = $this->instance->get($item->getKey());

if ($val === false) {
if (empty($val) || !\is_array($val)) {
return null;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Phpfastcache/Drivers/Predis/Driver.php
Expand Up @@ -143,7 +143,8 @@ protected function driverConnect(): bool
protected function driverRead(ExtendedCacheItemInterface $item): ?array
{
$val = $this->instance->get($item->getKey());
if ($val == false) {

if ($val === null) {
return null;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Phpfastcache/Drivers/Redis/Driver.php
Expand Up @@ -57,8 +57,9 @@ public function getStats(): DriverStatistic
->setSize((int)$info['used_memory'])
->setInfo(
sprintf(
"The Redis daemon v%s is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.",
"The Redis daemon v%s, php-ext v%s, is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.",
$info['redis_version'],
\phpversion("redis"),
$date->format(DATE_RFC2822)
)
);
Expand Down
3 changes: 2 additions & 1 deletion lib/Phpfastcache/Drivers/Ssdb/Driver.php
Expand Up @@ -94,7 +94,8 @@ protected function driverConnect(): bool
protected function driverRead(ExtendedCacheItemInterface $item): ?array
{
$val = $this->instance->get($item->getEncodedKey());
if (!$val) {

if (empty($val)) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Phpfastcache/Drivers/Wincache/Driver.php
Expand Up @@ -72,7 +72,7 @@ protected function driverRead(ExtendedCacheItemInterface $item): ?array
{
$val = wincache_ucache_get($item->getKey(), $suc);

if ($suc === false) {
if ($suc === false || empty($val)) {
return null;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Phpfastcache/Drivers/Zenddisk/Driver.php
Expand Up @@ -80,7 +80,8 @@ protected function driverConnect(): bool
protected function driverRead(ExtendedCacheItemInterface $item): ?array
{
$data = zend_disk_cache_fetch($item->getKey());
if ($data === false) {

if (empty($data) || !\is_array($data)) {
return null;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Phpfastcache/Drivers/Zendshm/Driver.php
Expand Up @@ -79,7 +79,8 @@ protected function driverConnect(): bool
protected function driverRead(ExtendedCacheItemInterface $item): ?array
{
$data = zend_shm_cache_fetch($item->getKey());
if ($data === false) {

if (empty($data) || !\is_array($data)) {
return null;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Memcached.test.php
Expand Up @@ -13,6 +13,7 @@
*/

use Phpfastcache\CacheManager;
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
use Phpfastcache\Drivers\Memcached\Config as MemcachedConfig;
use Phpfastcache\Tests\Helper\TestHelper;

Expand Down Expand Up @@ -82,4 +83,6 @@
$cacheInstanceDefSyntax->clear();
$cacheInstanceOldSyntax->clear();
$cacheInstanceNewSyntax->clear();

$testHelper->runCRUDTests($cacheInstanceNewSyntax);
$testHelper->terminateTest();
35 changes: 35 additions & 0 deletions tests/issues/Github-860.test.php
@@ -0,0 +1,35 @@
<?php

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

use Phpfastcache\CacheManager;
use Phpfastcache\Drivers\Files\Config as FilesConfig;
use Phpfastcache\Tests\Helper\TestHelper;

chdir(__DIR__);
require_once __DIR__ . '/../../vendor/autoload.php';
$testHelper = new TestHelper('Github issue #860 - Cache item throw an error on reading with DateTimeImmutable date objects');

$config = new FilesConfig();
$testHelper->preConfigure($config);
$cacheInstance = CacheManager::getInstance('Files', $config);
$cacheInstance->clear();

try {
$key = 'pfc_' . bin2hex(random_bytes(12));
$item = $cacheInstance->getItem($key);
$item->set(random_int(1000, 999999))
->setExpirationDate(new DateTimeImmutable('+1 month'))
->setCreationDate(new DateTimeImmutable())
->setModificationDate(new DateTimeImmutable('+1 week'));
$cacheInstance->save($item);
$cacheInstance->detachAllItems();
$item = $cacheInstance->getItem($key);
$testHelper->assertPass('Github issue #860 have not regressed.');
} catch (\TypeError $e) {
$testHelper->assertFail('Github issue #860 have regressed, exception caught: ' . $e->getMessage());
}

7 changes: 4 additions & 3 deletions tests/lib/Helper/TestHelper.php
Expand Up @@ -550,11 +550,12 @@ public function runCRUDTests(ExtendedCacheItemPoolInterface|PhpfastcacheAbstract
$pool->getIO()->getWriteHit()
)
);
$this->printInfoText('<yellow>Driver info</yellow>: <magenta>' . $pool->getStats()->getInfo() . '</magenta>');
$poolSize = $pool->getStats()->getSize();
$stats = $pool->getStats();
$this->printInfoText('<yellow>Driver info</yellow>: <magenta>' . $stats->getInfo() . '</magenta>');
$poolSize = $stats->getSize();

if($poolSize){
$this->printInfoText('<yellow>Driver size</yellow> (approximative): <magenta>' . round($pool->getStats()->getSize() / (1024 ** 2), 3) . ' Mo</magenta>');
$this->printInfoText('<yellow>Driver size</yellow> (approximative): <magenta>' . round($stats->getSize() / (1024 ** 2), 3) . ' Mo</magenta>');
}
}

Expand Down

0 comments on commit 517741c

Please sign in to comment.