Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 5... Línea 5...
5
use GuzzleHttp\Client;
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\TransferException;
6
use GuzzleHttp\Exception\TransferException;
7
use GuzzleHttp\Promise\FulfilledPromise;
7
use GuzzleHttp\Promise\FulfilledPromise;
8
use GuzzleHttp\Promise\Promise;
8
use GuzzleHttp\Promise\Promise;
9
use GuzzleHttp\Promise\RejectedPromise;
9
use GuzzleHttp\Promise\RejectedPromise;
10
use GuzzleHttp\Promise\Utils;
-
 
11
use GuzzleHttp\Psr7\Response;
10
use GuzzleHttp\Psr7\Response;
12
use Kevinrob\GuzzleCache\Strategy\CacheStrategyInterface;
11
use Kevinrob\GuzzleCache\Strategy\CacheStrategyInterface;
13
use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy;
12
use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy;
14
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\RequestInterface;
15
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ResponseInterface;
Línea 60... Línea 59...
60
    protected $safeMethods = ['GET' => true, 'HEAD' => true, 'OPTIONS' => true, 'TRACE' => true];
59
    protected $safeMethods = ['GET' => true, 'HEAD' => true, 'OPTIONS' => true, 'TRACE' => true];
Línea 61... Línea 60...
61
 
60
 
62
    /**
61
    /**
63
     * @param CacheStrategyInterface|null $cacheStrategy
62
     * @param CacheStrategyInterface|null $cacheStrategy
64
     */
63
     */
65
    public function __construct(CacheStrategyInterface $cacheStrategy = null)
64
    public function __construct(?CacheStrategyInterface $cacheStrategy = null)
