33namespace CyberDuck \Pardot \Provider ;
44
55use CyberDuck \Pardot \Service \PardotApiService ;
6+ use Psr \SimpleCache \CacheInterface ;
7+ use SilverStripe \Core \Injector \Injector ;
68
79/**
810 * Pardot Controller
1315 **/
1416class PardotShortCodeProvider
1517{
18+ protected static $ FORM_CACHE_KEY_PREFIX = "form_cache_key " ;
19+ protected static $ DYNAMIC_CONTENT_CACHE_KEY_PREFIX = "dynamic_content_cache_key " ;
20+ protected static $ CACHE_DURATION = ( 60 * 60 ) * 6 ; //6 hours
21+
1622 /**
1723 * Renders a Pardot form
1824 *
@@ -25,15 +31,27 @@ class PardotShortCodeProvider
2531 */
2632 public static function PardotForm ($ arguments , $ content , $ parser , $ shortcode , $ extra = [])
2733 {
28- $ form = PardotApiService::getApi ()->form ()->read ($ arguments ['id ' ]);
34+ $ form = null ;
35+ $ cache = Injector::inst ()->get (CacheInterface::class . '.cyberduckPardotCache ' );
36+
37+ if ($ cache ->has (self ::formCacheKey ($ arguments ['id ' ]))) {
38+ $ form = unserialize ($ cache ->get (self ::formCacheKey ($ arguments ['id ' ])));
39+ }
40+
41+ if (! $ form ) {
42+ $ form = PardotApiService::getApi ()->form ()->read ($ arguments ['id ' ]);
43+ $ cache ->set (self ::formCacheKey ($ arguments ['id ' ]), serialize ($ content ), self ::$ CACHE_DURATION );
44+ }
45+
2946 $ code = $ form ->embedCode ;
3047 if (array_key_exists ('class ' , $ arguments )) {
3148 $ code = str_replace (
32- '></ ' ,
49+ '></ ' ,
3350 sprintf (' class="%s"></ ' , $ arguments ['class ' ]),
3451 $ code
3552 );
3653 }
54+
3755 return $ code ;
3856 }
3957
@@ -49,8 +67,28 @@ public static function PardotForm($arguments, $content, $parser, $shortcode, $ex
4967 */
5068 public static function PardotDynamicContent ($ arguments , $ content , $ parser , $ shortcode , $ extra = [])
5169 {
52- $ content = PardotApiService::getApi ()->dynamicContent ()->read ($ arguments ['id ' ]);
53-
70+ $ content = null ;
71+ $ cache = Injector::inst ()->get (CacheInterface::class . '.cyberduckPardotCache ' );
72+
73+ if ($ cache ->has (self ::dynamicContentCacheKey ($ arguments ['id ' ]))) {
74+ $ content = unserialize ($ cache ->get (self ::dynamicContentCacheKey ($ arguments ['id ' ])));
75+ }
76+
77+ if (! $ content ) {
78+ $ content = PardotApiService::getApi ()->dynamicContent ()->read ($ arguments ['id ' ]);
79+ $ cache ->set (self ::dynamicContentCacheKey ($ arguments ['id ' ]), serialize ($ content ), self ::$ CACHE_DURATION );
80+ }
81+
5482 return $ content ->embedCode ;
5583 }
56- }
84+
85+ private static function formCacheKey ($ id )
86+ {
87+ return self ::$ FORM_CACHE_KEY_PREFIX . $ id ;
88+ }
89+
90+ private static function dynamicContentCacheKey ($ id )
91+ {
92+ return self ::$ DYNAMIC_CONTENT_CACHE_KEY_PREFIX . $ id ;
93+ }
94+ }
0 commit comments