Skip to content

Commit

Permalink
Try to help PHPMailer#1824
Browse files Browse the repository at this point in the history
Using psr-16-simple-cache on MIMECache

$ php composer require psr/simple-cache

$ php benchmark_reduce_mime_encoding.phps
...
Size	Method1	Method2	Method3	Method4
1.8kb	0.0645	0.1205	0.0573	0.1064
5.7kb	0.0814	0.1740	0.0605	0.1509
50kb	0.0872	0.1698	0.0622	0.1473
100kb	0.1030	0.1839	0.0678	0.1496
200kb	0.1302	0.2208	0.0677	0.1567
500kb	0.2261	0.3194	0.0878	0.1730
  • Loading branch information
changyy committed Sep 7, 2019
1 parent 904ffd3 commit 4ffe76c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 15 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"require": {
"php": ">=5.5.0",
"ext-ctype": "*",
"ext-filter": "*"
"ext-filter": "*",
"psr/simple-cache": "^1.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.2",
Expand Down
49 changes: 44 additions & 5 deletions examples/benchmark_mime_cache.phps
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ require '../vendor/autoload.php';
// $ curl https://sample-videos.com/img/Sample-jpg-image-200kb.jpg > /tmp/200kb.jpg
// $ curl https://sample-videos.com/img/Sample-jpg-image-100kb.jpg > /tmp/100kb.jpg
// $ curl https://sample-videos.com/img/Sample-jpg-image-50kb.jpg > /tmp/50kb.jpg
//
$image_files = array(
'1.8kb' => 'phpmailer_mini.png',
'5.7kb' => 'images/phpmailer.png',
Expand Down Expand Up @@ -102,7 +101,7 @@ echo "Sending $receiver_count mails with only one PHPMailer instance: \n";
$mail->addReplyTo('replyto@example.com', 'First Last');

foreach ($receiver_list as $receiver) {
$mail->clearAddresses();
$mail->clearAddresses();
$mail->addAddress($receiver);
$mail->Subject = 'Hi '.$receiver.'!';
$mail->msgHTML(str_replace( '<h1>This is a test of PHPMailer.</h1>', '<h1>Hi '.$receiver.'</h1>', file_get_contents('contents.html')), __DIR__);
Expand All @@ -127,6 +126,46 @@ echo "Sending $receiver_count mails with only one PHPMailer instance: \n";
}
}

class MyCacheHelper implements Psr\SimpleCache\CacheInterface {
protected $cacheStore = [];
public function get($key, $default = null) {
if (array_key_exists($key, $this->cacheStore)) {
return $this->cacheStore[$key];
}

return $default;
}

public function set($key, $value, $ttl = null) {
$this->cacheStore[$key] = $value;
}

public function delete($key) {
usset($this->cacheStore[$key]);
}

public function clear() {
usset($this->cacheStore);
$this->cacheStore = [];
}

public function has($key) {
return array_key_exists($key, $this->cacheStore);
}

public function getMultiple($keys, $default = null) {

}

public function setMultiple($values, $ttl = null) {

}

public function deleteMultiple($keys) {

}
}

echo "Sending $receiver_count mails with creating a new PHPMailer instance and MIMECache: \n";
{
foreach ($image_files as $filesize => $image_file) {
Expand All @@ -135,7 +174,7 @@ echo "Sending $receiver_count mails with creating a new PHPMailer instance and M
for ($i=1; $i<=$test_count_per_run ; ++$i) {
//echo "\t\tRun $i: ";
$start = microtime(true);
$cacheLookupTable = [];
$cacheLookupTable = new MyCacheHelper;
foreach ($receiver_list as $receiver) {
$mail = new PHPMailer;
$mail->isSMTP();
Expand Down Expand Up @@ -182,7 +221,7 @@ echo "Sending $receiver_count mails with only one PHPMailer instance and MIMECac
for ($i=1; $i<=$test_count_per_run ; ++$i) {
//echo "\t\tRun $i: ";
$start = microtime(true);
$cacheLookupTable = [];
$cacheLookupTable = new MyCacheHelper;

$mail = new PHPMailer;
$mail->isSMTP();
Expand All @@ -196,7 +235,7 @@ echo "Sending $receiver_count mails with only one PHPMailer instance and MIMECac
$mail->addReplyTo('replyto@example.com', 'First Last');

foreach ($receiver_list as $receiver) {
$mail->clearAddresses();
$mail->clearAddresses();
$mail->addAddress($receiver);
$mail->Subject = 'Hi '.$receiver.'!';
$mail->msgHTML(str_replace( '<h1>This is a test of PHPMailer.</h1>', '<h1>Hi '.$receiver.'</h1>', file_get_contents('contents.html')), __DIR__);
Expand Down
49 changes: 47 additions & 2 deletions examples/smtp_mime_cache.phps
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,54 @@ echo $cost . " sec\n";
//
// ---
//

class MyCacheHelper implements Psr\SimpleCache\CacheInterface {
protected $cacheStore = [];
public function get($key, $default = null) {
if (array_key_exists($key, $this->cacheStore)) {
return $this->cacheStore[$key];
}

return $default;
}

public function set($key, $value, $ttl = null) {
$this->cacheStore[$key] = $value;
}

public function delete($key) {
usset($this->cacheStore[$key]);
}

public function clear() {
usset($this->cacheStore);
$this->cacheStore = [];
}

public function has($key) {
return array_key_exists($key, $this->cacheStore);
}

public function getMultiple($keys, $default = null) {

}

public function setMultiple($values, $ttl = null) {

}

public function deleteMultiple($keys) {

}
}



echo "Sending $receiver_count mails with creating a new PHPMailer instance and MIMECache: ";

$start = microtime(true);
$cacheLookupTable = [];
//$cacheLookupTable = [];
$cacheLookupTable = new MyCacheHelper;
foreach ($receiver_list as $receiver) {
//Create a new PHPMailer instance
$mail = new PHPMailer;
Expand Down Expand Up @@ -179,7 +223,8 @@ echo $cost . " sec\n";
echo "Sending $receiver_count mails with only one PHPMailer instance and MIMECache: ";

$start = microtime(true);
$cacheLookupTable = [];
//$cacheLookupTable = [];
$cacheLookupTable = new MyCacheHelper;
{
//Create a new PHPMailer instance
$mail = new PHPMailer;
Expand Down
12 changes: 5 additions & 7 deletions src/PHPMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,11 @@ class PHPMailer
/**
* Whether to send the same message to each receiver
* with small changes in mail content.
* Use a lookup table to reduce attachment encoding.
* Use a SimpleCache to reduce attachment encoding.
*
* Storage for attachment encoding.
*
* @var array
* @var Psr\SimpleCache\CacheInterface
*/
public $MIMECache = false;

Expand Down Expand Up @@ -3040,11 +3040,9 @@ protected function encodeFile($path, $encoding = self::ENCODING_BASE64)
{
try {
$file_buffer = '';
if ($this->MIMECache !== false) {
if ($this->MIMECache) {
$cache_key = $path . "\t" . $encoding;
if (array_key_exists($cache_key, $this->MIMECache)) {
$file_buffer = &$this->MIMECache[$cache_key];
} else {
if (($file_buffer = $this->MIMECache->get($cache_key)) == null) {
if (!static::isPermittedPath($path) || !file_exists($path)) {
throw new Exception($this->lang('file_open') . $path, self::STOP_CONTINUE);
}
Expand All @@ -3053,7 +3051,7 @@ protected function encodeFile($path, $encoding = self::ENCODING_BASE64)
throw new Exception($this->lang('file_open') . $path, self::STOP_CONTINUE);
}
$file_buffer = $this->encodeString($file_buffer, $encoding);
$this->MIMECache[$cache_key] = $file_buffer;
$this->MIMECache->set($cache_key, $file_buffer);
}
} else {
if (!static::isPermittedPath($path) || !file_exists($path)) {
Expand Down

0 comments on commit 4ffe76c

Please sign in to comment.