66
    {
65
    {
Línea 67... Línea 66...
67
        $this->cacheStorage = $cacheStrategy !== null ? $cacheStrategy : new PrivateCacheStrategy();
66
        $this->cacheStorage = $cacheStrategy !== null ? $cacheStrategy : new PrivateCacheStrategy();
68
 
67
 
Línea 109... Línea 108...
109
    /**
108
    /**
110
     * Will be called at the end of the script.
109
     * Will be called at the end of the script.
111
     */
110
     */
112
    public function purgeReValidation()
111
    public function purgeReValidation()
113
    {
112
    {
114
        // Call to \GuzzleHttp\Promise\inspect_all throws error, replacing with the latest one.
-
 
115
        Utils::inspectAll($this->waitingRevalidate);
113
        \GuzzleHttp\Promise\Utils::inspectAll($this->waitingRevalidate);
116
    }
114
    }
Línea 117... Línea 115...
117
 
115
 
118
    /**
116
    /**
119
     * @param callable $handler
117
     * @param callable $handler
Línea 131... Línea 129...
131
                        if (!isset($this->safeMethods[$request->getMethod()])) {
129
                        if (!isset($this->safeMethods[$request->getMethod()])) {
132
                            // Invalidate cache after a call of non-safe method on the same URI
130
                            // Invalidate cache after a call of non-safe method on the same URI
133
                            $response = $this->invalidateCache($request, $response);
131
                            $response = $this->invalidateCache($request, $response);
134
                        }
132
                        }
Línea 135... Línea 133...
135
 
133
 
136
                        return $response->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_MISS);
134
                        return $response->withHeader(static::HEADER_CACHE_INFO, static::HEADER_CACHE_MISS);
137
                    }
135
                    }
138
                );
136
                );
Línea 139... Línea 137...
139
            }
137
            }
140
 
138
 
141
            if ($request->hasHeader(self::HEADER_RE_VALIDATION)) {
139
            if ($request->hasHeader(static::HEADER_RE_VALIDATION)) {
142
                // It's a re-validation request, so bypass the cache!
140
                // It's a re-validation request, so bypass the cache!
Línea 143... Línea 141...
143
                return $handler($request->withoutHeader(self::HEADER_RE_VALIDATION), $options);
141
                return $handler($request->withoutHeader(static::HEADER_RE_VALIDATION), $options);
144
            }
142
            }
145
 
143
 
Línea 162... Línea 160...
162
                if ($cacheEntry->isFresh()
160
                if ($cacheEntry->isFresh()
163
                    && ($minFreshCache === null || $cacheEntry->getStaleAge() + (int)$minFreshCache <= 0)
161
                    && ($minFreshCache === null || $cacheEntry->getStaleAge() + (int)$minFreshCache <= 0)
164
                ) {
162
                ) {
165
                    // Cache HIT!
163
                    // Cache HIT!
166
                    return new FulfilledPromise(
164
                    return new FulfilledPromise(
167
                        $cacheEntry->getResponse()->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_HIT)
165
                        $cacheEntry->getResponse()->withHeader(static::HEADER_CACHE_INFO, static::HEADER_CACHE_HIT)
168
                    );
166
                    );
169
                } elseif ($staleResponse
167
                } elseif ($staleResponse
170
                    || ($maxStaleCache !== null && $cacheEntry->getStaleAge() <= $maxStaleCache)
168
                    || ($maxStaleCache !== null && $cacheEntry->getStaleAge() <= $maxStaleCache)
171
                ) {
169
                ) {
172
                    // Staled cache!
170
                    // Staled cache!
173
                    return new FulfilledPromise(
171
                    return new FulfilledPromise(
174
                        $cacheEntry->getResponse()->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_HIT)
172
                        $cacheEntry->getResponse()->withHeader(static::HEADER_CACHE_INFO, static::HEADER_CACHE_HIT)
175
                    );
173
                    );
176
                } elseif ($cacheEntry->hasValidationInformation() && !$onlyFromCache) {
174
                } elseif ($cacheEntry->hasValidationInformation() && !$onlyFromCache) {
177
                    // Re-validation header
175
                    // Re-validation header
178
                    $request = static::getRequestWithReValidationHeader($request, $cacheEntry);
176
                    $request = static::getRequestWithReValidationHeader($request, $cacheEntry);
Línea 179... Línea 177...
179
 
177
 
180
                    if ($cacheEntry->staleWhileValidate()) {
178
                    if ($cacheEntry->staleWhileValidate()) {
Línea 181... Línea 179...
181
                        static::addReValidationRequest($request, $this->cacheStorage, $cacheEntry);
179
                        static::addReValidationRequest($request, $this->cacheStorage, $cacheEntry);
182
 
180
 
183
                        return new FulfilledPromise(
181
                        return new FulfilledPromise(
184
                            $cacheEntry->getResponse()
182
                            $cacheEntry->getResponse()
185
                                ->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_STALE)
183
                                ->withHeader(static::HEADER_CACHE_INFO, static::HEADER_CACHE_STALE)
186
                        );
184
                        );
187
                    }
185
                    }
188
                }
186
                }
Línea 215... Línea 213...
215
                    if ($response->getStatusCode() == 304 && $cacheEntry instanceof CacheEntry) {
213
                    if ($response->getStatusCode() == 304 && $cacheEntry instanceof CacheEntry) {
216
                        // Not modified => cache entry is re-validate
214
                        // Not modified => cache entry is re-validate
217
                        /** @var ResponseInterface $response */
215
                        /** @var ResponseInterface $response */
218
                        $response = $response
216
                        $response = $response
219
                            ->withStatus($cacheEntry->getResponse()->getStatusCode())
217
                            ->withStatus($cacheEntry->getResponse()->getStatusCode())
220
                            ->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_HIT);
218
                            ->withHeader(static::HEADER_CACHE_INFO, static::HEADER_CACHE_HIT);
221
                        $response = $response->withBody($cacheEntry->getResponse()->getBody());
219
                        $response = $response->withBody($cacheEntry->getResponse()->getBody());
Línea 222... Línea 220...
222
 
220
 
223
                        // Merge headers of the "304 Not Modified" and the cache entry
221
                        // Merge headers of the "304 Not Modified" and the cache entry
224
                        /**
222
                        /**
225
                         * @var string $headerName
223
                         * @var string $headerName
226
                         * @var string[] $headerValue
224
                         * @var string[] $headerValue
227
                         */
225
                         */
228
                        foreach ($cacheEntry->getOriginalResponse()->getHeaders() as $headerName => $headerValue) {
226
                        foreach ($cacheEntry->getOriginalResponse()->getHeaders() as $headerName => $headerValue) {
229
                            if (!$response->hasHeader($headerName) && $headerName !== self::HEADER_CACHE_INFO) {
227
                            if (!$response->hasHeader($headerName) && $headerName !== static::HEADER_CACHE_INFO) {
230
                                $response = $response->withHeader($headerName, $headerValue);
228
                                $response = $response->withHeader($headerName, $headerValue);
231
                            }
229
                            }
Línea 232... Línea 230...
232
                        }
230
                        }
233
 
231
 
234
                        $update = true;
232
                        $update = true;
235
                    } else {
233
                    } else {
Línea 236... Línea 234...
236
                        $response = $response->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_MISS);
234
                        $response = $response->withHeader(static::HEADER_CACHE_INFO, static::HEADER_CACHE_MISS);
237
                    }
235
                    }
238
 
236
 
239
                    return static::addToCache($this->cacheStorage, $request, $response, $update);
-
 
240
                },
237
                    return static::addToCache($this->cacheStorage, $request, $response, $update);
241
                function ($reason) use ($cacheEntry) {
238
                },
