Skip to content

Commit

Permalink
kw_cache - updated libraries and tests, added files source
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-kalanis committed May 16, 2023
1 parent d29bab0 commit da7d9b6
Show file tree
Hide file tree
Showing 22 changed files with 748 additions and 73 deletions.
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* text=auto

# Ignore stuff for export
/.github export-ignore
/doc export-ignore
/examples export-ignore
/php-tests export-ignore
4 changes: 2 additions & 2 deletions .github/workflows/code_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ '7.4', '8.0', '8.1' ]
php: [ '7.4', '8.1' ]
name: PHP ${{ matrix.php }} code style
steps:
- uses: actions/checkout@v2
Expand All @@ -43,7 +43,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ '7.4', '8.0', '8.1' ]
php: [ '7.4', '8.1' ]
name: PHP ${{ matrix.php }} phpstan
steps:
- uses: actions/checkout@v2
Expand Down
28 changes: 0 additions & 28 deletions .php-cs-fixer.dev.php

This file was deleted.

7 changes: 3 additions & 4 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
filter:
excluded_paths:
- 'examples/*'
- 'vendor/*'
- 'php-tests/_autoload.php'
- 'php-tests/_bootstrap.php'
- 'php-tests/data/*'
- '.github/*'
- 'examples/*'
- 'php-tests/*'
paths: { }

build:
Expand Down
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
language: php

php:
- 7.3
- 7.4
- 8.0
# - 8.1
Expand All @@ -12,7 +11,7 @@ before_script:
- composer install

script:
- XDEBUG_MODE=coverage phpunit --configuration phpunit.xml --coverage-clover php-tests/data/clover.xml
- XDEBUG_MODE=coverage vendor/bin/phpunit --configuration phpunit.xml --coverage-clover php-tests/data/clover.xml
- PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix --format=txt --verbose --diff --allow-risky=yes --config=.php-cs-fixer.dist.php
- vendor/bin/phpstan analyse -c phpstan.neon

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# kw_cache

