You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Curl transport provide transparent content deflating and cookies persistence. Ok, but what about caching feature. For example I use this in my project:
<?php/** * @author Igor A Tarasov <[email protected]> */declare(strict_types = 1);
useYii;
useyii\base\InvalidConfigException;
useyii\caching\CacheInterface;
useyii\caching\TagDependency;
useyii\di\Instance;
useyii\httpclient\Client;
useyii\httpclient\Request;
useyii\httpclient\Response;
usefunctionis_int;
/** * Client with response caching. * * For caching repeatable requests, headers (Cookies, User-Agent, ...) must be similars for next requests * * @noinspection PhpUnused */class CachingClient extends Client
{
/** @var \yii\caching\CacheInterface */public$cache = 'cache';
/** * @var bool if true, then cache key calculated with cookies. If false, then browsing is inkognito. * Use this only when response depends on cookies. */public$cacheCookies = false;
/** @var int cache time, s */public$cacheDuration;
/** * @inheritDoc * @throws \yii\base\InvalidConfigException */publicfunctioninit()
{
parent::init();
if (! empty($this->cache)) {
$this->cache = Instance::ensure($this->cache, CacheInterface::class);
}
if (empty($this->cacheDuration)) {
$this->cacheDuration = null;
} elseif (! is_int($this->cacheDuration) || $this->cacheDuration < 0) {
thrownewInvalidConfigException('cacheDuration');
}
}
/** * Return cache key for request. * * @param \yii\httpclient\Request $request * @return string[] */protectedfunctioncacheKey(Request$request)
{
$keyRequest = $this->cacheCookies ? $request : (clone$request)->setCookies([]);
return [__METHOD__, $keyRequest->toString()];
}
/** * @inheritDoc * * @throws \yii\base\InvalidConfigException * @throws \yii\httpclient\Exception */publicfunctionsend($request)
{
/** @var string[]|null $cacheKey key for cache */$cacheKey = null;
// load from cacheif (! empty($this->cache)) {
$cacheKey = $this->cacheKey($request);
// load response$response = $this->cache->get($cacheKey);
if ($responseinstanceof Response) {
Yii::debug('Used cached response for request: ' . $request->fullUrl, __METHOD__);
// restore client link$response->client = $request->client;
return$response;
}
}
// fetch from server$response = parent::send($request);
// store in cacheif ($response->isOk && ! empty($this->cache)) {
$cacheResponse = $this->cacheCookies ? $response : (clone$response)->setCookies([]);
// clean connection to client to not save it in cache$cacheResponse->client = null;
// save response$this->cache->set($cacheKey, $cacheResponse, $this->cacheDuration, newTagDependency([
'tags' => [__CLASS__]
]));
}
return$response;
}
}```
The text was updated successfully, but these errors were encountered:
Curl transport provide transparent content deflating and cookies persistence. Ok, but what about caching feature. For example I use this in my project:
The text was updated successfully, but these errors were encountered: