Skip to content
This repository has been archived by the owner on Aug 21, 2019. It is now read-only.

Commit

Permalink
added caching for json file routes
Browse files Browse the repository at this point in the history
this reduces api calls to mailjet while using the panel.

Signed-off-by: Bruno Meilick <b@bnomei.com>
  • Loading branch information
bnomei committed May 17, 2017
1 parent 47d1dad commit 246acac
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
44 changes: 28 additions & 16 deletions kirby-mailjet-class.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,38 +268,48 @@ public static function renderMustache($file, $mustachedata = [], $pluginFolder =

/////////////////////////////////////
//
private static $_segments = null;
public static function segments() {
$mj = self::client();
if(!$mj) return null;
if(self::$_segments) return self::$_segments;

$cl = array();
$exclude = c::get('plugin.mailjet.json-segments.exclude', []);
$response = $mj->get(Resources::$Contactfilter, []);
foreach ($response->getData() as $r) {
if(in_array($r['ID'], $exclude)) continue;
if(in_array($r['Name'], $exclude)) continue;
if($response->success()) {
foreach ($response->getData() as $r) {
if(in_array($r['ID'], $exclude)) continue;
if(in_array($r['Name'], $exclude)) continue;

$cl[$r['ID']] = $r['Name'];
}
$cl[$r['ID']] = $r['Name'];
}
self::$_segments = $cl;
}

return $cl;
}

/////////////////////////////////////
//
private static $_contactslists = null;
public static function contactslists() {
$mj = self::client();
if(!$mj) return null;
if(self::$_contactslists) return self::$_contactslists;

$cl = array();
$exclude = c::get('plugin.mailjet.json-contactslists.exclude', []);
$response = $mj->get(Resources::$Contactslist, []);
foreach ($response->getData() as $r) {
if(in_array($r['ID'], $exclude)) continue;
if(in_array($r['Name'], $exclude)) continue;
if($response->success()) {
foreach ($response->getData() as $r) {
if(in_array($r['ID'], $exclude)) continue;
if(in_array($r['Name'], $exclude)) continue;

$cl[$r['ID']] = $r['Name'];
}
$cl[$r['ID']] = $r['Name'];
}
self::$_contactslists = $cl;
}

return $cl;
}
Expand All @@ -316,12 +326,14 @@ public static function getContactslist($contactslistname) {
} else {
$response = $mj->get(Resources::$Contactslist, ['filters'=>['Name'=>$contactslistname]]);
$contactslistID = -1;
foreach ($response->getData() as $r) {
if($r['Name'] == $contactslistname) {
$contactslistID = $r['ID'];
break;
}
}
if($response->success()) {
foreach ($response->getData() as $r) {
if($r['Name'] == $contactslistname) {
$contactslistID = $r['ID'];
break;
}
}
}
}

if($contactslistID == -1) {
Expand Down
28 changes: 26 additions & 2 deletions kirby-mailjet-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
ROUTES
***************************************/

require_once __DIR__ . '/vendor/autoload.php';

use \Mailjet\Resources;

$kirby->set('route',
array(
'pattern' => 'kirby-mailjet/(:any)/mjml/(:any)',
Expand Down Expand Up @@ -33,14 +37,34 @@

// site()->user() // does not work with panel select field
if($hash == KirbyMailjet::hash()) {
$cl = null;
$cacheFile = kirby()->roots->cache() . DS . $file;
if($cache = f::read($cacheFile)) {
$cl = json_decode($cache, true);
}

if($file == 'contactslists.json') {
if($cl = KirbyMailjet::contactslists()) {
if(!$cl || f::modified($cacheFile) + c::get('plugin.mailjet.json.cache', 60*5) < time()) {
$cls = KirbyMailjet::contactslists();
if(count($cls) > 0) {
f::write($cacheFile, json_encode($cl));
$cl = $cls;
}
}
if($cl) {
$json = $cl;
$code = 200;
}
}
else if($file == 'segments.json') {
if($cl = KirbyMailjet::segments()) {
if(!$cl || f::modified($cacheFile) + c::get('plugin.mailjet.json.cache', 60*5) < time()) {
$cls = KirbyMailjet::segments();
if(count($cls) > 0) {
f::write($cacheFile, json_encode($cl));
$cl = $cls;
}
}
if($cl) {
$json = $cl;
$code = 200;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kirby-mailjet",
"description": "Kirby Mailjet makes sending emails with Mailjet simple.",
"version": "0.5.1",
"version": "0.6.0",
"author": "Bruno Meilick",
"type": "kirby-plugin",
"license": "Commercial"
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ You can set these in your `site/config/config.php`.
c::set('plugin.mailjet.logfile', kirby()->roots()->site().DS.'logs'.DS.'mailjet-'.date('Ym').'.log');
```

### plugin.mailjet.json.cache
- default: `300`
- time in seconds to cache the json files for contactslists and segments

### plugin.mailjet.json-contactslists.exclude
- default: `[]`
- array of numeric ids to exclude from the `contactslists.json`
Expand Down

0 comments on commit 246acac

Please sign in to comment.