From 99e9f76eea51899785fc16f50a33ce35d4eb3c6c Mon Sep 17 00:00:00 2001 From: Wai Yan Hein Date: Wed, 19 Jun 2019 17:15:01 +0100 Subject: [PATCH] - default cache duration 2 hours - env variable for cache duration --- src/Controller/PardotController.php | 17 +++++++++++++---- src/Provider/PardotShortCodeProvider.php | 14 +++++++++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/Controller/PardotController.php b/src/Controller/PardotController.php index a4e403b..6e1b135 100644 --- a/src/Controller/PardotController.php +++ b/src/Controller/PardotController.php @@ -17,6 +17,7 @@ use SilverStripe\View\ArrayData; use Psr\SimpleCache\CacheInterface; use SilverStripe\Core\Injector\Injector; +use SilverStripe\Core\Environment; /** * Pardot Controller @@ -29,7 +30,7 @@ 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 + protected static $CACHE_DURATION = ( 60 * 60 ) * 2; //2 hours private static $url_segment = 'pardot'; @@ -127,7 +128,8 @@ private function getForms() return unserialize($cache->get(self::$FORMS_CACHE_KEY)); } - foreach (PardotApiService::getApi()->form()->query()->form as $form) { + $queryForm = PardotApiService::getApi()->form()->query()->form; + foreach ($queryForm as $form) { $forms->push(ArrayData::create([ 'ID' => $form->id, 'Title' => sprintf('%s - %s', $form->campaign->name, $form->name), @@ -135,7 +137,7 @@ private function getForms() ])); } $formList = $forms->Sort('Title')->map(); - $cache->set(self::$FORMS_CACHE_KEY, serialize($formList), self::$CACHE_DURATION); + $cache->set(self::$FORMS_CACHE_KEY, serialize($formList), static::getCacheDuration()); return $formList; } @@ -159,8 +161,15 @@ private function getDynamicContent() ])); } $contentList = $contents->Sort('Title')->map(); - $cache->set(self::$DYNAMIC_CONTENTS_CACHE_KEY, serialize($formList), self::$CACHE_DURATION); + $cache->set(self::$DYNAMIC_CONTENTS_CACHE_KEY, serialize($formList), static::getCacheDuration()); return $contentList; } + + protected static function getCacheDuration() + { + $duration = Environment::getEnv('PARDOT_CACHE_DURATION'); + + return ((int)$duration > 0) ? (int)$duration: static::$CACHE_DURATION; + } } diff --git a/src/Provider/PardotShortCodeProvider.php b/src/Provider/PardotShortCodeProvider.php index 6e93441..f27bfa1 100644 --- a/src/Provider/PardotShortCodeProvider.php +++ b/src/Provider/PardotShortCodeProvider.php @@ -4,6 +4,7 @@ use CyberDuck\Pardot\Service\PardotApiService; use Psr\SimpleCache\CacheInterface; +use SilverStripe\Core\Environment; use SilverStripe\Core\Injector\Injector; /** @@ -17,7 +18,7 @@ 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 + protected static $CACHE_DURATION = ( 60 * 60 ) * 2; //2 hours /** * Renders a Pardot form @@ -40,7 +41,7 @@ public static function PardotForm($arguments, $content, $parser, $shortcode, $ex if (! $form) { $form = PardotApiService::getApi()->form()->read($arguments['id']); - $cache->set(self::formCacheKey($arguments['id']), serialize($content), self::$CACHE_DURATION); + $cache->set(self::formCacheKey($arguments['id']), serialize($content), static::getCacheDuration()); } $code = $form->embedCode; @@ -76,7 +77,7 @@ public static function PardotDynamicContent($arguments, $content, $parser, $shor if (! $content) { $content = PardotApiService::getApi()->dynamicContent()->read($arguments['id']); - $cache->set(self::dynamicContentCacheKey($arguments['id']), serialize($content), self::$CACHE_DURATION); + $cache->set(self::dynamicContentCacheKey($arguments['id']), serialize($content), static::getCacheDuration()); } return $content->embedCode; @@ -91,4 +92,11 @@ private static function dynamicContentCacheKey($id) { return self::$DYNAMIC_CONTENT_CACHE_KEY_PREFIX . $id; } + + protected static function getCacheDuration() + { + $duration = Environment::getEnv('PARDOT_CACHE_DURATION'); + + return ((int)$duration > 0) ? (int)$duration: static::$CACHE_DURATION; + } }