Skip to content

Commit fb57538

Browse files
authored
Merge pull request #6 from kodedphp/2
Start PHP 8 version
2 parents fcc822b + 42fc324 commit fb57538

20 files changed

+170
-125
lines changed

.scrutinizer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build:
77
- php-scrutinizer-run
88
environment:
99
php:
10-
version: '7.3'
10+
version: '8.0.1'
1111

1212
before_commands:
1313
- 'composer update -o --prefer-source --no-interaction'

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: php
22
os: linux
3-
dist: xenial
3+
dist: bionic
44

55
notifications:
66
email: false
@@ -10,9 +10,7 @@ cache:
1010
- $HOME/.composer/cache
1111

1212
php:
13-
- 7.2
14-
- 7.3
15-
- 7.4
13+
- 8.0.1
1614
- nightly
1715

1816
jobs:

CacheItem.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,52 @@
44

55
use Psr\Cache\CacheItemInterface;
66

7-
87
abstract class CacheItem implements CacheItemInterface
98
{
10-
/** @var bool */
11-
protected $isHit = false;
12-
13-
/** @var string Expects a valid cache key name */
14-
private $key;
15-
16-
/** @var mixed */
17-
private $value;
18-
19-
/** @var int Number of seconds for the expiration time */
20-
private $expiresAt;
21-
9+
protected bool $isHit = false;
10+
private string $key;
11+
private mixed $value = null;
12+
private ?int $expiresAt;
2213

2314
public function __construct($key, ?int $ttl)
2415
{
2516
$this->key = $key;
2617
$this->expiresAt = $ttl;
2718
}
2819

29-
3020
public function getKey(): string
3121
{
3222
return $this->key;
3323
}
3424

35-
3625
public function get()
3726
{
3827
return $this->value;
3928
}
4029

41-
4230
public function isHit(): bool
4331
{
4432
return $this->isHit;
4533
}
4634

47-
4835
public function set($value)
4936
{
5037
$this->value = $value;
51-
5238
return $this;
5339
}
5440

55-
56-
public function expiresAfter($time)
41+
public function expiresAfter($time): static
5742
{
5843
// The TTL is calculated in the cache client instance
5944
return $this->expiresAt($time);
6045
}
6146

62-
63-
public function expiresAt($expiration)
47+
public function expiresAt($expiration): static
6448
{
6549
$this->expiresAt = normalize_ttl($expiration ?? $this->expiresAt);
66-
6750
if ($this->expiresAt < 1) {
6851
$this->isHit = false;
6952
}
70-
7153
return $this;
7254
}
7355

CacheItemPool.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66
use Psr\Cache\{CacheItemInterface, CacheItemPoolInterface};
77
use function Koded\Stdlib\now;
88

9-
109
abstract class CacheItemPool implements CacheItemPoolInterface
1110
{
12-
/** @var Cache */
13-
protected $client;
11+
protected Cache $client;
1412

1513
/** @var CacheItemInterface[] */
16-
private $deferred = [];
17-
14+
private array $deferred = [];
1815

1916
abstract public function __construct(string $client, array $parameters);
2017

@@ -32,34 +29,28 @@ public function commit(): bool
3229
unset($this->deferred[$key]);
3330
}
3431
}
35-
3632
return empty($this->deferred);
3733
}
3834

39-
4035
public function save(CacheItemInterface $item): bool
4136
{
4237
/** @var CacheItem $item */
4338
return $this->client->set($item->getKey(), $item->get(), $item->getExpiresAt());
4439
}
4540

46-
4741
public function getItems(array $keys = []): array
4842
{
4943
$items = [];
5044
foreach ($keys as $key) {
5145
$items[$key] = $this->getItem($key);
5246
}
53-
5447
return $items;
5548
}
5649

