<?php use Jlam\HorkosBundle\phpFastCache; // phpFastCache Library require_once dirname(__FILE__) . "/phpfastcache/3.0.0/phpfastcache.php"; // OK, setup your cache phpFastCache::$config = array("storage" => "auto", "default_chmod" => 0777, "htaccess" => true, "path" => "", "securityKey" => "auto", "memcache" => array(array("127.0.0.1", 11211, 1)), "redis" => array("host" => "127.0.0.1", "port" => "", "password" => "", "database" => "", "timeout" => ""), "extensions" => array(), "fallback" => "files"); // temporary disabled phpFastCache phpFastCache::$disabled = false;
public function indexAction() { #Get request parameters $election = $this->getRequest()->get('election') ?: self::DEFAULT_ELECTION; $language = 'en'; $fresh = $this->getRequest()->get('fresh'); $format = $this->getRequest()->get('format') ?: 'html'; #Setup caching $cacheKey = "{$election}.{$language}.{$format}"; $root = $this->get('kernel')->getRootDir(); require_once "{$root}/../src/Jlam/HorkosBundle/phpfastcache-final/phpfastcache.php"; phpFastCache::setup("storage", "auto"); $cache = new phpFastCache(); #If a cache is available, use it and return it right away. $response = $cache->get($cacheKey); if ($response !== null && $fresh !== 'yes') { return $response; } /* Doing it with ACL: if ($responseObject = $this->get('cache')->fetch($cacheKey)) { $response = unserialize($responseObject); return $response; } */ # Get the engine class name $engineClassName = self::getScrappingEngineClassName($election); # Set the logger for the riding entity Riding::setLogger($this->get('logger')); #If not, slurp data $engineClassName::initialize($this->container); $engineClassName::scrape(); $engineClassName::validate(); ### Prepare the rendering ##################################################### $summary = $engineClassName::getSummary(); $magnitude = 1; if (!$engineClassName::getScraperError()) { $partyTally = Riding::getPartyTally(FALSE)->getTally(); $jurisdictionTally = Riding::getJurisdictionTally()->getTally(); #$partyTallyWasted = $partyTally['wasted']; #arsort($partyTallyWasted); $summary['totalWastedVotes'] = $jurisdictionTally['wasted']; $magnitude = Riding::calculateMagnitudeWinner(); } else { $partyTally = array(); $jurisdictionTally = array(); $partyTallyWasted = array(); $summary['totalWastedVotes'] = null; } ### Render #################################################################### $response = $this->render("JlamHorkosBundle:Horkos:index.{$format}.twig", array('ridings' => Riding::getAllRdingsSorted(), 'partyTally' => $partyTally, 'jurisdiction' => $jurisdictionTally, 'summary' => $summary, 'election' => $election ? $election : self::DEFAULT_ELECTION, 'error' => $engineClassName::getScraperError(), 'magnitude' => $magnitude)); #Save into caching $date = new \DateTime(); $date->modify('+' . self::CACHE_TTL_SECS . ' seconds'); $response->setPublic(); $response->setExpires($date); $response->setMaxAge(self::CACHE_TTL_SECS); $response->setSharedMaxAge(self::CACHE_TTL_SECS); if ($format == 'csv') { $response->headers->set('Content-Type', 'text/csv'); $response->headers->set('Content-Disposition', 'attachment; filename="export.csv"'); } $cache->set($cacheKey, $response, self::CACHE_TTL_SECS); #Return the controller return $response; }
protected function __setChmodAuto() { return phpFastCache::__setChmodAuto($this->config); }