Skip to content

Commit 982ee6b

Browse files
feat: setup project
0 parents  commit 982ee6b

19 files changed

+390
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
build
2+
composer.lock
3+
vendor
4+
.php_cs.cache
5+
coverage
6+
.phpunit.result.cache
7+
/.idea
8+
/.phpunit.cache/
9+
/.phpunit.cache/test-results
10+
.php-cs-fixer.cache
11+
auth.json

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Clean cache when something happens in the database
2+
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/giacomomasseron/laravel-cache-when-db.svg?style=flat-square)](https://packagist.org/packages/giacomomasseron/laravel-cache-when-db)
4+
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/giacomomasseron/laravel-cache-when-db/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/giacomomasseron/laravel-cache-when-db/actions?query=workflow%3Arun-tests+branch%3Amain)
5+
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/giacomomasseron/laravel-cache-when-db/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/giacomomasseron/laravel-cache-when-db/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
6+
[![Total Downloads](https://img.shields.io/packagist/dt/giacomomasseron/laravel-cache-when-db.svg?style=flat-square)](https://packagist.org/packages/giacomomasseron/laravel-cache-when-db)
7+
8+
## Installation
9+
10+
You can install the package via composer:
11+
12+
```bash
13+
composer require giacomomasseron/laravel-cache-when-db
14+
```
15+
16+
You can publish the config file with:
17+
18+
```bash
19+
php artisan vendor:publish --tag="laravel-cache-when-db-config"
20+
```
21+
22+
This is the contents of the published config file:
23+
24+
```php
25+
return [
26+
/**
27+
* Define Cache keys to clean when something happens in the database
28+
*
29+
* [
30+
* 'cache_key' => [
31+
* 'database_table',
32+
* 'database_table',
33+
* ],
34+
* ]
35+
*/
36+
];
37+
```
38+
39+
## Testing
40+
41+
```bash
42+
composer test
43+
```
44+
45+
## Changelog
46+
47+
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
48+
49+
## Contributing
50+
51+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
52+
53+
## Security Vulnerabilities
54+
55+
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
56+
57+
## Credits
58+
59+
- [Giacomo Masseroni](https://github.com/giacomomasseron)
60+
- [All Contributors](../../contributors)
61+
62+
## License
63+
64+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"name": "giacomomasseron/laravel-cache-when-db",
3+
"description": "Register cache you want to clean when something happens in the database",
4+
"keywords": [
5+
"Giacomo Masseroni",
6+
"laravel",
7+
"laravel-cache-when-db",
8+
"eloquent"
9+
],
10+
"homepage": "https://github.com/giacomomasseron/laravel-models-generator",
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Giacomo Masseroni",
15+
"email": "[email protected]",
16+
"role": "Developer"
17+
}
18+
],
19+
"require": {
20+
"php": "^8.2",
21+
"illuminate/contracts": "^10.0||^11.0"
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "^10.1",
25+
"larastan/larastan": "^2.9",
26+
"laravel/pint": "^1.14",
27+
"orchestra/testbench": "^9.0.0||^8.22.0"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"GiacomoMasseroni\\LaravelCacheWhenDb\\": "src/"
32+
}
33+
},
34+
"extra": {
35+
"laravel": {
36+
"providers": [
37+
"LaravelCacheWhenDbServiceProvider"
38+
]
39+
}
40+
},
41+
"minimum-stability": "dev",
42+
"prefer-stable": true,
43+
"autoload-dev": {
44+
"psr-4": {
45+
"Workbench\\App\\": "workbench/app/",
46+
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
47+
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
48+
}
49+
},
50+
"scripts": {
51+
"post-autoload-dump": "@composer run prepare",
52+
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
53+
"prepare": "@php vendor/bin/testbench package:discover --ansi",
54+
"build": "@php vendor/bin/testbench workbench:build --ansi",
55+
"test:lint": "pint --test",
56+
"test:refactor": "rector --dry-run",
57+
"test:types": "phpstan analyse",
58+
"test:unit": "@php vendor/bin/phpunit",
59+
"serve": [
60+
"@build",
61+
"@php vendor/bin/testbench serve"
62+
],
63+
"test": [
64+
"@test:lint",
65+
"@test:refactor",
66+
"@test:types",
67+
"@test:unit"
68+
]
69+
}
70+
}

