1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AnimeDb package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
8
|
|
|
*/ |
9
|
|
|
namespace AnimeDb\Bundle\AniDbBrowserBundle\Service\Client; |
10
|
|
|
|
11
|
|
|
use AnimeDb\Bundle\AniDbBrowserBundle\Service\Client\Cache\ExpireResolver; |
12
|
|
|
use AnimeDb\Bundle\AniDbBrowserBundle\Service\Client\Cache\Storage\StorageInterface; |
13
|
|
|
|
14
|
|
|
class CacheClient implements ClientInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ClientInterface |
18
|
|
|
*/ |
19
|
|
|
protected $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ExpireResolver |
23
|
|
|
*/ |
24
|
|
|
protected $resolver; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var StorageInterface |
28
|
|
|
*/ |
29
|
|
|
protected $storage; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param ClientInterface $client |
33
|
|
|
* @param ExpireResolver $resolver |
34
|
|
|
* @param StorageInterface $storage |
35
|
|
|
*/ |
36
|
9 |
|
public function __construct(ClientInterface $client, ExpireResolver $resolver, StorageInterface $storage) |
37
|
|
|
{ |
38
|
9 |
|
$this->client = $client; |
39
|
9 |
|
$this->resolver = $resolver; |
40
|
9 |
|
$this->storage = $storage; |
41
|
9 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param int $timeout |
45
|
|
|
* |
46
|
|
|
* @return CacheClient |
47
|
|
|
*/ |
48
|
1 |
|
public function setTimeout($timeout) |
49
|
|
|
{ |
50
|
1 |
|
$this->client->setTimeout($timeout); |
51
|
|
|
|
52
|
1 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $proxy |
57
|
|
|
* |
58
|
|
|
* @return CacheClient |
59
|
|
|
*/ |
60
|
1 |
|
public function setProxy($proxy) |
61
|
|
|
{ |
62
|
1 |
|
$this->client->setProxy($proxy); |
63
|
|
|
|
64
|
1 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $request |
69
|
|
|
* @param array $params |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
7 |
|
public function get($request, array $params = []) |
74
|
|
|
{ |
75
|
7 |
|
$expires = $this->resolver->getExpire($request, new \DateTime()); |
76
|
7 |
|
if (!$expires) { |
77
|
1 |
|
return $this->client->get($request, $params); |
78
|
|
|
} |
79
|
|
|
|
80
|
6 |
|
$key = http_build_query(['request' => $request] + $params); |
81
|
6 |
|
if (!($response = $this->storage->get($key))) { |
82
|
3 |
|
$response = $this->client->get($request, $params); |
83
|
3 |
|
$this->storage->set($key, $response, $expires); |
84
|
3 |
|
} |
85
|
|
|
|
86
|
6 |
|
return $response; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|