Skip to content

Commit c9695fb

Browse files
authored
Merge pull request #2 from Cyber-Duck/feature/cache_duration_config
- default cache duration 2 hours
2 parents 007a984 + 99e9f76 commit c9695fb

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/Controller/PardotController.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use SilverStripe\View\ArrayData;
1818
use Psr\SimpleCache\CacheInterface;
1919
use SilverStripe\Core\Injector\Injector;
20+
use SilverStripe\Core\Environment;
2021

2122
/**
2223
* Pardot Controller
@@ -29,7 +30,7 @@ class PardotController extends Controller
2930
{
3031
protected static $FORMS_CACHE_KEY = 'pardot_cache_forms';
3132
protected static $DYNAMIC_CONTENTS_CACHE_KEY = 'pardot_dynamic_contents';
32-
protected static $CACHE_DURATION = ( 60 * 60 ) * 6; //6 hours
33+
protected static $CACHE_DURATION = ( 60 * 60 ) * 2; //2 hours
3334

3435
private static $url_segment = 'pardot';
3536

@@ -127,15 +128,16 @@ private function getForms()
127128
return unserialize($cache->get(self::$FORMS_CACHE_KEY));
128129
}
129130

130-
foreach (PardotApiService::getApi()->form()->query()->form as $form) {
131+
$queryForm = PardotApiService::getApi()->form()->query()->form;
132+
foreach ($queryForm as $form) {
131133
$forms->push(ArrayData::create([
132134
'ID' => $form->id,
133135
'Title' => sprintf('%s - %s', $form->campaign->name, $form->name),
134136
'EmbedCode' => $form->embedCode,
135137
]));
136138
}
137139
$formList = $forms->Sort('Title')->map();
138-
$cache->set(self::$FORMS_CACHE_KEY, serialize($formList), self::$CACHE_DURATION);
140+
$cache->set(self::$FORMS_CACHE_KEY, serialize($formList), static::getCacheDuration());
139141

140142
return $formList;
141143
}
@@ -159,8 +161,15 @@ private function getDynamicContent()
159161
]));
160162
}
161163
$contentList = $contents->Sort('Title')->map();
162-
$cache->set(self::$DYNAMIC_CONTENTS_CACHE_KEY, serialize($formList), self::$CACHE_DURATION);
164+
$cache->set(self::$DYNAMIC_CONTENTS_CACHE_KEY, serialize($formList), static::getCacheDuration());
163165

164166
return $contentList;
165167
}
168+
169+
protected static function getCacheDuration()
170+
{
171+
$duration = Environment::getEnv('PARDOT_CACHE_DURATION');
172+
173+
return ((int)$duration > 0) ? (int)$duration: static::$CACHE_DURATION;
174+
}
166175
}

src/Provider/PardotShortCodeProvider.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CyberDuck\Pardot\Service\PardotApiService;
66
use Psr\SimpleCache\CacheInterface;
7+
use SilverStripe\Core\Environment;
78
use SilverStripe\Core\Injector\Injector;
89

910
/**
@@ -17,7 +18,7 @@ class PardotShortCodeProvider
1718
{
1819
protected static $FORM_CACHE_KEY_PREFIX = "form_cache_key";
1920
protected static $DYNAMIC_CONTENT_CACHE_KEY_PREFIX = "dynamic_content_cache_key";
20-
protected static $CACHE_DURATION = ( 60 * 60 ) * 6; //6 hours
21+
protected static $CACHE_DURATION = ( 60 * 60 ) * 2; //2 hours
2122

2223
/**
2324
* Renders a Pardot form
@@ -40,7 +41,7 @@ public static function PardotForm($arguments, $content, $parser, $shortcode, $ex
4041

4142
if (! $form) {
4243
$form = PardotApiService::getApi()->form()->read($arguments['id']);
43-
$cache->set(self::formCacheKey($arguments['id']), serialize($content), self::$CACHE_DURATION);
44+
$cache->set(self::formCacheKey($arguments['id']), serialize($content), static::getCacheDuration());
4445
}
4546

4647
$code = $form->embedCode;
@@ -76,7 +77,7 @@ public static function PardotDynamicContent($arguments, $content, $parser, $shor
7677

7778
if (! $content) {
7879
$content = PardotApiService::getApi()->dynamicContent()->read($arguments['id']);
79-
$cache->set(self::dynamicContentCacheKey($arguments['id']), serialize($content), self::$CACHE_DURATION);
80+
$cache->set(self::dynamicContentCacheKey($arguments['id']), serialize($content), static::getCacheDuration());
8081
}
8182

8283
return $content->embedCode;
@@ -91,4 +92,11 @@ private static function dynamicContentCacheKey($id)
9192
{
9293
return self::$DYNAMIC_CONTENT_CACHE_KEY_PREFIX . $id;
9394
}
95+
96+
protected static function getCacheDuration()
97+
{
98+
$duration = Environment::getEnv('PARDOT_CACHE_DURATION');
99+
100+
return ((int)$duration > 0) ? (int)$duration: static::$CACHE_DURATION;
101+
}
94102
}

0 commit comments

Comments
 (0)