Skip to content

Commit

Permalink
- default cache duration 2 hours
Browse files Browse the repository at this point in the history
- env variable for cache duration
  • Loading branch information
waiyanheincyberduck committed Jun 19, 2019
1 parent d764391 commit 99e9f76
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
17 changes: 13 additions & 4 deletions src/Controller/PardotController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use SilverStripe\View\ArrayData;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Environment;

/**
* Pardot Controller
Expand All @@ -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';

Expand Down Expand Up @@ -127,15 +128,16 @@ 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),
'EmbedCode' => $form->embedCode,
]));
}
$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;
}
Expand All @@ -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;
}
}
14 changes: 11 additions & 3 deletions src/Provider/PardotShortCodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

/**
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}

0 comments on commit 99e9f76

Please sign in to comment.