242
                    if ($reason instanceof TransferException) {
239
                function ($reason) use ($cacheEntry) {
243
                        $response = static::getStaleResponse($cacheEntry);
-
 
244
                        if ($response instanceof ResponseInterface) {
240
                    $response = static::getStaleResponse($cacheEntry);
Línea 245... Línea 241...
245
                            return $response;
241
                    if ($response instanceof ResponseInterface) {
246
                        }
242
                        return $response;
247
                    }
243
                    }
Línea 301... Línea 297...
301
        CacheEntry $cacheEntry
297
        CacheEntry $cacheEntry
302
    ) {
298
    ) {
303
        // Add the promise for revalidate
299
        // Add the promise for revalidate
304
        if ($this->client !== null) {
300
        if ($this->client !== null) {
305
            /** @var RequestInterface $request */
301
            /** @var RequestInterface $request */
306
            $request = $request->withHeader(self::HEADER_RE_VALIDATION, '1');
302
            $request = $request->withHeader(static::HEADER_RE_VALIDATION, '1');
307
            $this->waitingRevalidate[] = $this->client
303
            $this->waitingRevalidate[] = $this->client
308
                ->sendAsync($request)
304
                ->sendAsync($request)
309
                ->then(function (ResponseInterface $response) use ($request, &$cacheStorage, $cacheEntry) {
305
                ->then(function (ResponseInterface $response) use ($request, &$cacheStorage, $cacheEntry) {
310
                    $update = false;
306
                    $update = false;
Línea 337... Línea 333...
337
    /**
333
    /**
338
     * @param CacheEntry|null $cacheEntry
334
     * @param CacheEntry|null $cacheEntry
339
     *
335
     *
340
     * @return null|ResponseInterface
336
     * @return null|ResponseInterface
341
     */
337
     */
342
    protected static function getStaleResponse(CacheEntry $cacheEntry = null)
338
    protected static function getStaleResponse(?CacheEntry $cacheEntry = null)
343
    {
339
    {
344
        // Return staled cache entry if we can
340
        // Return staled cache entry if we can
345
        if ($cacheEntry instanceof CacheEntry && $cacheEntry->serveStaleIfError()) {
341
        if ($cacheEntry instanceof CacheEntry && $cacheEntry->serveStaleIfError()) {
346
            return $cacheEntry->getResponse()
342
            return $cacheEntry->getResponse()
347
                ->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_STALE);
343
                ->withHeader(static::HEADER_CACHE_INFO, static::HEADER_CACHE_STALE);
348
        }
344
        }
Línea 349... Línea 345...
349
 
345
 
350
        return;
346
        return;
Línea 354... Línea 350...
354
     * @param RequestInterface $request
350
     * @param RequestInterface $request
355
     * @param CacheEntry       $cacheEntry
351
     * @param CacheEntry       $cacheEntry
356
     *
352
     *
357
     * @return RequestInterface
353
     * @return RequestInterface
358
     */
354
     */
359
    protected static function getRequestWithReValidationHeader(RequestInterface $request, CacheEntry $cacheEntry)
355
    protected static function getRequestWithReValidationHeader(RequestInterface $request, ?CacheEntry $cacheEntry)
360
    {
356
    {
361
        if ($cacheEntry->getResponse()->hasHeader('Last-Modified')) {
357
        if ($cacheEntry->getResponse()->hasHeader('Last-Modified')) {
362
            $request = $request->withHeader(
358
            $request = $request->withHeader(
363
                'If-Modified-Since',
359
                'If-Modified-Since',
364
                $cacheEntry->getResponse()->getHeader('Last-Modified')
360
                $cacheEntry->getResponse()->getHeader('Last-Modified')
Línea 379... Línea 375...
379
     *
375
     *
380
     * @return CacheMiddleware the Middleware for Guzzle HandlerStack
376
     * @return CacheMiddleware the Middleware for Guzzle HandlerStack
381
     *
377
     *
382
     * @deprecated Use constructor => `new CacheMiddleware()`
378
     * @deprecated Use constructor => `new CacheMiddleware()`
383
     */
379
     */
384
    public static function getMiddleware(CacheStrategyInterface $cacheStorage = null)
380
    public static function getMiddleware(?CacheStrategyInterface $cacheStorage = null)
385
    {
381
    {
386
        return new self($cacheStorage);
382
        return new self($cacheStorage);
387
    }
383
    }
Línea 388... Línea 384...
388
 
384
 
Línea 397... Línea 393...
397
    {
393
    {
398
        foreach (array_keys($this->httpMethods) as $method) {
394
        foreach (array_keys($this->httpMethods) as $method) {
399
            $this->cacheStorage->delete($request->withMethod($method));
395
            $this->cacheStorage->delete($request->withMethod($method));
400
        }
396
        }
Línea 401... Línea 397...
401
 
397
 
402
        return $response->withHeader(self::HEADER_INVALIDATION, true);
398
        return $response->withHeader(static::HEADER_INVALIDATION, true);
403
    }
399
    }