Skip to content

Commit

Permalink
Added use of Redis/Predis to cache heavy hitters for 20 mins
Browse files Browse the repository at this point in the history
  • Loading branch information
gig3m committed Apr 21, 2013
1 parent cbd2d28 commit afe43ca
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"require": {
"slim/slim": "2.*",
"slim/extras": "*",
"twig/twig": "*"
"twig/twig": "*",
"predis/predis": "0.8.*@dev"
}
}
51 changes: 44 additions & 7 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require '../vendor/autoload.php';
require '../vots.php';

$redis = new Predis\Client();

////////////////////////
// Setup
////////////////////////
Expand All @@ -14,6 +16,10 @@
//Twig hates the config array above
$app->view(new \Slim\Extras\Views\Twig());

//Do Dependancy Injection
$env = $app->environment();
$env['redis'] = $redis;

////////////////////////
// Routes
////////////////////////
Expand Down Expand Up @@ -45,9 +51,17 @@
//Status
$app->get('/status/:last/:vin/', function ($last, $vin) use ($app) {

$jeep = new VOTSService($last, $vin);
if ($jeep->isValid())
//Get environment and DI
$env = $app->environment();
$redis = $env['redis'];

//Check redis cache for record
if ($cached = $redis->get($vin))
{
//decode redis/JSON and pass it to VOTS class
$jeep = new VOTSService(json_decode($cached, TRUE));


$data = array(
'statusCode' => $jeep->getStatusCode(),
'statusDesc' => $jeep->getStatusDesc(),
Expand All @@ -57,12 +71,35 @@
//Do something
$app->render('response.twig', $data);
}
else

//redis has no key, lets go fetch Chrysler's response
else
{
$data = array(
'error' => $jeep->getError()
);
$app->render('error.twig', $data);
$jeep = new VOTSService();
$jeep->getJSON($last, $vin);
if ($jeep->isValid())
{
$data = array(
'statusCode' => $jeep->getStatusCode(),
'statusDesc' => $jeep->getStatusDesc(),
'statusExplanation' => $jeep->getStatusExplanation()
);

//put json into redis on key=vin for caching
$redis->set($vin, json_encode($jeep::$decoded));
$redis->expire($vin,60*20); //cache for 20 minutes

//Do something
$app->render('response.twig', $data);
}
else
{
//set an error message to display, courtesy of Chrysler
$data = array(
'error' => $jeep->getError()
);
$app->render('error.twig', $data);
}
}

});
Expand Down
14 changes: 8 additions & 6 deletions vots.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,19 @@ class VOTSService {
);


public function __construct($lastname, $vin)
public function __construct($json = NULL)
{
$this->lastname = $lastname;
$this->vin = $vin;
self::$decoded = NULL;

$this->getJSON();
self::$decoded = $json;

}

public function getJSON()
public function getJSON($lastname, $vin)
{
//set our variables
$this->lastname = $lastname;
$this->vin = $vin;

//replace a space with +, as Chrysler expects
$lastname = preg_replace("/ /","+",$this->lastname);
$vin = $this->vin;
Expand Down

0 comments on commit afe43ca

Please sign in to comment.