Permalink
Browse files

added cache for request

  • Loading branch information...
chemix committed Jun 12, 2014
1 parent 17cf922 commit e471528d4265d6ee9487a0fbb0401a59a04a3d4a
Showing with 24 additions and 11 deletions.
  1. +24 −11 app/presenters/ImportPresenter.php
@@ -12,30 +12,43 @@
use Facebook\FacebookRequestException;
use Facebook\FacebookSession;
use Tracy\Dumper;
use Nette\Caching\Cache;
/**
* Import presenter.
*/
class ImportPresenter extends BasePresenter
{
/**
* @var \Nette\Caching\IStorage @inject
*/
public $cacheStorage;
public function renderDefault()
{
FacebookSession::setDefaultApplication('YOUR_APP_ID', 'YOUR_APP_SECRET');
$session = FacebookSession::newAppSession();
$data = array();
try {
$request = new FacebookRequest($session, 'GET', '/nettefw/feed');
$response = $request->execute();
$posts = $response->getGraphObject()->asArray();
$data = $posts['data'];
} catch (\Exception $ex) {
throw $ex;
$this->terminate();
$cache = new Cache($this->cacheStorage, 'facebookWall');
$data = $cache->load("stories");
if (empty($data)) {
try {
$request = new FacebookRequest($session, 'GET', '/nettefw/feed');
$response = $request->execute();
$posts = $response->getGraphObject()->asArray();
$data = $posts['data'];
$cache->save("stories", $data, array(
Cache::EXPIRATION => '+30 minutes',
Cache::SLIDING => TRUE
));
} catch (\Exception $ex) {
throw $ex;
$this->terminate();
}
}
Dumper::dump($data);

0 comments on commit e471528

Please sign in to comment.