Skip to content

Commit

Permalink
Merge pull request #1 from Cyber-Duck/feature/caching
Browse files Browse the repository at this point in the history
added caching for 6 hours.
  • Loading branch information
waiyanhein authored Jun 14, 2019
2 parents a517c1c + d764391 commit 007a984
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
27 changes: 25 additions & 2 deletions src/Controller/PardotController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\View\ArrayData;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Core\Injector\Injector;

/**
* Pardot Controller
Expand All @@ -25,6 +27,10 @@
**/
class PardotController extends Controller
{
protected static $FORMS_CACHE_KEY = 'pardot_cache_forms';
protected static $DYNAMIC_CONTENTS_CACHE_KEY = 'pardot_dynamic_contents';
protected static $CACHE_DURATION = ( 60 * 60 ) * 6; //6 hours

private static $url_segment = 'pardot';

private static $allowed_actions = [
Expand Down Expand Up @@ -115,18 +121,32 @@ public function PardotDynamicContentForm()
private function getForms()
{
$forms = ArrayList::create();
$cache = Injector::inst()->get(CacheInterface::class . '.cyberduckPardotCache');

if ($cache->has(self::$FORMS_CACHE_KEY)) {
return unserialize($cache->get(self::$FORMS_CACHE_KEY));
}

foreach (PardotApiService::getApi()->form()->query()->form as $form) {
$forms->push(ArrayData::create([
'ID' => $form->id,
'Title' => sprintf('%s - %s', $form->campaign->name, $form->name),
'EmbedCode' => $form->embedCode,
]));
}
return $forms->Sort('Title')->map();
$formList = $forms->Sort('Title')->map();
$cache->set(self::$FORMS_CACHE_KEY, serialize($formList), self::$CACHE_DURATION);

return $formList;
}

private function getDynamicContent()
{
$cache = Injector::inst()->get(CacheInterface::class . '.cyberduckPardotCache');
if ($cache->has(self::$DYNAMIC_CONTENTS_CACHE_KEY)) {
return unserialize($cache->get(self::$DYNAMIC_CONTENTS_CACHE_KEY));
}

$data = PardotApiService::getApi()->dynamicContent()->query()->dynamicContent;
$data = is_array($data) ? $data : [$data];

Expand All @@ -138,6 +158,9 @@ private function getDynamicContent()
'EmbedCode' => $content->embedCode,
]));
}
return $contents->Sort('Title')->map();
$contentList = $contents->Sort('Title')->map();
$cache->set(self::$DYNAMIC_CONTENTS_CACHE_KEY, serialize($formList), self::$CACHE_DURATION);

return $contentList;
}
}
48 changes: 43 additions & 5 deletions src/Provider/PardotShortCodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace CyberDuck\Pardot\Provider;

use CyberDuck\Pardot\Service\PardotApiService;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Core\Injector\Injector;

/**
* Pardot Controller
Expand All @@ -13,6 +15,10 @@
**/
class PardotShortCodeProvider
{
protected static $FORM_CACHE_KEY_PREFIX = "form_cache_key";
protected static $DYNAMIC_CONTENT_CACHE_KEY_PREFIX = "dynamic_content_cache_key";
protected static $CACHE_DURATION = ( 60 * 60 ) * 6; //6 hours

/**
* Renders a Pardot form
*
Expand All @@ -25,15 +31,27 @@ class PardotShortCodeProvider
*/
public static function PardotForm($arguments, $content, $parser, $shortcode, $extra = [])
{
$form = PardotApiService::getApi()->form()->read($arguments['id']);
$form = null;
$cache = Injector::inst()->get(CacheInterface::class . '.cyberduckPardotCache');

if ($cache->has(self::formCacheKey($arguments['id']))) {
$form = unserialize($cache->get(self::formCacheKey($arguments['id'])));
}

if (! $form) {
$form = PardotApiService::getApi()->form()->read($arguments['id']);
$cache->set(self::formCacheKey($arguments['id']), serialize($content), self::$CACHE_DURATION);
}

$code = $form->embedCode;
if(array_key_exists('class', $arguments)) {
$code = str_replace(
'></',
'></',
sprintf(' class="%s"></', $arguments['class']),
$code
);
}

return $code;
}

Expand All @@ -49,8 +67,28 @@ public static function PardotForm($arguments, $content, $parser, $shortcode, $ex
*/
public static function PardotDynamicContent($arguments, $content, $parser, $shortcode, $extra = [])
{
$content = PardotApiService::getApi()->dynamicContent()->read($arguments['id']);

$content = null;
$cache = Injector::inst()->get(CacheInterface::class . '.cyberduckPardotCache');

if ($cache->has(self::dynamicContentCacheKey($arguments['id']))) {
$content = unserialize($cache->get(self::dynamicContentCacheKey($arguments['id'])));
}

if (! $content) {
$content = PardotApiService::getApi()->dynamicContent()->read($arguments['id']);
$cache->set(self::dynamicContentCacheKey($arguments['id']), serialize($content), self::$CACHE_DURATION);
}

return $content->embedCode;
}
}

private static function formCacheKey($id)
{
return self::$FORM_CACHE_KEY_PREFIX . $id;
}

private static function dynamicContentCacheKey($id)
{
return self::$DYNAMIC_CONTENT_CACHE_KEY_PREFIX . $id;
}
}

0 comments on commit 007a984

Please sign in to comment.