Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

Fix: Removed the cache logic from module controller #299

Merged
merged 1 commit into from
Jan 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions config/autoload/cache.local.php.dist
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
<?php
return array(
'zfmodule' => array(
/**
* Cache configuration
*/
'cache' => array(
'adapter' => array(
'name' => 'filesystem',
'options' => array(
'cache_dir' => realpath('./data/cache'),
'writable' => true,
),
),
'plugins' => array(
'exception_handler' => array('throw_exceptions' => true),
'serializer'
)
),
'cache_key' => 'zfmodule_app',
),
'edpgithub' => array(
/**
* Cache configuration
Expand Down
19 changes: 0 additions & 19 deletions module/ZfModule/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,4 @@
'composerView' => 'ZfModule\View\Helper\ComposerView',
),
),
'zfmodule' => array(
/**
* Cache configuration
*/
'cache' => array(
'adapter' => array(
'name' => 'filesystem',
'options' => array(
'cache_dir' => realpath('./data/cache'),
'writable' => false,
),
),
'plugins' => array(
'exception_handler' => array('throw_exceptions' => true),
'serializer'
)
),
'cache_key' => 'zfmodule_app',
),
);
68 changes: 3 additions & 65 deletions module/ZfModule/src/ZfModule/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@

class IndexController extends AbstractActionController
{
/**
* @var Cache\Storage\StorageInterface
*/
private $moduleCache;

/**
* @var Mapper\Module
*/
Expand All @@ -29,34 +24,23 @@ class IndexController extends AbstractActionController
*/
private $moduleService;

/**
* @var Client
*/
private $githubClient;

/**
* @var RepositoryRetriever
*/
private $repositoryRetriever;

/**
* @param Cache\Storage\StorageInterface $moduleCache
* @param Mapper\Module $moduleMapper
* @param Service\Module $moduleService
* @param Client $githubClient
* @param RepositoryRetriever $repositoryRetriever
*/
public function __construct(
Cache\Storage\StorageInterface $moduleCache,
Mapper\Module $moduleMapper,
Service\Module $moduleService,
Client $githubClient,
RepositoryRetriever $repositoryRetriever
) {
$this->moduleCache = $moduleCache;
$this->moduleMapper = $moduleMapper;
$this->moduleService = $moduleService;
$this->githubClient = $githubClient;
$this->repositoryRetriever = $repositoryRetriever;
}

Expand All @@ -71,15 +55,8 @@ public function viewAction()
return;
}

$repositoryCacheKey = 'module-view-' . $vendor . '-' . $module;
$repository = $this->repositoryRetriever->getUserRepositoryMetadata($vendor, $module);

$httpClient = $this->githubClient->getHttpClient();
$response= $httpClient->getResponse();
if ($response->getStatusCode() == Http\Response::STATUS_CODE_304 && $this->moduleCache->hasItem($repositoryCacheKey)) {
return $this->moduleCache->getItem($repositoryCacheKey);
}

$readme = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'README.md');
$license = $this->repositoryRetriever->getRepositoryFileContent($vendor, $module, 'LICENSE');
$license = !$license ? 'No license file found for this Module' : $license;
Expand All @@ -96,8 +73,6 @@ public function viewAction()
'license' => $license,
));

$this->moduleCache->setItem($repositoryCacheKey, $viewModel);

return $viewModel;
}

Expand All @@ -115,11 +90,7 @@ public function indexAction()
);

$repos = $this->repositoryRetriever->getAuthenticatedUserRepositories($params);

$identity = $this->zfcUserAuthentication()->getIdentity();
$cacheKey = 'modules-user-' . $identity->getId();

$repositories = $this->fetchModules($repos, $cacheKey);
$repositories = $this->fetchModules($repos);

$viewModel = new ViewModel(array('repositories' => $repositories));
$viewModel->setTerminal(true);
Expand All @@ -140,11 +111,8 @@ public function organizationAction()
);

$repos = $this->repositoryRetriever->getUserRepositories($owner, $params);
$repositories = $this->fetchModules($repos);

$identity = $this->zfcUserAuthentication()->getIdentity();
$cacheKey = 'modules-organization-' . $identity->getId() . '-' . $owner;

$repositories = $this->fetchModules($repos, $cacheKey);
$viewModel = new ViewModel(array('repositories' => $repositories));
$viewModel->setTerminal(true);
$viewModel->setTemplate('zf-module/index/index.phtml');
Expand All @@ -153,38 +121,19 @@ public function organizationAction()

/**
* @param RepositoryCollection $repos
* @param string $cacheKey
* @return array
*/
private function fetchModules(RepositoryCollection $repos, $cacheKey)
private function fetchModules(RepositoryCollection $repos)
{
$cacheKey .= '-github';

$repositories = array();

foreach ($repos as $repo) {
$isModule = $this->moduleService->isModule($repo);
//Verify if repos have been modified
$httpClient = $this->githubClient->getHttpClient();
/* @var $response \Zend\Http\Response */
$response = $httpClient->getResponse();

$hasCache = $this->moduleCache->hasItem($cacheKey);

if ($response->getStatusCode() == Http\Response::STATUS_CODE_304 && $hasCache) {
$repositories = $this->moduleCache->getItem($cacheKey);
break;
}

if (!$repo->fork && $repo->permissions->push && $isModule && !$this->moduleMapper->findByName($repo->name)) {
$repositories[] = $repo;
$this->moduleCache->removeItem($cacheKey);
}
}

//save list of modules to cache
$this->moduleCache->setItem($cacheKey, $repositories);

return $repositories;
}

Expand Down Expand Up @@ -236,19 +185,9 @@ public function addAction()
);
}

$this->clearModuleCache();

return $this->redirect()->toRoute('zfcuser');
}

public function clearModuleCache()
{
$identity = $this->zfcUserAuthentication()->getIdentity();

$tags = array($identity->getUsername() . '-' . $identity->getId());
$this->moduleCache->clearByTags($tags);
}

/**
* This function is used to remove a module from the site
* @throws Exception\UnexpectedValueException
Expand Down Expand Up @@ -298,7 +237,6 @@ public function removeAction()
);
}

$this->clearModuleCache();
return $this->redirect()->toRoute('zfcuser');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,18 @@ public function createService(ServiceLocatorInterface $controllerManager)
/* @var ControllerManager $controllerManager */
$serviceManager = $controllerManager->getServiceLocator();

/* @var Cache\Storage\StorageInterface $moduleCache */
$moduleCache = $serviceManager->get('zfmodule_cache');

/* @var Mapper\Module $moduleMapper */
$moduleMapper = $serviceManager->get('zfmodule_mapper_module');

/* @var Service\Module $moduleService */
$moduleService = $serviceManager->get('zfmodule_service_module');

/* @var Client $githubClient */
$githubClient = $serviceManager->get(Client::class);

/* @var RepositoryRetriever $repositoryRetriever */
$repositoryRetriever = $serviceManager->get(RepositoryRetriever::class);

return new IndexController(
$moduleCache,
$moduleMapper,
$moduleService,
$githubClient,
$repositoryRetriever
);
}
Expand Down
6 changes: 0 additions & 6 deletions module/ZfModule/src/ZfModule/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ public function getServiceConfig()
{
return array(
'factories' => array(
'zfmodule_cache' => function ($sm) {
$config = $sm->get('Config');
$storage = StorageFactory::factory($config['zfmodule']['cache']);

return $storage;
},
'zfmodule_mapper_module' => function ($sm) {
$mapper = new Mapper\Module();
$mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter'));
Expand Down