Custom Caching Mechanism

The Descope PHP SDK uses a caching mechanism to store frequently accessed data, like JWKs, for session token validation. By default APCu is used for caching, but if it is not available caching is disabled. In this case, a custom caching mechanism can be implemented using the CacheInterface that exists within the SDK.

Custom Caching with CacheInterface

You can provide a custom caching mechanism using CacheInterface through the SDK. The following methods are supported through the SDK:

  • get(string $key): Retrieve a value by key.
  • set(string $key, $value, int $ttl = 3600): bool: Store a value with a time-to-live (TTL).
  • delete(string $key): bool: Remove a value by key.

Here is an example of setup and use of Laravel's cache system with the Descope SDK:

LaravelCache.php
namespace App\Cache;
 
use Descope\SDK\Cache\CacheInterface;
use Illuminate\Support\Facades\Cache;
 
class LaravelCache implements CacheInterface
{
    public function get(string $key)
    {
        return Cache::get($key);
    }
 
    public function set(string $key, $value, int $ttl = 3600): bool
    {
        // Laravel TTL is in minutes
        return Cache::put($key, $value, max(1, ceil($ttl / 60)));
    }
 
    public function delete(string $key): bool
    {
        return Cache::forget($key);
    }
}
use Descope\SDK\DescopeSDK;
use App\Cache\LaravelCache;
 
$descopeSDK = new DescopeSDK([
    'projectId' => $_ENV['DESCOPE_PROJECT_ID'],
    'managementKey' => $_ENV['DESCOPE_MANAGEMENT_KEY'],
], new LaravelCache());

Additional Resources

Was this helpful?

On this page