Skip to content

Commit 0238672

Browse files
author
Michael Babker
committed
Implement caching mechanism
1 parent cacb1c2 commit 0238672

File tree

5 files changed

+124
-44
lines changed

5 files changed

+124
-44
lines changed

administrator/components/com_patchtester/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<option value="0">JNO</option>
3030
</field>
3131

32-
<field name="cache_lifetime" type="password" default="60"
32+
<field name="cache_lifetime" type="text" default="60"
3333
description="COM_PATCHTESTER_FIELD_CACHE_LIFETIME_DESC"
3434
label="COM_PATCHTESTER_FIELD_CACHE_LIFETIME_LABEL"
3535
/>

administrator/components/com_patchtester/controller.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,32 @@ class PatchTesterController extends JControllerLegacy
2323
* @since 1.0
2424
*/
2525
protected $default_view = 'pulls';
26+
27+
/**
28+
* Method to purge the cache
29+
*
30+
* @return void
31+
*
32+
* @since 2.0
33+
*/
34+
public function purge()
35+
{
36+
// Check for request forgeries
37+
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
38+
39+
jimport('joomla.filesystem.file');
40+
41+
if (file_exists(JPATH_CACHE . '/patchtester.json') && !JFile::delete(JPATH_CACHE . '/patchtester.json'))
42+
{
43+
$msg = JText::_('COM_PATCHTESTER_PURGE_FAIL');
44+
$msgType = 'error';
45+
}
46+
else
47+
{
48+
$msg = JText::_('COM_PATCHTESTER_PURGE_SUCCESS');
49+
$msgType = 'message';
50+
}
51+
52+
$this->setRedirect('index.php?option=com_patchtester&view=pulls', $msg, $msgType);
53+
}
2654
}

administrator/components/com_patchtester/language/en-GB/en-GB.com_patchtester.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ COM_PATCHTESTER_NO_CREDENTIALS="No user credentials are saved, this will allow o
3333
COM_PATCHTESTER_NOT_APPLIED="Not Applied"
3434
COM_PATCHTESTER_OPEN_IN_GITHUB="Open in GitHub"
3535
COM_PATCHTESTER_OPEN_IN_JOOMLACODE="Open in JoomlaCode"
36+
COM_PATCHTESTER_PURGE_CACHE="Purge Cache"
37+
COM_PATCHTESTER_PURGE_FAIL="Could not purge the cache"
38+
COM_PATCHTESTER_PURGE_SUCCESS="Cache purged successfully"
3639
COM_PATCHTESTER_REPO_IS_GONE="The patch could not be applied because the repository is missing"
3740
COM_PATCHTESTER_REVERT_OK="Patch successfully reverted"
3841
COM_PATCHTESTER_REVERT_PATCH="Revert Patch"

administrator/components/com_patchtester/models/pulls.php

Lines changed: 91 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -164,74 +164,122 @@ public function getItems()
164164

165165
$this->ordering = $this->getState('list.ordering', 'title');
166166
$this->orderDir = $this->getState('list.direction', 'asc');
167-
$search = $this->getState('filter.search');
168-
$searchId = $this->getState('filter.searchid');
169-
170-
$page = $this->getPagination()->pagesCurrent;
171167

172168
try
173169
{
174-
// If over the API limit, we can't build this list
175-
// TODO - Cache the request data in case of API limiting
176-
if ($this->rate->remaining > 0)
170+
$cacheFile = JPATH_CACHE . '/patchtester.json';
171+
$params = $this->getState('params');
172+
173+
// Check if caching is enabled
174+
if ($params->get('cache', 1) == 1)
177175
{
178-
$pulls = $this->github->pulls->getList($this->getState('github_user'), $this->getState('github_repo'), 'open', $page);
179-
usort($pulls, array($this, 'sortItems'));
176+
// Fetch cache time from component parameters and convert to seconds
177+
$cacheTime = $params->get('cache_lifetime', 60);
178+
$cacheTime = $cacheTime * 60;
180179

181-
foreach ($pulls as $i => &$pull)
180+
// Cache files expired?
181+
if (!file_exists($cacheFile) || (time() - @filemtime($cacheFile) > $cacheTime))
182182
{
183-
if ($search && false === strpos($pull->title, $search))
184-
{
185-
unset($pulls[$i]);
186-
continue;
187-
}
183+
// Do a request to the GitHub API for new data
184+
$pulls = $this->requestFromGithub();
185+
}
186+
else
187+
{
188+
// Render from the cached data
189+
$pulls = json_decode(file_get_contents($cacheFile));
190+
}
191+
}
192+
else
193+
{
194+
// No caching, request from GitHub
195+
$pulls = $this->requestFromGithub();
196+
}
188197

189-
if ($searchId && $pull->number != $searchId)
190-
{
191-
unset($pulls[$i]);
192-
continue;
193-
}
198+
return $pulls;
199+
}
200+
catch (Exception $e)
201+
{
202+
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
194203

195-
// Try to find a Joomlacode issue number
196-
$pulls[$i]->joomlacode_issue = 0;
204+
return array();
205+
}
206+
}
197207

