Skip to content

Commit

Permalink
Libs update (#13)
Browse files Browse the repository at this point in the history
* Update composer libs

* Fix CachedItem

* Fix CompositeCache

* Fix InvalidArgumentException

* Fix InMemoryCache

* Re-format code in CompositeCacheTest

* Re-format code in InMemoryCacheTest

* Add Timer object

* Add TimerFactory object

* Use timer object in InMemoryCache

* Add comments to InMemoryCacheTest

* Use Timer mock in InMemoryCacheTest to avoid sleep()

* Fix psalm settings

* Remove infection exception related to InMemoryCache::__construct

* Remove infection exception related to InMemoryCache

* Add dependabot

* Add opcache to docker container

* Update phpunit to v10

* Update infection to v0.27

* Add php 8.2 to actions
  • Loading branch information
marvin255 committed Aug 13, 2023
1 parent 51b911d commit 9a38aa3
Show file tree
Hide file tree
Showing 19 changed files with 541 additions and 209 deletions.
18 changes: 18 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "composer" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
allow:
- dependency-name: "psr/simple-cache"
- dependency-name: "phpunit/phpunit"
- dependency-name: "friendsofphp/php-cs-fixer"
- dependency-name: "vimeo/psalm"
- dependency-name: "infection/infection"
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php-versions: ['8.1']
php-versions: ['8.1', '8.2']
steps:
- uses: actions/checkout@v2
- name: Install PHP
Expand Down
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
"psr/simple-cache": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"phpunit/phpunit": "^10.0",
"friendsofphp/php-cs-fixer": "^3.0",
"sebastian/phpcpd": "^6.0",
"vimeo/psalm": "^5.0",
"infection/infection": "^0.26.13"
"infection/infection": "^0.27.0"
},
"autoload": {
"psr-4": {
Expand All @@ -37,7 +36,6 @@
],
"linter": [
"vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php -v --dry-run --stop-on-violation --allow-risky=yes",
"vendor/bin/phpcpd ./ --exclude vendor --exclude docker",
"vendor/bin/psalm --show-info=true --php-version=$(php -r \"echo phpversion();\")"
],
"infection": [
Expand Down
7 changes: 4 additions & 3 deletions docker/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ RUN set -xe && apk update && apk add --no-cache \
git \
autoconf \
g++ \
make
make \
linux-headers


RUN docker-php-ext-install zip soap \
RUN docker-php-ext-install zip opcache \
&& docker-php-source extract \
&& pecl install xdebug \
&& docker-php-ext-enable xdebug \
Expand All @@ -24,7 +25,7 @@ RUN docker-php-ext-install zip soap \
&& echo 'xdebug.mode=coverage' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini


RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --version=2.4.2 \
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --version=2.5.8 \
&& mkdir -p /.composer && chmod -Rf 777 /.composer


Expand Down
17 changes: 1 addition & 16 deletions infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,6 @@
"text": "infection.log"
},
"mutators": {
"@default": true,
"DecrementInteger": {
"ignore": [
"Marvin255\\InMemoryCache\\InMemoryCache::__construct"
]
},
"IncrementInteger": {
"ignore": [
"Marvin255\\InMemoryCache\\InMemoryCache::__construct"
]
},
"GreaterThanOrEqualTo": {
"ignore": [
"Marvin255\\InMemoryCache\\InMemoryCache::isItemValid"
]
}
"@default": true
}
}
29 changes: 12 additions & 17 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
colors="true"
bootstrap="./vendor/autoload.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>

