feat: implement repository architecture with smart caching
- Add base repository interfaces and abstract classes - Implement separated read/write repositories for Domain and Username models - Add intelligent query caching with automatic invalidation - Include cache management service and CLI commands - Add comprehensive configuration for cache TTL and monitoring - Enhance performance through optimized data access patterns
This commit is contained in:
52
app/Repositories/Contracts/ReadRepositoryInterface.php
Normal file
52
app/Repositories/Contracts/ReadRepositoryInterface.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Contracts;
|
||||
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
interface ReadRepositoryInterface
|
||||
{
|
||||
public function findById(int $id, array $columns = ['*']): ?Model;
|
||||
|
||||
public function findBy(string $column, mixed $value, array $columns = ['*']): ?Model;
|
||||
|
||||
public function all(array $columns = ['*']): Collection;
|
||||
|
||||
public function get(array $columns = ['*']): Collection;
|
||||
|
||||
public function paginate(int $perPage = 15, array $columns = ['*'], string $pageName = 'page'): LengthAwarePaginator;
|
||||
|
||||
public function whereIn(string $column, array $values, array $columns = ['*']): Collection;
|
||||
|
||||
public function where(string $column, mixed $operator, mixed $value = null): self;
|
||||
|
||||
public function whereNull(string $column): self;
|
||||
|
||||
public function whereNotNull(string $column): self;
|
||||
|
||||
public function whereBetween(string $column, array $values): self;
|
||||
|
||||
public function orderBy(string $column, string $direction = 'asc'): self;
|
||||
|
||||
public function limit(int $limit): self;
|
||||
|
||||
public function with(array $relations): self;
|
||||
|
||||
public function withCount(array $relations): self;
|
||||
|
||||
public function exists(): bool;
|
||||
|
||||
public function count(): int;
|
||||
|
||||
public function first(array $columns = ['*']): ?Model;
|
||||
|
||||
public function firstWhere(string $column, mixed $operator, mixed $value = null): ?Model;
|
||||
|
||||
public function pluck(string $column, ?string $key = null): Collection;
|
||||
|
||||
public function clearCache(): void;
|
||||
|
||||
public function clearCacheForModel(Model $model): void;
|
||||
}
|
||||
16
app/Repositories/Contracts/RepositoryInterface.php
Normal file
16
app/Repositories/Contracts/RepositoryInterface.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Contracts;
|
||||
|
||||
interface RepositoryInterface extends ReadRepositoryInterface, WriteRepositoryInterface
|
||||
{
|
||||
public function getModel(): string;
|
||||
|
||||
public function startTransaction(): void;
|
||||
|
||||
public function commitTransaction(): void;
|
||||
|
||||
public function rollbackTransaction(): void;
|
||||
|
||||
public function transaction(callable $callback): mixed;
|
||||
}
|
||||
45
app/Repositories/Contracts/WriteRepositoryInterface.php
Normal file
45
app/Repositories/Contracts/WriteRepositoryInterface.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Contracts;
|
||||
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
interface WriteRepositoryInterface
|
||||
{
|
||||
public function create(array $data): Model;
|
||||
|
||||
public function createMany(array $data): Collection;
|
||||
|
||||
public function update(Model $model, array $data): bool;
|
||||
|
||||
public function updateById(int $id, array $data): bool;
|
||||
|
||||
public function upsert(array $data, array|string $uniqueBy, ?array $update = null): Collection;
|
||||
|
||||
public function delete(Model $model): bool;
|
||||
|
||||
public function deleteById(int $id): bool;
|
||||
|
||||
public function deleteMultiple(array $ids): int;
|
||||
|
||||
public function restore(Model $model): bool;
|
||||
|
||||
public function restoreById(int $id): bool;
|
||||
|
||||
public function forceDelete(Model $model): bool;
|
||||
|
||||
public function forceDeleteById(int $id): bool;
|
||||
|
||||
public function sync(Model $model, string $relation, array $ids, bool $detaching = true): array;
|
||||
|
||||
public function attach(Model $model, string $relation, array $ids, array $attributes = [], bool $touch = true): void;
|
||||
|
||||
public function detach(Model $model, string $relation, ?array $ids = null, bool $touch = true): int;
|
||||
|
||||
public function clearCache(): void;
|
||||
|
||||
public function clearCacheForModel(Model $model): void;
|
||||
|
||||
public function clearCacheForCollection(Collection $models): void;
|
||||
}
|
||||
Reference in New Issue
Block a user