Skip to content

Commit

Permalink
feat: setup project
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomomasseron committed Nov 3, 2024
1 parent 55ec689 commit 95af8ec
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 86 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
[![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)
[![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)

Clean cache keys when you insert, update or delete some records of your database.

## Installation

You can install the package via composer:
Expand All @@ -16,15 +18,15 @@ composer require giacomomasseron/laravel-cache-when-db
You can publish the config file with:

```bash
php artisan vendor:publish --tag="laravel-cache-when-db-config"
php artisan vendor:publish --tag="laravel-cache-when-db"
```

This is the contents of the published config file:

```php
return [
/**
* Define Cache keys to clean when something happens in the database
* Define Cache keys to clean when one the tables of the array is updated
*
* [
* 'cache_key' => [
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"laravel-cache-when-db",
"eloquent"
],
"homepage": "https://github.com/giacomomasseron/laravel-models-generator",
"homepage": "https://github.com/giacomomasseron/laravel-cache-when-db",
"license": "MIT",
"authors": [
{
Expand All @@ -34,7 +34,7 @@
"extra": {
"laravel": {
"providers": [
"LaravelCacheWhenDbServiceProvider"
"GiacomoMasseroni\\LaravelCacheWhenDb\\LaravelCacheWhenDbServiceProvider"
]
}
},
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ parameters:
- src/

# Level 9 is the highest level
level: 5
level: 8

# ignoreErrors:
# - '#PHPDoc tag @var#'
Expand Down
8 changes: 7 additions & 1 deletion src/LaravelCacheWhenDb.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace GiacomoMasseroni\LaravelCacheWhenDb;

use Illuminate\Support\Facades\Cache;
Expand All @@ -13,8 +15,12 @@ public static function queryChangedDatabase(string $sql): bool
return str_starts_with($sql, 'INSERT') || str_starts_with($sql, 'UPDATE') || str_starts_with($sql, 'DELETE');
}

public static function clearCache(string $sql): void
public static function cleanCache(string $sql): void
{
/**
* @var string $key
* @var array<string> $dbTables
*/
foreach (config('laravel-cache-when-db', []) as $key => $dbTables) {
$clean = false;

Expand Down
28 changes: 13 additions & 15 deletions src/LaravelCacheWhenDbServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,29 @@

declare(strict_types=1);

namespace Crurated\ApiUseCaseManagement;
namespace GiacomoMasseroni\LaravelCacheWhenDb;

use GiacomoMasseroni\LaravelCacheWhenDb\LaravelCacheWhenDb;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;

class LaravelCacheWhenDbServiceProvider extends ServiceProvider
{
public function register()
public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-cache-when-db');
$this->mergeConfigFrom(__DIR__.'/../config/laravel-cache-when-db.php', 'laravel-cache-when-db');
}

public function boot()
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/config.php' => config_path('laravel-cache-when-db'),
]);
$this->publishes([
__DIR__.'/../config/laravel-cache-when-db.php' => config_path('laravel-cache-when-db.php'),
]);

DB::listen(function ($query): void {
if (LaravelCacheWhenDb::queryChangedDatabase($query->sql)) {
LaravelCacheWhenDb::clearCache($query->sql);
}
});
}
DB::listen(function (QueryExecuted $query): void {
if (LaravelCacheWhenDb::queryChangedDatabase($query->sql)) {
LaravelCacheWhenDb::cleanCache($query->sql);
}
});
}
}
34 changes: 0 additions & 34 deletions tests/Unit/CacheTest.php

This file was deleted.

31 changes: 0 additions & 31 deletions tests/Unit/EventsQueueTest.php

This file was deleted.

0 comments on commit 95af8ec

Please sign in to comment.