57-
5850
public function getItem($key): CacheItemInterface
5951
{
6052
try {
6153
$item = new class($key, $this->client->getTtl()) extends CacheItem {};
62-
6354
if (false === $this->client->has($key)) {
6455
if (isset($this->deferred[$key])) {
6556
return clone $this->deferred[$key];
@@ -79,7 +70,6 @@ public function getItem($key): CacheItemInterface
7970
}
8071
}
8172

82-
8373
public function hasItem($key): bool
8474
{
8575
try {
@@ -89,17 +79,14 @@ public function hasItem($key): bool
8979
}
9080
}
9181

92-
9382
public function clear(): bool
9483
{
9584
if ($cleared = $this->client->clear()) {
9685
$this->deferred = [];
9786
}
98-
9987
return $cleared;
10088
}
10189

102-
10390
public function deleteItems(array $keys): bool
10491
{
10592
try {
@@ -109,22 +96,18 @@ public function deleteItems(array $keys): bool
10996
}
11097
}
11198

112-
11399
public function deleteItem($key): bool
114100
{
115101
try {
116102
if ($deleted = $this->client->delete($key)) {
117103
unset($this->deferred[$key]);
118104
}
119-
120105
return $deleted;
121-
122106
} catch (Exception $e) {
123107
throw CachePoolException::from($e);
124108
}
125109
}
126110

127-
128111
public function saveDeferred(CacheItemInterface $item): bool
129112
{
130113
/** @var CacheItem $item */
@@ -139,4 +122,16 @@ public function saveDeferred(CacheItemInterface $item): bool
139122

140123
return true;
141124
}
142-
}
125+
126+
/**
127+
* Returns the instance of the underlying cache client.
128+
*
129+
* This method is not part of the PSR-6.
130+
*
131+
* @return Cache
132+
*/
133+
public function client(): Cache
134+
{
135+
return $this->client;
136+
}
137+
}