198-
$matches = array();
208+
/**
209+
* Method to request new data from GitHub
210+
*
211+
* @return array Pull request data
212+
*
213+
* @since 2.0
214+
*/
215+
protected function requestFromGithub()
216+
{
217+
// If over the API limit, we can't build this list
218+
if ($this->rate->remaining > 0)
219+
{
220+
$page = $this->getPagination()->pagesCurrent;
221+
$search = $this->getState('filter.search');
222+
$searchId = $this->getState('filter.searchid');
199223

200-
preg_match('#\[\#([0-9]+)\]#', $pull->title, $matches);
224+
$pulls = $this->github->pulls->getList($this->getState('github_user'), $this->getState('github_repo'), 'open', $page);
225+
usort($pulls, array($this, 'sortItems'));
226+
227+
foreach ($pulls as $i => &$pull)
228+
{
229+
if ($search && false === strpos($pull->title, $search))
230+
{
231+
unset($pulls[$i]);
232+
continue;
233+
}
234+
235+
if ($searchId && $pull->number != $searchId)
236+
{
237+
unset($pulls[$i]);
238+
continue;
239+
}
240+
241+
// Try to find a Joomlacode issue number
242+
$pulls[$i]->joomlacode_issue = 0;
243+
244+
$matches = array();
245+
246+
preg_match('#\[\#([0-9]+)\]#', $pull->title, $matches);
247+
248+
if (isset($matches[1]))
249+
{
250+
$pulls[$i]->joomlacode_issue = (int) $matches[1];
251+
}
252+
else
253+
{
254+
preg_match('#(http://joomlacode[-\w\./\?\S]+)#', $pull->body, $matches);
201255

202256
if (isset($matches[1]))
203257
{
204-
$pulls[$i]->joomlacode_issue = (int) $matches[1];
205-
}
206-
else
207-
{
208-
preg_match('#(http://joomlacode[-\w\./\?\S]+)#', $pull->body, $matches);
258+
preg_match('#tracker_item_id=([0-9]+)#', $matches[1], $matches);
209259

210260
if (isset($matches[1]))
211261
{
212-
preg_match('#tracker_item_id=([0-9]+)#', $matches[1], $matches);
213-
214-
if (isset($matches[1]))
215-
{
216-
$pulls[$i]->joomlacode_issue = (int) $matches[1];
217-
}
262+
$pulls[$i]->joomlacode_issue = (int) $matches[1];
218263
}
219264
}
220265
}
221266
}
222-
else
267+
268+
// If caching is enabled, save the request data
269+
$params = $this->getState('params');
270+
271+
if ($params->get('cache') == 1)
223272
{
224-
$pulls = array();
273+
$data = json_encode($pulls);
274+
file_put_contents(JPATH_CACHE . '/patchtester.json', $data);
225275
}
226-
227-
return $pulls;
228276
}
229-
catch (Exception $e)
277+
else
230278
{
231-
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
232-
233-
return array();
279+
$pulls = array();
234280
}
281+
282+
return $pulls;
235283
}
236284

237285
/**

administrator/components/com_patchtester/views/pulls/view.html.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public function display($tpl = null)
117117
protected function addToolbar()
118118
{
119119
JToolBarHelper::title(JText::_('COM_PATCHTESTER'), 'patchtester');
120+
JToolbarHelper::custom('purge', 'delete.png', 'delete_f2.png', 'COM_PATCHTESTER_PURGE_CACHE', false);
120121
JToolBarHelper::preferences('com_patchtester');
121122

122123
JFactory::getDocument()->addStyleDeclaration(

0 commit comments

Comments
 (0)