1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\HttpRequest; |
4
|
|
|
|
5
|
|
|
use Onoi\Cache\Cache; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Simple cache layer from the client-side to avoid repeated requests to |
9
|
|
|
* the same target. |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 1.0 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class CachedCurlRequest extends CurlRequest { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Fixed constant |
20
|
|
|
*/ |
21
|
|
|
const CACHE_PREFIX = 'onoi:http:'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Cache |
25
|
|
|
*/ |
26
|
|
|
private $cache; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var boolean |
30
|
|
|
*/ |
31
|
|
|
private $isFromCache = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @since 1.0 |
35
|
|
|
* |
36
|
|
|
* @param resource $handle |
37
|
|
|
* @param Cache $cache |
38
|
|
|
*/ |
39
|
9 |
|
public function __construct( $handle, Cache $cache ) { |
40
|
9 |
|
parent::__construct( $handle ); |
41
|
|
|
|
42
|
9 |
|
$this->cache = $cache; |
43
|
9 |
|
$this->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_TTL, 60 ); // 60 sec by default |
44
|
9 |
|
$this->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX, '' ); |
45
|
9 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @deprecated since 1.3, use option ONOI_HTTP_REQUEST_RESPONSECACHE_TTL instead |
49
|
|
|
* @since 1.0 |
50
|
|
|
* |
51
|
|
|
* @param integer $expiry |
52
|
|
|
*/ |
53
|
1 |
|
public function setExpiryInSeconds( $expiry ) { |
54
|
1 |
|
$this->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_TTL, (int)$expiry ); |
55
|
1 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @deprecated since 1.3, use option ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX instead |
59
|
|
|
* @since 1.0 |
60
|
|
|
* |
61
|
|
|
* @param string $cachePrefix |
62
|
|
|
*/ |
63
|
2 |
|
public function setCachePrefix( $cachePrefix ) { |
64
|
2 |
|
$this->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX, (string)$cachePrefix ); |
65
|
2 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @deprecated since 1.3, use CachedCurlRequest::isFromCache instead |
69
|
|
|
* @since 1.0 |
70
|
|
|
* |
71
|
|
|
* @return boolean |
72
|
|
|
*/ |
73
|
1 |
|
public function isCached() { |
74
|
1 |
|
return $this->isFromCache(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @since 1.3 |
79
|
|
|
* |
80
|
|
|
* @return boolean |
81
|
|
|
*/ |
82
|
2 |
|
public function isFromCache() { |
83
|
2 |
|
return $this->isFromCache; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @since 1.0 |
88
|
|
|
* |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
5 |
|
public function execute() { |
92
|
|
|
|
93
|
5 |
|
list( $key, $expiry ) = $this->getKeysFromOptions(); |
94
|
5 |
|
$this->isFromCache = false; |
95
|
|
|
|
96
|
5 |
|
if ( $this->cache->contains( $key ) ) { |
97
|
2 |
|
$this->isFromCache = true; |
98
|
2 |
|
return $this->cache->fetch( $key ); |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
$response = parent::execute(); |
102
|
|
|
|
103
|
|
|
// Do not cache any failed response |
104
|
4 |
|
if ( $this->getLastErrorCode() !== 0 ) { |
105
|
1 |
|
return $response; |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
$this->cache->save( |
109
|
3 |
|
$key, |
110
|
3 |
|
$response, |
111
|
|
|
$expiry |
112
|
3 |
|
); |
113
|
|
|
|
114
|
3 |
|
return $response; |
115
|
|
|
} |
116
|
|
|
|
117
|
5 |
|
private function getKeysFromOptions() { |
118
|
|
|
|
119
|
|
|
// curl_init can provide the URL which will set the value to the |
120
|
|
|
// CURLOPT_URL option, ensure to have the URL as part of the options |
121
|
|
|
// independent from where/when it was set |
122
|
5 |
|
$this->setOption( |
123
|
5 |
|
CURLOPT_URL, |
124
|
5 |
|
$this->getLastTransferInfo( CURLINFO_EFFECTIVE_URL ) |
125
|
5 |
|
); |
126
|
|
|
|
127
|
|
|
// Avoid an unsorted order that would create unstable keys |
128
|
5 |
|
ksort( $this->options ); |
129
|
|
|
|
130
|
5 |
|
$expiry = $this->getOption( ONOI_HTTP_REQUEST_RESPONSECACHE_TTL ); |
131
|
5 |
|
$prefix = $this->getOption( ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX ); |
132
|
|
|
|
133
|
5 |
|
$key = $prefix . self::CACHE_PREFIX . md5( |
134
|
5 |
|
json_encode( $this->options ) |
135
|
5 |
|
); |
136
|
|
|
|
137
|
|
|
// Reuse the handle but clear the options |
138
|
5 |
|
$this->options = array(); |
139
|
|
|
|
140
|
5 |
|
return array( $key, $expiry ); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|