33
0
mirror of https://github.com/joomla-extensions/patchtester.git synced 2024-12-23 11:29:00 +00:00

Implement caching mechanism

This commit is contained in:
Michael Babker 2013-07-14 14:34:29 -05:00
parent cacb1c20ee
commit 0238672232
5 changed files with 129 additions and 49 deletions

View File

@ -29,7 +29,7 @@
<option value="0">JNO</option> <option value="0">JNO</option>
</field> </field>
<field name="cache_lifetime" type="password" default="60" <field name="cache_lifetime" type="text" default="60"
description="COM_PATCHTESTER_FIELD_CACHE_LIFETIME_DESC" description="COM_PATCHTESTER_FIELD_CACHE_LIFETIME_DESC"
label="COM_PATCHTESTER_FIELD_CACHE_LIFETIME_LABEL" label="COM_PATCHTESTER_FIELD_CACHE_LIFETIME_LABEL"
/> />

View File

@ -23,4 +23,32 @@ class PatchTesterController extends JControllerLegacy
* @since 1.0 * @since 1.0
*/ */
protected $default_view = 'pulls'; protected $default_view = 'pulls';
/**
* Method to purge the cache
*
* @return void
*
* @since 2.0
*/
public function purge()
{
// Check for request forgeries
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
jimport('joomla.filesystem.file');
if (file_exists(JPATH_CACHE . '/patchtester.json') && !JFile::delete(JPATH_CACHE . '/patchtester.json'))
{
$msg = JText::_('COM_PATCHTESTER_PURGE_FAIL');
$msgType = 'error';
}
else
{
$msg = JText::_('COM_PATCHTESTER_PURGE_SUCCESS');
$msgType = 'message';
}
$this->setRedirect('index.php?option=com_patchtester&view=pulls', $msg, $msgType);
}
} }

View File

@ -33,6 +33,9 @@ COM_PATCHTESTER_NO_CREDENTIALS="No user credentials are saved, this will allow o
COM_PATCHTESTER_NOT_APPLIED="Not Applied" COM_PATCHTESTER_NOT_APPLIED="Not Applied"
COM_PATCHTESTER_OPEN_IN_GITHUB="Open in GitHub" COM_PATCHTESTER_OPEN_IN_GITHUB="Open in GitHub"
COM_PATCHTESTER_OPEN_IN_JOOMLACODE="Open in JoomlaCode" COM_PATCHTESTER_OPEN_IN_JOOMLACODE="Open in JoomlaCode"
COM_PATCHTESTER_PURGE_CACHE="Purge Cache"
COM_PATCHTESTER_PURGE_FAIL="Could not purge the cache"
COM_PATCHTESTER_PURGE_SUCCESS="Cache purged successfully"
COM_PATCHTESTER_REPO_IS_GONE="The patch could not be applied because the repository is missing" COM_PATCHTESTER_REPO_IS_GONE="The patch could not be applied because the repository is missing"
COM_PATCHTESTER_REVERT_OK="Patch successfully reverted" COM_PATCHTESTER_REVERT_OK="Patch successfully reverted"
COM_PATCHTESTER_REVERT_PATCH="Revert Patch" COM_PATCHTESTER_REVERT_PATCH="Revert Patch"

View File

@ -164,17 +164,63 @@ class PatchtesterModelPulls extends JModelList
$this->ordering = $this->getState('list.ordering', 'title'); $this->ordering = $this->getState('list.ordering', 'title');
$this->orderDir = $this->getState('list.direction', 'asc'); $this->orderDir = $this->getState('list.direction', 'asc');
$search = $this->getState('filter.search');
$searchId = $this->getState('filter.searchid');
$page = $this->getPagination()->pagesCurrent;
try try
{
$cacheFile = JPATH_CACHE . '/patchtester.json';
$params = $this->getState('params');
// Check if caching is enabled
if ($params->get('cache', 1) == 1)
{
// Fetch cache time from component parameters and convert to seconds
$cacheTime = $params->get('cache_lifetime', 60);
$cacheTime = $cacheTime * 60;
// Cache files expired?
if (!file_exists($cacheFile) || (time() - @filemtime($cacheFile) > $cacheTime))
{
// Do a request to the GitHub API for new data
$pulls = $this->requestFromGithub();
}
else
{
// Render from the cached data
$pulls = json_decode(file_get_contents($cacheFile));
}
}
else
{
// No caching, request from GitHub
$pulls = $this->requestFromGithub();
}
return $pulls;
}
catch (Exception $e)
{
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
return array();
}
}
/**
* Method to request new data from GitHub
*
* @return array Pull request data
*
* @since 2.0
*/
protected function requestFromGithub()
{ {
// If over the API limit, we can't build this list // If over the API limit, we can't build this list
// TODO - Cache the request data in case of API limiting
if ($this->rate->remaining > 0) if ($this->rate->remaining > 0)
{ {
$page = $this->getPagination()->pagesCurrent;
$search = $this->getState('filter.search');
$searchId = $this->getState('filter.searchid');
$pulls = $this->github->pulls->getList($this->getState('github_user'), $this->getState('github_repo'), 'open', $page); $pulls = $this->github->pulls->getList($this->getState('github_user'), $this->getState('github_repo'), 'open', $page);
usort($pulls, array($this, 'sortItems')); usort($pulls, array($this, 'sortItems'));
@ -218,6 +264,15 @@ class PatchtesterModelPulls extends JModelList
} }
} }
} }
// If caching is enabled, save the request data
$params = $this->getState('params');
if ($params->get('cache') == 1)
{
$data = json_encode($pulls);
file_put_contents(JPATH_CACHE . '/patchtester.json', $data);
}
} }
else else
{ {
@ -226,13 +281,6 @@ class PatchtesterModelPulls extends JModelList
return $pulls; return $pulls;
} }
catch (Exception $e)
{
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
return array();
}
}
/** /**
* Method to sort the items array * Method to sort the items array

View File

@ -117,6 +117,7 @@ class PatchtesterViewPulls extends JViewLegacy
protected function addToolbar() protected function addToolbar()
{ {
JToolBarHelper::title(JText::_('COM_PATCHTESTER'), 'patchtester'); JToolBarHelper::title(JText::_('COM_PATCHTESTER'), 'patchtester');
JToolbarHelper::custom('purge', 'delete.png', 'delete_f2.png', 'COM_PATCHTESTER_PURGE_CACHE', false);
JToolBarHelper::preferences('com_patchtester'); JToolBarHelper::preferences('com_patchtester');
JFactory::getDocument()->addStyleDeclaration( JFactory::getDocument()->addStyleDeclaration(