config/config.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
return [
4+
/**
5+
* Define Cache keys to clean when something happens in the database
6+
*
7+
* [
8+
* 'cache_key' => [
9+
* 'database_table',
10+
* 'database_table',
11+
* ],
12+
* ]
13+
*/
14+
];

phpstan.neon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
includes:
2+
- vendor/larastan/larastan/extension.neon
3+
4+
parameters:
5+
6+
paths:
7+
- src/
8+
9+
# Level 9 is the highest level
10+
level: 5
11+
12+
# ignoreErrors:
13+
# - '#PHPDoc tag @var#'
14+
#
15+
# excludePaths:
16+
# - ./*/*/FileToBeExcluded.php
17+
#
18+
# checkMissingIterableValueType: false

phpunit.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<coverage/>
4+
<testsuites>
5+
<testsuite name="Unit">
6+
<directory suffix="Test.php">./tests/Unit</directory>
7+
</testsuite>
8+
<!--<testsuite name="Feature">
9+
<directory suffix="Test.php">./tests/Feature</directory>
10+
</testsuite>-->
11+
</testsuites>
12+
<php>
13+
<env name="DB_CONNECTION" value="testing"/>
14+
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/>
15+
<env name="APP_ENV" value="testing"/>
16+
</php>
17+
<source>
18+
<include>
19+
<directory suffix=".php">src/</directory>
20+
</include>
21+
</source>
22+
</phpunit>

rector.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
return RectorConfig::configure()
8+
->withPaths([
9+
__DIR__.'/config',
10+
__DIR__.'/src',
11+
__DIR__.'/tests',
12+
__DIR__.'/workbench',
13+
])
14+
// uncomment to reach your current PHP version
15+
// ->withPhpSets()
16+
->withTypeCoverageLevel(0);

src/LaravelCacheWhenDb.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace GiacomoMasseroni\LaravelCacheWhenDb;
4+
5+
use Illuminate\Support\Facades\Cache;
6+
7+
class LaravelCacheWhenDb
8+
{
9+
public static function queryChangedDatabase(string $sql): bool
10+
{
11+
$sql = strtoupper($sql);
12+
13+
return str_starts_with($sql, 'INSERT') || str_starts_with($sql, 'UPDATE') || str_starts_with($sql, 'DELETE');
14+
}
15+
16+
public static function clearCache(string $sql): void
17+
{
18+
foreach (config('laravel-cache-when-db', []) as $key => $dbTables) {
19+
$clean = false;
20+
21+
foreach ($dbTables as $dbTable) {
22+
if (self::sqlContainsTable($sql, $dbTable)) {
23+
$clean = true;
24+
break;
25+
}
26+
}
27+
28+
if ($clean) {
29+
Cache::forget($key);
30+
}
31+
}
32+
}
33+
34+
private static function sqlContainsTable(string $sql, string $dbTable): bool
35+
{
36+
return str_contains($sql, $dbTable);
37+
}
38+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Crurated\ApiUseCaseManagement;
6+
7+
use GiacomoMasseroni\LaravelCacheWhenDb\LaravelCacheWhenDb;
8+
use Illuminate\Support\Facades\DB;
9+
use Illuminate\Support\ServiceProvider;
10+
11+
class LaravelCacheWhenDbServiceProvider extends ServiceProvider
12+
{
13+
public function register()
14+
{
15+
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-cache-when-db');
16+
}
17+
18+
public function boot()
19+
{
20+
if ($this->app->runningInConsole()) {
21+
$this->publishes([
22+
__DIR__.'/../config/config.php' => config_path('laravel-cache-when-db'),
23+
]);
24+
25+
DB::listen(function ($query): void {
26+
if (LaravelCacheWhenDb::queryChangedDatabase($query->sql)) {
27+
LaravelCacheWhenDb::clearCache($query->sql);
28+
}
29+
});
30+
}
31+
}
32+
}

testbench.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
providers: []
2+
3+
migrations:
4+
- workbench/database/migrations
5+
6+
seeders:
7+
- Workbench\Database\Seeders\DatabaseSeeder
8+
9+
workbench:
10+
start: '/'
11+
install: true
12+
welcome: true
13+
discovers:
14+
web: false
15+
api: false
16+
commands: false
17+
build: []
18+
assets: []
19+
sync: []

0 commit comments

Comments
 (0)