Skip to content

Commit 2c66635

Browse files
iamacarpetJRitchieAOCopilot
authored
Cache: Instance Scoped Cache Provider (#96)
* Cache: Instance Scoped Cache Provider * Cache: register earlier * Cache: merge for instance-scoped config Co-authored-by: James Ritchie <[email protected]> * Cache: add mixin for Repository Co-authored-by: James Ritchie <[email protected]> * Cache: configure instance-scoped path via config Co-authored-by: Copilot <[email protected]> Co-authored-by: James Ritchie <[email protected]> --------- Co-authored-by: James Ritchie <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent e5c2373 commit 2c66635

File tree

6 files changed

+67
-33
lines changed

6 files changed

+67
-33
lines changed

src/AffordableMobiles/GServerlessSupportLaravel/Auth/Token/OIDC.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
use AffordableMobiles\GServerlessSupportLaravel\Auth\Exception\InvalidTokenException;
88
use AffordableMobiles\GServerlessSupportLaravel\Auth\Token\Type\JWT;
9-
use AffordableMobiles\GServerlessSupportLaravel\Cache\InstanceLocal as InstanceLocalCache;
109
use AffordableMobiles\GServerlessSupportLaravel\Integration\Guzzle\Tools as GuzzleTools;
1110
use Google\Cloud\Core\ExponentialBackoff;
1211
use GuzzleHttp\Client;
12+
use Illuminate\Support\Facades\Cache;
1313

1414
class OIDC
1515
{
@@ -103,7 +103,7 @@ public static function shouldRetry($ex, $retryAttempt = 1)
103103
*/
104104
protected static function get_jwk_url()
105105
{
106-
return InstanceLocalCache::remember('jwk_url__'.self::OPENID_CONFIGURATION_URI, 86400, static function () {
106+
return Cache::store('instance-scoped')->remember('jwk_url__'.self::OPENID_CONFIGURATION_URI, 86400, static function () {
107107
$httpclient = new Client();
108108

109109
$content = [

src/AffordableMobiles/GServerlessSupportLaravel/Auth/Token/Type/JWT.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
namespace AffordableMobiles\GServerlessSupportLaravel\Auth\Token\Type;
66

77
use AffordableMobiles\GServerlessSupportLaravel\Auth\Exception\InvalidTokenException;
8-
use AffordableMobiles\GServerlessSupportLaravel\Cache\InstanceLocal as InstanceLocalCache;
98
use AffordableMobiles\GServerlessSupportLaravel\Integration\Guzzle\Tools as GuzzleTools;
109
use Google\Cloud\Core\ExponentialBackoff;
1110
use GuzzleHttp\Client;
11+
use Illuminate\Support\Facades\Cache;
1212
use SimpleJWT\InvalidTokenException as JWTInvalidTokenException;
1313
use SimpleJWT\JWT as JWTValidator;
1414
use SimpleJWT\Keys\KeySet;
@@ -123,7 +123,7 @@ protected static function get_jwk_set($jwk_url)
123123
protected static function get_jwk_set_raw($jwk_url)
124124
{
125125
// get the public key JWK Set object (RFC7517)
126-
return InstanceLocalCache::remember('jwk_set__'.$jwk_url, 86400, static function () use ($jwk_url) {
126+
return Cache::store('instance-scoped')->remember('jwk_set__'.$jwk_url, 86400, static function () use ($jwk_url) {
127127
$httpclient = new Client();
128128

129129
$content = [

src/AffordableMobiles/GServerlessSupportLaravel/Cache/InstanceLocal.php

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,35 @@
66

77
use Illuminate\Cache\CacheManager;
88
use Illuminate\Contracts\Cache\Repository;
9-
9+
use Illuminate\Support\Facades\Cache;
10+
11+
/**
12+
* @deprecated Use the 'instance-scoped' cache store instead.
13+
* Example: Cache::store('instance-scoped')->get('key');
14+
*
15+
* @mixin \Illuminate\Contracts\Cache\Repository
16+
*/
1017
class InstanceLocal extends CacheManager
1118
{
1219
private static $instance;
1320

14-
private $driver;
15-
1621
/**
17-
* Dynamically call the default driver instance (statically).
22+
* Handle static calls like InstanceLocal::get().
23+
* Optimized to skip local instantiation.
1824
*
19-
* @param string $method
20-
* @param array $parameters
21-
*
22-
* @return mixed
25+
* @param mixed $method
26+
* @param mixed $parameters
2327
*/
2428
public static function __callStatic($method, $parameters)
2529
{
26-
return self::getInstance()->store()->{$method}(...$parameters);
30+
return Cache::store('instance-scoped')->{$method}(...$parameters);
2731
}
2832

2933
/**
3034
* Get a singleton instance of this class.
35+
* Keeps backward compatibility for code using InstanceLocal::getInstance()->get().
3136
*
32-
* @return Repository
37+
* @return static
3338
*/
3439
public static function getInstance()
3540
{
@@ -41,28 +46,15 @@ public static function getInstance()
4146
}
4247

4348
/**
44-
* Get a cache store instance by name.
49+
* Override the store method to redirect instance calls.
50+
* This handles: $instance = new InstanceLocal(...); $instance->get(...);.
4551
*
4652
* @param null|string $name
4753
*
4854
* @return Repository
4955
*/
5056
public function store($name = null)
5157
{
52-
return $this->getDriver();
53-
}
54-
55-
/**
56-
* Get the driver instance.
57-
*
58-
* @return Repository
59-
*/
60-
private function getDriver()
61-
{
62-
if (!$this->driver) {
63-
$this->driver = $this->createFileDriver(['path' => '/tmp/cache/GServerlessSupportLaravel']);
64-
}
65-
66-
return $this->driver;
58+
return Cache::store('instance-scoped');
6759
}
6860
}

src/AffordableMobiles/GServerlessSupportLaravel/Database/Connectors/ConnectionFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace AffordableMobiles\GServerlessSupportLaravel\Database\Connectors;
66

7-
use AffordableMobiles\GServerlessSupportLaravel\Cache\InstanceLocal as InstanceLocalCache;
87
use AffordableMobiles\sqlcommenter\Connectors\ConnectionFactory as BaseConnectionFactory;
98
use Illuminate\Contracts\Debug\ExceptionHandler;
109
use Illuminate\Support\Arr;
10+
use Illuminate\Support\Facades\Cache;
1111

1212
class ConnectionFactory extends BaseConnectionFactory
1313
{
@@ -66,7 +66,7 @@ protected function createPdoResolverWithSockets(array $config)
6666
$cacheKey = 'GServerlessSupportLaravel-Database-ConnectionFactory-'.$config['name'];
6767
$sockets = Arr::shuffle($this->parseSockets($config));
6868

69-
$current = is_g_serverless_development() ? null : InstanceLocalCache::get($cacheKey);
69+
$current = is_g_serverless_development() ? null : Cache::store('instance-scoped')->get($cacheKey);
7070
if (!empty($current)) {
7171
$sockets = array_filter($sockets, static fn ($socket) => $socket !== $current);
7272
array_unshift($sockets, $current);
@@ -81,7 +81,7 @@ protected function createPdoResolverWithSockets(array $config)
8181
$runtimeConfig = $this->resolveRuntimeConfig($config);
8282
$connection = $this->createConnector($runtimeConfig)->connect($runtimeConfig);
8383

84-
InstanceLocalCache::forever($cacheKey, $socket);
84+
Cache::store('instance-scoped')->forever($cacheKey, $socket);
8585

8686
return $connection;
8787
} catch (\PDOException $e) {

src/AffordableMobiles/GServerlessSupportLaravel/GServerlessSupportServiceProvider.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function register(): void
3434
__DIR__.'/../../config/gserverlesssupport.php',
3535
'gserverlesssupport'
3636
);
37+
38+
$this->registerInstanceScopedCache();
3739
}
3840

3941
/**
@@ -87,6 +89,31 @@ public function provides()
8789
return ['g-serverless-support'];
8890
}
8991

92+
/**
93+
* Registers the instance-scoped cache store configuration.
94+
*/
95+
protected function registerInstanceScopedCache(): void
96+
{
97+
$config = $this->app->make('config');
98+
99+
$cachePath = $config->get(
100+
'gserverlesssupport.system_cache_path',
101+
'/tmp/cache/GServerlessSupportLaravel' // Fallback
102+
);
103+
104+
$defaults = [
105+
'driver' => 'file',
106+
'path' => $cachePath,
107+
];
108+
109+
$userConfig = $config->get('cache.stores.instance-scoped', []);
110+
111+
$config->set(
112+
'cache.stores.instance-scoped',
113+
array_merge($defaults, $userConfig)
114+
);
115+
}
116+
90117
/**
91118
* Finds and shares the versioned asset paths from the Vite manifest.
92119
*/

src/config/gserverlesssupport.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,19 @@
2323
'logout_redirect' => '/',
2424
],
2525
],
26+
27+
/*
28+
|--------------------------------------------------------------------------
29+
| Instance-Local Cache Path
30+
|--------------------------------------------------------------------------
31+
|
32+
| Defines the storage path for the 'instance-scoped' cache store.
33+
|
34+
| This store provides a local, per-instance cache (living in /tmp) that
35+
| is NOT shared between serverless instances. It is used internally
36+
| by this package (for DB sockets, OIDC keys) and is available for
37+
| your application via: Cache::store('instance-scoped')->get(...);
38+
|
39+
*/
40+
'system_cache_path' => env('G_SERVERLESS_SYSTEM_CACHE_PATH', '/tmp/cache/GServerlessSupportLaravel'),
2641
];

0 commit comments

Comments
 (0)