CachePool.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,20 @@
2222
*/
2323
final class CachePool
2424
{
25-
/**
26-
* @var CacheItemPoolInterface[] The registry
27-
*/
28-
private static $instances = [];
25+
/** @var CacheItemPoolInterface[] The registry */
26+
private static array $instances = [];
2927

3028
/**
3129
* @param string $client The name of the cache client
3230
* @param array $parameters [optional] Configuration parameters for the cache client
33-
*
3431
* @return CacheItemPoolInterface
3532
*/
3633
public static function use(string $client, array $parameters = []): CacheItemPoolInterface
3734
{
38-
$ident = md5($client . serialize($parameters));
39-
35+
$ident = \md5($client . \serialize($parameters));
4036
if (isset(self::$instances[$ident])) {
4137
return self::$instances[$ident];
4238
}
43-
4439
return self::$instances[$ident] = new class($client, $parameters) extends CacheItemPool
4540
{
4641
public function __construct(string $client, array $parameters)
@@ -57,8 +52,8 @@ public function __construct(string $client, array $parameters)
5752
*/
5853
class CachePoolException extends Exception implements InvalidArgumentException
5954
{
60-
public static function from(Exception $e)
55+
public static function from(Exception $e): static
6156
{
62-
return new self($e->getMessage(), $e->getCode());
57+
return new static($e->getMessage(), $e->getCode());
6358
}
64-
}
59+
}

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BSD 3-Clause License
22

3-
Copyright (c) 2019, Mihail Binev
3+
Copyright (c) 2021, Mihail Binev
44
All rights reserved.
55

66
Redistribution and use in source and binary forms, with or without

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Koded - Extended Caching Library
55
[![Build Status](https://travis-ci.org/kodedphp/cache-extended.svg?branch=master)](https://travis-ci.org/kodedphp/cache-extended)
66
[![Code Coverage](https://scrutinizer-ci.com/g/kodedphp/cache-extended/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/kodedphp/cache-extended/?branch=master)
77
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/kodedphp/cache-extended/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/kodedphp/cache-extended/?branch=master)
8-
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.2-8892BF.svg)](https://php.net/)
8+
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.0-8892BF.svg)](https://php.net/)
99
[![Software license](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](LICENSE)
1010

1111
A [PSR-6][psr-6] caching library for PHP 7 using several caching technologies.
@@ -15,7 +15,7 @@ Requirements
1515
------------
1616

1717
- Linux machine
18-
- PHP 7.1 or higher
18+
- PHP 8
1919

2020
Recommended cache technologies are
2121

@@ -59,7 +59,7 @@ Please see the README in that repository for the specific arguments.
5959
You can grab the cache client if you want to use it directly
6060

6161
```php
62-
/** $var Psr\SimpleCache\CacheInterface $client */
62+
/** $var Koded\Caching\Cache $client */
6363
$client = $cache->client();
6464
```
6565

Tests/CacheItemTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
namespace Koded\Caching;
3+
namespace Tests\Koded\Caching;
44

5+
use Koded\Caching\CachePool;
56
use PHPUnit\Framework\TestCase;
67

78
class CacheItemTest extends TestCase
89
{
9-
1010
public function test_expiresAfter_with_global_ttl()
1111
{
1212
$pool = CachePool::use('memory', ['ttl' => 60]);

Tests/CachePoolFactoryTest.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
<?php
22

3-
namespace Koded\Caching;
3+
namespace Tests\Koded\Caching;
44

5-
use Koded\Caching\Client\{FileClient, MemcachedClient, MemoryClient, PredisClient, RedisClient};
5+
use Koded\Caching\Client\{FileClient, MemcachedClient, MemoryClient, PredisClient, RedisClient, ShmopClient};
6+
use Koded\Caching\CachePool;
67
use PHPUnit\Framework\TestCase;
78

89
class CachePoolFactoryTest extends TestCase
910
{
10-
1111
public function test_MemcachedClient()
1212
{
13+
if (false === \extension_loaded('memcached')) {
14+
$this->markTestSkipped('memcached extension is not loaded');
15+
}
1316
$pool = CachePool::use('memcached');
1417
$this->assertAttributeInstanceOf(MemcachedClient::class, 'client', $pool);
1518
}
1619

1720
public function test_RedisClient()
1821
{
22+
if (false === \extension_loaded('redis')) {
23+
$this->markTestSkipped('redis extension is not loaded');
24+
}
1925
$pool = CachePool::use('redis', [
2026
'host' => getenv('REDIS_SERVER_HOST'),
2127
]);
@@ -24,12 +30,24 @@ public function test_RedisClient()
2430

2531
public function test_PredisClient()
2632
{
33+
if (false === \extension_loaded('redis')) {
34+
$this->markTestSkipped('redis extension is not loaded');
35+
}
2736
$pool = CachePool::use('predis', [
2837
'host' => getenv('REDIS_SERVER_HOST'),
2938
]);
3039
$this->assertAttributeInstanceOf(PredisClient::class, 'client', $pool);
3140
}
3241

42+
public function test_ShmopClient()
43+
{
44+
if (false === \extension_loaded('shmop')) {
45+
$this->markTestSkipped('shmop extension is not loaded');
46+
}
47+
$pool = CachePool::use('shmop');
48+
$this->assertAttributeInstanceOf(ShmopClient::class, 'client', $pool);
49+
}
50+
3351
public function test_MemoryClient()
3452
{
3553
$pool1 = CachePool::use('memory');

Tests/Integration/CachePoolIntegrationTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<?php
22

3-
namespace Koded\Caching\Tests\Integration;
3+
namespace Tests\Koded\Caching\Integration;
44

55
use Cache\IntegrationTests\CachePoolTest;
66

77
abstract class CachePoolIntegrationTest extends CachePoolTest
88
{
9+
protected $skippedTests = [
10+
'testGetItemInvalidKeys' => 'Does not make sense for typed arguments',
11+
'testGetItemsInvalidKeys' => 'Does not make sense for typed arguments',
12+
'testHasItemInvalidKeys' => 'Does not make sense for typed arguments',
13+
];
914

1015
public static function invalidKeys()
1116
{

0 commit comments

Comments
 (0)