-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kw_cache - updated libraries and tests, added files source
- Loading branch information
1 parent
d29bab0
commit da7d9b6
Showing
22 changed files
with
748 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.