[![Build Status](https://travis-ci.org/alex-kalanis/kw_cache.svg?branch=master)](https://travis-ci.org/alex-kalanis/kw_cache)
[![Build Status](https://app.travis-ci.com/alex-kalanis/kw_cache.svg?branch=master)](https://app.travis-ci.com/github/alex-kalanis/kw_cache)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/alex-kalanis/kw_cache/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/alex-kalanis/kw_cache/?branch=master)
[![Latest Stable Version](https://poser.pugx.org/alex-kalanis/kw_cache/v/stable.svg?v=1)](https://packagist.org/packages/alex-kalanis/kw_cache)
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.3-8892BF.svg)](https://php.net/)
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
"license": "BSD-3-Clause",
"type": "library",
"require": {
"alex-kalanis/kw_paths": ">=1.0",
"alex-kalanis/kw_paths": ">=3.0",
"alex-kalanis/kw_semaphore": ">=1.0",
"ext-json": "*",
"php": ">=7.3.0"
},
"require-dev": {
"alex-kalanis/kw_storage": ">=3.0",
"alex-kalanis/kw_storage": ">=3.3",
"alex-kalanis/kw_files": ">=3.3",
"friendsofphp/php-cs-fixer": "^3.0",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
Expand Down
2 changes: 1 addition & 1 deletion php-src/Cache/Dual.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function set(string $content): bool
if (false === $result) {
return false;
}
# remove signal to save
// remove signal to save
if ($this->reloadCache->exists()) {
$this->reloadCache->clear();
}
Expand Down
92 changes: 92 additions & 0 deletions php-src/Files/Basic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace kalanis\kw_cache\Files;


use kalanis\kw_cache\CacheException;
use kalanis\kw_cache\Interfaces\ICache;
use kalanis\kw_files\Access\CompositeAdapter;
use kalanis\kw_files\FilesException;
use kalanis\kw_files\Traits\TToString;
use kalanis\kw_paths\ArrayPath;
use kalanis\kw_paths\PathsException;
use kalanis\kw_paths\Stuff;


/**
* Class Basic
* @package kalanis\kw_cache\Files
* Caching content by files
*/
class Basic implements ICache
{
use TToString;

/** @var CompositeAdapter */
protected $cacheStorage = null;
/** @var string[] */
protected $cachePath = [];

public function __construct(CompositeAdapter $cacheStorage)
{
$this->cacheStorage = $cacheStorage;
}

public function init(string $what): void
{
try {
$arr = new ArrayPath();
$arr->setString($what);
$this->cachePath = array_merge(
$arr->getArrayDirectory(),
[$arr->getFileName() . ICache::EXT_CACHE]
);
// @codeCoverageIgnoreStart
} catch (PathsException $ex) {
// just when it has problems with separating the path
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
}
// @codeCoverageIgnoreEnd
}

public function exists(): bool
{
try {
return $this->cacheStorage->exists($this->cachePath);
} catch (FilesException | PathsException $ex) {
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
}
}

public function set(string $content): bool
{
try {
return $this->cacheStorage->saveFile($this->cachePath, $content);
} catch (FilesException | PathsException $ex) {
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
}
}

public function get(): string
{
try {
return $this->exists()
? $this->toString(
Stuff::arrayToPath($this->cachePath),
$this->cacheStorage->readFile($this->cachePath)
)
: '';
} catch (FilesException | PathsException $ex) {
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
}
}

public function clear(): void
{
try {
$this->cacheStorage->deleteFile($this->cachePath);
} catch (FilesException | PathsException $ex) {
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
}
}
}
107 changes: 107 additions & 0 deletions php-src/Files/Dual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace kalanis\kw_cache\Files;


use kalanis\kw_cache\CacheException;
use kalanis\kw_cache\Interfaces\ICache;
use kalanis\kw_files\Access\CompositeAdapter;
use kalanis\kw_files\FilesException;
use kalanis\kw_files\Traits\TToString;
use kalanis\kw_paths\ArrayPath;
use kalanis\kw_paths\PathsException;
use kalanis\kw_paths\Stuff;


/**
* Class Dual
* @package kalanis\kw_cache\Files
* Caching content by files - file as semaphore
*/
class Dual implements ICache
{
use TToString;

/** @var CompositeAdapter */
protected $cacheLib = null;
/** @var CompositeAdapter */
protected $reloadLib = null;
/** @var string[] */
protected $cachePath = [];
/** @var string[] */
protected $reloadPath = [];

public function __construct(CompositeAdapter $cacheLib, ?CompositeAdapter $reloadLib = null)
{
$this->cacheLib = $cacheLib;
$this->reloadLib = $reloadLib ?? $cacheLib;
}

public function init(string $what): void
{
try {
$arr = new ArrayPath();
$arr->setString($what);
$this->cachePath = array_merge(
$arr->getArrayDirectory(),
[$arr->getFileName() . ICache::EXT_CACHE]
);;
$this->reloadPath = array_merge(
$arr->getArrayDirectory(),
[$arr->getFileName() . ICache::EXT_RELOAD]
);
// @codeCoverageIgnoreStart
} catch (PathsException $ex) {
// just when it has problems with separating the path
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
}
// @codeCoverageIgnoreEnd
}

public function exists(): bool
{
try {
return $this->cacheLib->exists($this->cachePath) && !$this->reloadLib->exists($this->reloadPath);
} catch (FilesException | PathsException $ex) {
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
}
}

public function set(string $content): bool
{
try {
$result = $this->cacheLib->saveFile($this->cachePath, $content);
if (false === $result) {
return false;
}
// remove signal to save
if ($this->reloadLib->exists($this->reloadPath)) {
$this->reloadLib->deleteFile($this->reloadPath);
}
return true;
} catch (FilesException | PathsException $ex) {
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
}
}

public function get(): string
{
try {
return $this->exists()
? $this->toString(Stuff::arrayToPath($this->cachePath), $this->cacheLib->readFile($this->cachePath))
: ''
;
} catch (FilesException | PathsException $ex) {
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
}
}

public function clear(): void
{
try {
$this->cacheLib->deleteFile($this->cachePath);
} catch (FilesException | PathsException $ex) {
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
}
}
}

0 comments on commit da7d9b6

Please sign in to comment.