papimod / cache
Module Papi
Installs: 6
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/papimod/cache
Requires
- php: >=8.3.0
- papi/papi: ^2.1.0
- papimod/dotenv: ^2.1.0
Requires (Dev)
- phpunit/phpunit: ^12.4
- squizlabs/php_codesniffer: ^4.0
README
Description
Enable routes and DI caching in your papi in production.
By default you should add .cache/ into your .gitignore file
I also recommend reading the section "deployment in production".
Prerequisites Modules
Configuration
ENVIRONMENT (.ENV)
| Required | No |
| Type | PRODUCTION, DEVELOPMENT, TEST or null |
| Description | Enable caching when set to PRODUCTION |
| Default | null |
CACHE_DIRECTORY (.ENV)
| Required | No |
| Type | string |
| Description | Cache directory path |
| Default | {{project_directory}}/.cache |
CACHE_TIMEOUT (.ENV)
| Required | No |
| Type | Int |
| Description | Time in seconds between cache refreshes |
| Default | 86400 |
API
Usage
You can add the following options to your .env file:
ENVIRONMENT=PRODUCTION
CACHE_DIRECTORY=tmp
CACHE_TIMEOUT=900
Import the module when creating your application:
require __DIR__ . "/../vendor/autoload.php"; use Papi\PapiBuilder; use Papimod\Dotenv\DotEnvModule; use Papimod\Cache\CacheModule; use function DI\create; define("PAPI_DOTENV_DIRECTORY", __DIR__); # Optionnal define("PAPI_DOTENV_FILE", ".env"); # Optionnal $builder = new PapiBuilder(); $builder->setModule( DotEnvModule::class, CacheModule::class ) ->build() ->run();
Use the dedicated service anywhere:
final class MyService { private readonly CacheService $cache_service; public function __construct(CacheService $cache_service) { $this->cache_service = $cache_service; } public increment(): string { $foo = $this->cache_service->get('foo'); if($foo !== null) { $foo++ } else { $foo = 1; } $this->cache_service->set('foo', $foo, 120); } }