Remove outdated doctrine/cache package and change configuration

This commit is contained in:
2026-05-26 15:38:15 +00:00
parent 9f339953fe
commit 23f00b2374
7 changed files with 295 additions and 518 deletions

View File

@@ -6,15 +6,23 @@
namespace CloudObjects\SDK;
use Exception;
use DateTime, Exception;
use ML\IRI\IRI, ML\JsonLD\JsonLD;
use Doctrine\Common\Cache\RedisCache;
use Psr\Log\LoggerInterface, Psr\Log\LoggerAwareTrait;
use GuzzleHttp\ClientInterface, GuzzleHttp\Client, GuzzleHttp\HandlerStack;
use GuzzleHttp\Exception\RequestException;
use Kevinrob\GuzzleCache\CacheMiddleware, Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage;
use GuzzleHttp\Psr7\Request,
GuzzleHttp\Psr7\Response;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy;
use CloudObjects\SDK\Exceptions\CoreAPIException;
use Kevinrob\GuzzleCache\Storage\CacheStorageInterface,
Kevinrob\GuzzleCache\Storage\Psr6CacheStorage,
Kevinrob\GuzzleCache\Storage\Psr16CacheStorage,
Kevinrob\GuzzleCache\CacheEntry;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;
use CloudObjects\SDK\Exceptions\CoreAPIException,
CloudObjects\SDK\Exceptions\InvalidSDKConfigurationException;
use CloudObjects\SDK\AccountGateway\AccountContext;
/**
@@ -30,13 +38,14 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
private $cache;
private $objects;
private $defaultReader;
private $customCacheDefaultRequest;
const CO_API_URL = 'https://od.coid.link/';
public function __construct($options = []) {
// Merge options with defaults
$this->options = array_merge([
'cache_provider' => 'none',
'cache_storage' => null,
'cache_prefix' => 'clobj:',
'cache_ttl' => 60,
'static_config_path' => null,
@@ -48,35 +57,27 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
'connect_timeout' => 5
], $options);
// Set up object cache
switch ($this->options['cache_provider']) {
case 'none':
// no caching
$this->cache = null;
break;
case 'redis':
// caching with Redis
$redis = new \Redis();
$redis->pconnect(
isset($this->options['cache_provider.redis.host']) ? $this->options['cache_provider.redis.host'] : '127.0.0.1',
isset($this->options['cache_provider.redis.port']) ? $this->options['cache_provider.redis.port'] : 6379);
// Check object cache configuration
if (isset($this->options['cache_storage'])
&& !($this->options['cache_storage'] instanceof CacheStorageInterface)
&& !($this->options['cache_storage'] instanceof CacheItemPoolInterface)
&& !($this->options['cache_storage'] instanceof CacheInterface)
) {
throw new InvalidSDKConfigurationException('Invalid cache_storage specified; must be an instance of Kevinrob\GuzzleCache\CacheStorageInterface, Psr\Cache\CacheItemPoolInterface or Psr\SimpleCache\CacheInterface.');
}
$this->cache = new RedisCache();
$this->cache->setRedis($redis);
break;
case 'file':
// caching on the filesystem
$this->cache = new \Doctrine\Common\Cache\FilesystemCache(
isset($this->options['cache_provider.file.directory']) ? $this->options['cache_provider.file.directory'] : sys_get_temp_dir()
);
break;
default:
throw new Exception('Valid values for cache_provider are: none, redis, file');
if ($this->options['cache_storage'] instanceof CacheStorageInterface) {
$this->cache = $this->options['cache_storage'];
} elseif ($this->options['cache_storage'] instanceof CacheItemPoolInterface) {
$this->cache = new Psr6CacheStorage($this->options['cache_storage']);
} elseif ($this->options['cache_storage'] instanceof CacheInterface) {
$this->cache = new Psr16CacheStorage($this->options['cache_storage']);
}
// Set up logger
if (is_a($this->options['logger'], LoggerInterface::class))
if (is_a($this->options['logger'], LoggerInterface::class)) {
$this->setLogger($this->options['logger']);
}
// Set up handler stack
$stack = HandlerStack::create();
@@ -85,11 +86,14 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
if (isset($this->cache)) {
$stack->push(
new CacheMiddleware(
new PrivateCacheStrategy(
new DoctrineCacheStorage($this->cache)
)
new PrivateCacheStrategy($this->cache)
)
);
// We also use the cache for storing object descriptions and attachments
// without the HTTP request, so we create a dummy request; this way
// we can reuse GuzzleCache for maximum compatibility
$this->customCacheDefaultRequest = new Request('GET', '/');
}
// Initialize client
@@ -127,18 +131,32 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
$this->logger->info($message, [ 'elapsed_ms' => round((microtime(true) - $ts) * 1000) ]);
}
private function getCacheKey($id) {
return $this->options['cache_prefix'].$this->options['auth_ns'].'/'.$id;
public function getCacheKey($id) {
return $this->options['cache_prefix'] . $this->options['auth_ns'] . '/' . $id;
}
private function getFromCache($id) {
return (isset($this->cache) && $this->cache->contains($this->getCacheKey($id)))
? $this->cache->fetch($this->getCacheKey($id)) : null;
if (!$this->cache) {
return null;
}
$cachedData = $this->cache->fetch($this->getCacheKey($id));
if (!$cachedData) {
return null;
}
return $cachedData->getOriginalResponse()->getBody()->getContents();
}
private function putIntoCache($id, $data, $ttl) {
if (isset($this->cache))
$this->cache->save($this->getCacheKey($id), $data, $ttl);
if ($this->cache) {
$entry = new CacheEntry($this->customCacheDefaultRequest,
new Response(200, [], $data),
((new DateTime)->modify('+'.$ttl.' seconds')));
$this->cache->save($this->getCacheKey($id), $entry, $ttl);
}
}
public function getFromCacheCustom($id) {