diff --git a/.env.example b/.env.example index d082170..e7464c7 100644 --- a/.env.example +++ b/.env.example @@ -68,4 +68,5 @@ ELASTICSEARCH_PORT=9200 SCOUT_DRIVER=elastic EXPLORER_ELASTIC_LOGGER_ENABLED=true -BOOK_DUMMY_DATA_TOTAL_RECORDS=1 \ No newline at end of file +BOOK_DUMMY_DATA_TOTAL_RECORDS=1000 +ENABLE_REDIS_CACHE=true diff --git a/app/Http/Middleware/CacheMiddleware.php b/app/Http/Middleware/CacheMiddleware.php index bffc3da..4f03abf 100644 --- a/app/Http/Middleware/CacheMiddleware.php +++ b/app/Http/Middleware/CacheMiddleware.php @@ -5,6 +5,7 @@ use Closure; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Log; use Symfony\Component\HttpFoundation\Response; class CacheMiddleware extends AbstractCacheMiddleware @@ -19,6 +20,13 @@ class CacheMiddleware extends AbstractCacheMiddleware public function handle(Request $request, Closure $next): Response { $response = $next($request); + + if (!config('app.enable_redis_cache')) { + Log::warning('Disabled Redis cache'); + + return $response; + } + if ($response->getStatusCode() !== Response::HTTP_OK) { return $response; } diff --git a/app/Http/Middleware/CheckCacheMiddleware.php b/app/Http/Middleware/CheckCacheMiddleware.php index 007ac2c..e0ddf39 100644 --- a/app/Http/Middleware/CheckCacheMiddleware.php +++ b/app/Http/Middleware/CheckCacheMiddleware.php @@ -2,9 +2,10 @@ namespace App\Http\Middleware; -use Cache; use Closure; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Log; use Symfony\Component\HttpFoundation\Response; class CheckCacheMiddleware extends AbstractCacheMiddleware @@ -16,6 +17,12 @@ class CheckCacheMiddleware extends AbstractCacheMiddleware */ public function handle(Request $request, Closure $next): Response { + if (!config('app.enable_redis_cache')) { + Log::warning('Disabled Redis cache'); + + return $next($request); + } + $cacheKey = $this->generateCacheKey($request); if (Cache::has($cacheKey)) { return response(Cache::get($cacheKey)); diff --git a/app/Logging/CreateElasticsearchLogger.php b/app/Logging/CreateElasticsearchLogger.php new file mode 100644 index 0000000..b4bbcb0 --- /dev/null +++ b/app/Logging/CreateElasticsearchLogger.php @@ -0,0 +1,30 @@ +setHosts([$config['endpoint']]) + ->build(); + //create the handler + $options = [ + 'index' => $config['index'], + 'type' => '_doc', + ]; + $handler = new ElasticsearchHandler($client, $options, Level::Info, true); + + $logger->setHandlers([$handler]); + + return $logger; + } +} diff --git a/config/app.php b/config/app.php index f467267..e87389d 100644 --- a/config/app.php +++ b/config/app.php @@ -123,4 +123,5 @@ 'store' => env('APP_MAINTENANCE_STORE', 'database'), ], + 'enable_redis_cache' => env('ENABLE_REDIS_CACHE', true), ]; diff --git a/config/logging.php b/config/logging.php index 167f11f..df27c82 100644 --- a/config/logging.php +++ b/config/logging.php @@ -128,10 +128,10 @@ ], 'elasticsearch' => [ - 'driver' => 'daily', - 'path' => storage_path('logs/elasticsearch.log'), - 'level' => env('LOG_LEVEL', 'debug'), - 'days' => 14, + 'driver' => 'custom', + 'via' => App\Logging\CreateElasticsearchLogger::class, + 'index' => 'book_logs', + 'endpoint' => env('ELASTICSEARCH_ENDPOINT', null), ], ], diff --git a/docker-compose.yml b/docker-compose.yml index 9b40388..5301a3f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -61,7 +61,6 @@ services: - "ES_JAVA_OPTS=-Xms1g -Xmx1g" - xpack.security.enabled=false - xpack.security.enrollment.enabled=false - - "index.max_result_window=1000000" ports: - 9200:9200 volumes: