/
CacheAwareFeedFetcher.php
133 lines (98 loc) · 4 KB
/
CacheAwareFeedFetcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Copyright 2006 - 2013 TubePress LLC (http://tubepress.com)
*
* This file is part of TubePress (http://tubepress.com)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/**
* Base functionality for feed retrieval services.
*/
class tubepress_impl_feed_CacheAwareFeedFetcher implements tubepress_spi_feed_FeedFetcher
{
/**
* @var ehough_epilog_Logger
*/
private $_logger;
public function __construct()
{
$this->_logger = ehough_epilog_LoggerFactory::getLogger('Cache Aware Feed Fetcher');
}
/**
* Fetches the feed from the remote provider
*
* @param string $url The URL to fetch.
*
* @return mixed The raw feed from the provider, or null if there was a problem.
*/
public final function fetch($url)
{
$context = tubepress_impl_patterns_sl_ServiceLocator::getExecutionContext();
$cacheEnabled = $context->get(tubepress_api_const_options_names_Cache::CACHE_ENABLED);
$isDebugEnabled = $this->_logger->isHandling(ehough_epilog_Logger::DEBUG);
if ($cacheEnabled) {
$data = $this->_getFromCache($url, $context, $isDebugEnabled);
} else {
if ($isDebugEnabled) {
$this->_logger->debug(sprintf('Skip cache check for <a href="%s">URL</a>', $url));
}
$data = $this->_getFromNetwork($url);
}
if ($isDebugEnabled) {
$this->_logger->debug(sprintf('Raw result for <a href="%s">URL</a> is in the HTML source for this page. <span style="display:none">%s</span>',
$url, htmlspecialchars($data)));
}
return $data;
}
private function _getFromNetwork($url)
{
$u = new ehough_curly_Url($url);
$request = new ehough_shortstop_api_HttpRequest(ehough_shortstop_api_HttpRequest::HTTP_METHOD_GET, $u);
$client = tubepress_impl_patterns_sl_ServiceLocator::getHttpClient();
$eventDispatcher = tubepress_impl_patterns_sl_ServiceLocator::getEventDispatcher();
$response = $client->execute($request);
$event = new tubepress_spi_event_EventBase($response->getEntity()->getContent(), array(
'request' => $request,
'response' => $response
));
$eventDispatcher->dispatch(tubepress_api_const_event_EventNames::HTTP_RESPONSE, $event);
return $event->getSubject();
}
private function _getFromCache($url, tubepress_spi_context_ExecutionContext $context, $isDebugEnabled)
{
/**
* @var $cache ehough_stash_interfaces_PoolInterface
*/
$cache = tubepress_impl_patterns_sl_ServiceLocator::getCacheService();
if ($isDebugEnabled) {
$this->_logger->debug(sprintf('First asking cache for <a href="%s">URL</a>', $url));
}
$cacheKey = $this->_urlToCacheKey($url);
$result = $cache->getItem($cacheKey);
if ($result && !$result->isMiss()) {
if ($isDebugEnabled) {
$this->_logger->debug(sprintf('Cache has <a href="%s">URL</a>. Sweet.', $url));
}
} else {
if ($isDebugEnabled) {
$this->_logger->debug(sprintf('Cache does not have <a href="%s">URL</a>. We\'ll have to get it from the network.', $url));
}
$data = $this->_getFromNetwork($url);
$storedSuccessfully = $result->set($data, $context->get(tubepress_api_const_options_names_Cache::CACHE_LIFETIME_SECONDS));
if (!$storedSuccessfully) {
if ($isDebugEnabled) {
$this->_logger->debug('Unable to store data to cache');
}
return $data;
}
}
return $result->get();
}
private function _urlToCacheKey($url)
{
return str_replace('/', '~', $url);
}
}