<testsuites>
<testsuite name="InMemoryCache tests suit">
<directory>./tests</directory>
</testsuite>
</testsuites>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="./vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd">
<coverage/>
<testsuites>
<testsuite name="InMemoryCache tests suit">
<directory>./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0"?>
<psalm
cacheDirectory="/tmp"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config file:///var/www/vendor/vimeo/psalm/config.xsd"
Expand Down
12 changes: 4 additions & 8 deletions src/CachedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@
*/
final class CachedItem
{
private readonly mixed $payload;

private readonly int $validTill;

private int $selectCount = 0;

public function __construct(mixed $payload, int $validTill)
{
$this->payload = $payload;
$this->validTill = $validTill;
public function __construct(
private readonly mixed $payload,
private readonly int $validTill
) {
}

public function getValidTill(): int
Expand Down
18 changes: 8 additions & 10 deletions src/CompositeCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@
* The second is something that require socket connection e.g. redis based cache.
* Composite cache tries to find data in the light cache and if there is no data
* in light cache makes a request for heavy cache.
*
* @psalm-api
*/
final class CompositeCache implements CacheInterface
{
private readonly CacheInterface $lightCache;

private readonly CacheInterface $heavyCache;

public function __construct(CacheInterface $lightCache, CacheInterface $heavyCache)
{
$this->lightCache = $lightCache;
$this->heavyCache = $heavyCache;
public function __construct(
private readonly CacheInterface $lightCache,
private readonly CacheInterface $heavyCache
) {
}

/**
Expand All @@ -44,7 +42,7 @@ public function get(string $key, mixed $default = null): mixed
/**
* {@inheritDoc}
*/
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
public function set(string $key, mixed $value, int|\DateInterval $ttl = null): bool
{
return $this->heavyCache->set($key, $value, $ttl)
&& $this->lightCache->set($key, $value, $ttl);
Expand Down Expand Up @@ -84,7 +82,7 @@ public function getMultiple(iterable $keys, mixed $default = null): iterable
/**
* {@inheritDoc}
*/
public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool
public function setMultiple(iterable $values, int|\DateInterval $ttl = null): bool
{
return $this->heavyCache->setMultiple($values, $ttl)
&& $this->lightCache->setMultiple($values, $ttl);
Expand Down
38 changes: 17 additions & 21 deletions src/InMemoryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,33 @@

/**
* Simple PSR-16 implementation that uses internal array.
*
* @psalm-api
*/
final class InMemoryCache implements CacheInterface
{
private readonly int $stackSize;
public const DEFAULT_STACK_SIZE = 1000;
public const DEFAULT_TTL = 60;

private readonly int $defaultTTL;
private Timer $timer;

/**
* @var array<string, CachedItem>
*/
private array $stack = [];

public function __construct(int $stackSize = 1000, int $defaultTTL = 60)
{
if ($stackSize < 1) {
public function __construct(
private readonly int $stackSize = self::DEFAULT_STACK_SIZE,
private readonly int $defaultTTL = self::DEFAULT_TTL,
Timer $timer = null
) {
if ($this->stackSize < 1) {
throw new InvalidArgumentException('Stack size must be greater than 0');
}
if ($defaultTTL < 1) {
if ($this->defaultTTL < 1) {
throw new InvalidArgumentException('Default TTL must be greater than 0');
}

$this->stackSize = $stackSize;
$this->defaultTTL = $defaultTTL;
$this->timer = $timer ?: TimerFactory::create();
}

/**
Expand All @@ -48,7 +52,7 @@ public function get(string $key, mixed $default = null): mixed
/**
* {@inheritDoc}
*/
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
public function set(string $key, mixed $value, int|\DateInterval $ttl = null): bool
{
if (\count($this->stack) >= $this->stackSize) {
$this->clearStack();
Expand Down Expand Up @@ -96,7 +100,7 @@ public function getMultiple(iterable $keys, mixed $default = null): iterable
/**
* {@inheritDoc}
*/
public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool
public function setMultiple(iterable $values, int|\DateInterval $ttl = null): bool
{
foreach ($values as $key => $value) {
$this->set((string) $key, $value, $ttl);
Expand Down Expand Up @@ -130,7 +134,7 @@ public function has(string $key): bool
*/
private function createValidTill(null|int|\DateInterval $ttl): int
{
$validTill = $this->getCurrentTimestamp();
$validTill = $this->timer->getCurrentTimestamp();

if ($ttl === null) {
$validTill += $this->defaultTTL;
Expand Down Expand Up @@ -176,7 +180,7 @@ private function clearStack(): void
*/
private function isItemValid(CachedItem $item): bool
{
return $item->getValidTill() >= $this->getCurrentTimestamp();
return $item->getValidTill() >= $this->timer->getCurrentTimestamp();
}

/**
Expand All @@ -186,12 +190,4 @@ private function calculateItemSortScore(CachedItem $item): int
{
return $item->getSelectCount();
}

/**
* Returns current timestamp.
*/
private function getCurrentTimestamp(): int
{
return time();
}
}
5 changes: 5 additions & 0 deletions src/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace Marvin255\InMemoryCache;

/**
* Specific cache related exception.
*
* @internal
*/
final class InvalidArgumentException extends \Exception implements \Psr\SimpleCache\InvalidArgumentException
{
}
16 changes: 16 additions & 0 deletions src/Timer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Marvin255\InMemoryCache;

/**
* Object that provides timestamp.
*/
interface Timer
{
/**
* Return current timestamp.
*/
public function getCurrentTimestamp(): int;
}
31 changes: 31 additions & 0 deletions src/TimerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Marvin255\InMemoryCache;

/**
* Factory that provides timer object.
*
* @psalm-api
*/
final class TimerFactory
{
private static ?Timer $timer = null;

private function __construct()
{
}

/**
* Return timer object.
*/
public static function create(): Timer
{
if (self::$timer === null) {
self::$timer = new TimerInternal();
}

return self::$timer;
}
}
21 changes: 21 additions & 0 deletions src/TimerInternal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Marvin255\InMemoryCache;

/**
* Object that provides timestamp using php time() function.
*
* @internal
*/
final class TimerInternal implements Timer
{
/**
* {@inheritdoc}
*/
public function getCurrentTimestamp(): int
{
return time();
}
}
2 changes: 2 additions & 0 deletions tests/BaseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/**
* Base test case for all tests.
*
* @internal
*/
abstract class BaseCase extends TestCase
{
Expand Down
Loading

0 comments on commit 9a38aa3

Please sign in to comment.