Skip to content

Commit 9a8aa04

Browse files
committed
chore: improved doctrine cache configuration
allow to spec out the store Signed-off-by: [email protected] <[email protected]> Change-Id: I1937d478f89662998eb5408ccd7036f87cee10d4
1 parent 5fa22d1 commit 9a8aa04

File tree

5 files changed

+70
-4
lines changed

5 files changed

+70
-4
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php namespace App\libs\Utils\Doctrine;
2+
/*
3+
* Copyright 2025 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
15+
use Illuminate\Contracts\Cache\Factory;
16+
use Illuminate\Support\ServiceProvider;
17+
use LaravelDoctrine\ORM\Configuration\Cache\CacheManager;
18+
use LaravelDoctrine\ORM\Configuration\Cache\RedisCacheProvider;
19+
20+
class CustomRedisCacheProvider extends RedisCacheProvider
21+
{
22+
23+
/**
24+
* CustomRedisCacheProvider constructor.
25+
*/
26+
public function __construct(protected Factory $cache)
27+
{
28+
parent::__construct($cache);
29+
$this->store = null;
30+
}
31+
}
32+
/**
33+
* Class DoctrineCacheServiceProvider
34+
* @package App\libs\Utils\Doctrine
35+
*/
36+
class DoctrineCacheServiceProvider extends ServiceProvider
37+
{
38+
public function register()
39+
{
40+
$this->app->resolving(CacheManager::class, function (CacheManager $cache) {
41+
// Override the 'redis' driver
42+
$cache->extend('redis', function (array $settings, $app) {
43+
$res = new CustomRedisCacheProvider($app->make(Factory::class));
44+
return $res->resolve($settings);
45+
});
46+
});
47+
}
48+
}

config/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
// remove 'Laravel\Socialite\SocialiteServiceProvider',
173173
\SocialiteProviders\Manager\ServiceProvider::class, // add
174174
\App\libs\Utils\Html\HtmlServiceProvider::class,
175+
App\libs\Utils\Doctrine\DoctrineCacheServiceProvider::class,
175176
],
176177

177178
/*

config/cache.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
'connection' => env('CACHE_REDIS_CONN', 'cache'),
6464
],
6565

66+
'doctrine_redis' => [
67+
'driver' => 'redis',
68+
'connection' => env('DOCTRINE_CACHE_REDIS_CONN', 'doctrine_cache'),
69+
],
6670
],
6771

6872
/*

config/database.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@
138138
'timeout' => env('REDIS_TIMEOUT', 30.0),
139139
'scheme' => env('REDIS_SCHEME', 'tcp'),
140140
],
141+
'doctrine_cache' => [
142+
'host' => env('REDIS_HOST'),
143+
'port' => env('REDIS_PORT'),
144+
'database' => env('REDIS_DOCTRINE_CACHE_DB', 5),
145+
'password' => env('REDIS_PASSWORD'),
146+
'timeout' => env('REDIS_TIMEOUT', 30.0),
147+
'scheme' => env('REDIS_SCHEME', 'tcp'),
148+
],
141149
],
142150
'allow_disabled_pk' => env('ALLOW_DISABLED_PK', false),
143151
];

config/doctrine.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,22 +227,27 @@
227227
'region_lifetime' => 3600,
228228
'region_lock_lifetime' => 60,
229229
'regions' => [
230-
231230
],
232231
'log_enabled' => true,
233232
'file_lock_region_directory' => '/tmp'
234233
],
235234
'metadata' => [
236235
'driver' => env('DOCTRINE_METADATA_CACHE', env('DOCTRINE_CACHE', 'redis')),
237-
'namespace' => null,
236+
'namespace' => 'meta',
237+
'store' => env('DOCTRINE_QUERY_CACHE_STORE', 'doctrine_redis'),
238+
'lifetime' => 7200, // 1 hour
238239
],
239240
'query' => [
240241
'driver' => env('DOCTRINE_QUERY_CACHE', env('DOCTRINE_CACHE', 'redis')),
241-
'namespace' => null,
242+
'namespace' => 'qry',
243+
'store' => env('DOCTRINE_QUERY_CACHE_STORE', 'doctrine_redis'),
244+
'lifetime' => 3600, // 1 hour
242245
],
243246
'result' => [
244247
'driver' => env('DOCTRINE_RESULT_CACHE', env('DOCTRINE_CACHE', 'redis')),
245-
'namespace' => null,
248+
'namespace' => 'res',
249+
'store' => env('DOCTRINE_QUERY_CACHE_STORE', 'doctrine_redis'),
250+
'lifetime' => 3600, // 1 hour
246251
],
247252
],
248253

0 commit comments

Comments
 (0)