Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a cache service #19

Merged
merged 4 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.vscode
vendor
vendor
cache
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,18 @@ $dispatcher->dispatch(new MyEvent());
That's all!

Please note that if your package is also a Brick, it will scan only `src` and `include` directory for Services.

## Supplied Services

**ServiceManager**

With `ServiceManager::get(class-string)` you can retrieve any Service. But it's better if you use automatic Service
injection in your own Service constructor.

**EventDispatcher**

With `EventDispatcher::dispatch(mixed)` you can dispatch an Event to all its listeners.

**CacheInterface**

Implementation of [PSR-16](https://www.php-fig.org/psr/psr-16/) cache system.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"azjezz/psl": "^2.9",
"cuyz/valinor": "^1.12",
"symfony/yaml": "^7.0",
"vlucas/phpdotenv": "^5.6"
"vlucas/phpdotenv": "^5.6",
"psr/simple-cache": "^3.0"
},
"autoload-dev": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions include/Cache/CacheInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* MIT License
*
* Copyright (c) 2024-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

declare(strict_types=1);

namespace Archict\Core\Cache;

/**
* @internal
*/
final class CacheInfo
{
/**
* @param array<non-empty-string, int> $files_ttl For each file id, if it has a ttl, value is max valid timestamp
*/
public function __construct(
public array $files_ttl,
) {
}
}
48 changes: 48 additions & 0 deletions include/Cache/CacheLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* MIT License
*
* Copyright (c) 2024-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

declare(strict_types=1);

namespace Archict\Core\Cache;

use Archict\Core\Env\EnvironmentService;
use CuyZ\Valinor\MapperBuilder;
use CuyZ\Valinor\Normalizer\Format;
use Psr\SimpleCache\CacheInterface;

final class CacheLoader
{
public static function loadCache(EnvironmentService $environment): CacheInterface
{
$mapper = (new MapperBuilder())->allowPermissiveTypes()->mapper();
$normalizer = (new MapperBuilder())->normalizer(Format::json());

return match ((string) $environment->get('MODE', 'PROD')) {
'PROD' => new FileSystemCache($mapper, $normalizer),
'DEV' => new MemoryCache(),
default => new NoopCache(),
};
}
}
Loading