From d76439158d729a89dc08ec6f304ee06213de73a6 Mon Sep 17 00:00:00 2001 From: Wai Yan Hein Date: Fri, 14 Jun 2019 14:44:21 +0100 Subject: [PATCH] added caching for 6 hours. --- src/Controller/PardotController.php | 27 ++++++++++++- src/Provider/PardotShortCodeProvider.php | 48 +++++++++++++++++++++--- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/Controller/PardotController.php b/src/Controller/PardotController.php index 66399a6..a4e403b 100644 --- a/src/Controller/PardotController.php +++ b/src/Controller/PardotController.php @@ -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 @@ -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 = [ @@ -115,6 +121,12 @@ 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, @@ -122,11 +134,19 @@ private function getForms() '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]; @@ -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; } } diff --git a/src/Provider/PardotShortCodeProvider.php b/src/Provider/PardotShortCodeProvider.php index 621664f..6e93441 100644 --- a/src/Provider/PardotShortCodeProvider.php +++ b/src/Provider/PardotShortCodeProvider.php @@ -3,6 +3,8 @@ namespace CyberDuck\Pardot\Provider; use CyberDuck\Pardot\Service\PardotApiService; +use Psr\SimpleCache\CacheInterface; +use SilverStripe\Core\Injector\Injector; /** * Pardot Controller @@ -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 * @@ -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( - '>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; } -} \ No newline at end of file + + 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; + } +}