1: <?php
2:
3: namespace Guzzle\Plugin\Cache;
4:
5: use Guzzle\Http\Message\RequestInterface;
6:
7: /**
8: * Default cache key strategy that uses all but the body of a request for the key. Add a cache.key_filter parameter to
9: * a request to modify what gets cached.
10: *
11: * cache.key_filter is a string containing semicolon separated values. Each value contains a comma separated list of
12: * things to filter from the key. The currently supported filters are 'header' and 'query'. For example, to filter out
13: * the 'X-Foo' header, the 'X-Bar' header, and the 'test' query string value, set the filter to
14: * "header=X-Foo,X-Bar; query=test"
15: */
16: class DefaultCacheKeyProvider implements CacheKeyProviderInterface
17: {
18: /**
19: * @var string Request parameter holding the cache key
20: */
21: const CACHE_KEY = 'cache.key';
22:
23: /**
24: * @var string Request parameter holding the cache key filter settings
25: */
26: const CACHE_KEY_FILTER = 'cache.key_filter';
27:
28: /**
29: * @var string Request parameter holding the raw key
30: */
31: const CACHE_KEY_RAW = 'cache.raw_key';
32:
33: /**
34: * {@inheritdoc}
35: */
36: public function getCacheKey(RequestInterface $request)
37: {
38: // See if the key has already been calculated
39: $key = $request->getParams()->get(self::CACHE_KEY);
40:
41: if (!$key) {
42:
43: $cloned = clone $request;
44: $cloned->removeHeader('Cache-Control');
45:
46: // Check to see how and if the key should be filtered
47: foreach (explode(';', $request->getParams()->get(self::CACHE_KEY_FILTER)) as $part) {
48: $pieces = array_map('trim', explode('=', $part));
49: if (isset($pieces[1])) {
50: foreach (array_map('trim', explode(',', $pieces[1])) as $remove) {
51: if ($pieces[0] == 'header') {
52: $cloned->removeHeader($remove);
53: } elseif ($pieces[0] == 'query') {
54: $cloned->getQuery()->remove($remove);
55: }
56: }
57: }
58: }
59:
60: $raw = (string) $cloned;
61: $key = 'GZ' . md5($raw);
62: $request->getParams()->set(self::CACHE_KEY, $key)->set(self::CACHE_KEY_RAW, $raw);
63: }
64:
65: return $key;
66: }
67: }
68: