Skip to content

Commit

Permalink
Moved cache drivers outside core to extensions (plugins)
Browse files Browse the repository at this point in the history
  • Loading branch information
the-djmaze committed Mar 22, 2024
1 parent d5690fc commit 4fc0464
Show file tree
Hide file tree
Showing 267 changed files with 39 additions and 64 deletions.
20 changes: 10 additions & 10 deletions examples/docker/Docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ To setup MySQL as the DB, in Admin Panel, click `Contacts`, check `Enable contac

Click the `Test` button. If it turns green, MySQL is ready to be used for contacts.

To setup Redis for caching, in Admin Panel, click `Config`, update the following configuration options:
To setup Redis for caching, in Admin Panel, click `Extensions`, update the following configuration options:

- `cache > enable`: yes
- `cache > fast_cache_driver`: `redis`
- `labs > fast_cache_redis_host`: `redis`
- `labs > fast_cache_redis_port`: `6379`
- `Cache Redis`: `install`
- `Cache Redis`: `enable`
- `Cache Redis > host`: `redis`
- `Cache Redis > port`: `6379`

Redis caching is now enabled.

Expand Down Expand Up @@ -83,12 +83,12 @@ To use PostgreSQL as the DB, in Admin Panel, click `Contacts`, check `Enable con

Click the `Test` button. If it turns green, PostgreSQL is ready to be used for contacts.

To setup Redis for caching, in Admin Panel, click `Config`, update the following configuration options:
To setup Redis for caching, in Admin Panel, click `Extensions`, update the following configuration options:

- `cache > enable`: yes
- `cache > fast_cache_driver`: `redis`
- `labs > fast_cache_redis_host`: `redis`
- `labs > fast_cache_redis_port`: `6379`
- `Cache Redis`: `install`
- `Cache Redis`: `enable`
- `Cache Redis > host`: `redis`
- `Cache Redis > port`: `6379`

Redis caching is now enabled.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ class APCU implements \MailSo\Cache\DriverInterface
{
private string $sKeyPrefix;

function __construct(string $sKeyPrefix = '')
public function setPrefix(string $sKeyPrefix) : void
{
$sKeyPrefix = \rtrim(\trim($sKeyPrefix), '\\/');
$this->sKeyPrefix = empty($sKeyPrefix)
? $sKeyPrefix
: \preg_replace('/[^a-zA-Z0-9_]/', '_', \rtrim(\trim($sKeyPrefix), '\\/')).'/';
: \preg_replace('/[^a-zA-Z0-9_]/', '_', $sKeyPrefix).'/';
}

public function Set(string $sKey, string $sValue) : bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,22 @@ class Memcache implements \MailSo\Cache\DriverInterface

private string $sKeyPrefix;

function __construct(string $sHost = '127.0.0.1', int $iPort = 11211, int $iExpire = 43200, string $sKeyPrefix = '')
function __construct(string $sHost = '127.0.0.1', int $iPort = 11211, int $iExpire = 43200)
{
$this->iExpire = 0 < $iExpire ? $iExpire : 43200;

$this->oMem = \class_exists('Memcache',false) ? new \Memcache : new \Memcached;
if (!$this->oMem->addServer($sHost, \strpos($sHost, ':/') ? 0 : $iPort)) {
$this->oMem = null;
}
}

public function setPrefix(string $sKeyPrefix) : void
{
$sKeyPrefix = \rtrim(\trim($sKeyPrefix), '\\/');
$this->sKeyPrefix = empty($sKeyPrefix)
? $sKeyPrefix
: \preg_replace('/[^a-zA-Z0-9_]/', '_', \rtrim(\trim($this->sKeyPrefix), '\\/')) . '/';
: \preg_replace('/[^a-zA-Z0-9_]/', '_', $sKeyPrefix).'/';
}

public function Set(string $sKey, string $sValue) : bool
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Redis implements \MailSo\Cache\DriverInterface

private string $sKeyPrefix;

function __construct(string $sHost = '127.0.0.1', int $iPort = 6379, int $iExpire = 43200, string $sKeyPrefix = '')
function __construct(string $sHost = '127.0.0.1', int $iPort = 6379, int $iExpire = 43200)
{
$this->iExpire = 0 < $iExpire ? $iExpire : 43200;

Expand All @@ -51,10 +51,14 @@ function __construct(string $sHost = '127.0.0.1', int $iPort = 6379, int $iExpir
$this->oRedis = null;
unset($oExc);
}
}

public function setPrefix(string $sKeyPrefix) : void
{
$sKeyPrefix = \rtrim(\trim($sKeyPrefix), '\\/');
$this->sKeyPrefix = empty($sKeyPrefix)
? $sKeyPrefix
: \preg_replace('/[^a-zA-Z0-9_]/', '_', rtrim(trim($sKeyPrefix), '\\/')) . '/';
: \preg_replace('/[^a-zA-Z0-9_]/', '_', $sKeyPrefix).'/';
}

public function Set(string $sKey, string $sValue) : bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
interface DriverInterface
{
public function setPrefix(string $sKeyPrefix) : void;

public function Set(string $sKey, string $sValue) : bool;

public function Exists(string $sKey) : bool;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ class File implements \MailSo\Cache\DriverInterface

private string $sKeyPrefix = '';

function __construct(string $sCacheFolder, string $sKeyPrefix = '')
function __construct(string $sCacheFolder)
{
$this->sCacheFolder = \rtrim(\trim($sCacheFolder), '\\/').'/';

// http://www.brynosaurus.com/cachedir/
$tag = $this->sCacheFolder . 'CACHEDIR.TAG';
\is_file($tag) || \file_put_contents($tag, 'Signature: 8a477f597d28d172789f06886806bc55');
}

public function setPrefix(string $sKeyPrefix) : void
{
if (!empty($sKeyPrefix)) {
$sKeyPrefix = \str_pad(\preg_replace('/[^a-zA-Z0-9_]/', '_',
\rtrim(\trim($sKeyPrefix), '\\/')), 5, '_');

$this->sKeyPrefix = '__/'.
\substr($sKeyPrefix, 0, 2).'/'.\substr($sKeyPrefix, 2, 2).'/'.
$sKeyPrefix.'/';
Expand Down
49 changes: 8 additions & 41 deletions snappymail/v/0.0.0/app/libraries/RainLoop/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,49 +479,16 @@ public function Cacher(?Model\Account $oAccount = null, bool $bForceFile = false
if (!isset($this->aCachers[$sIndexKey])) {
$this->aCachers[$sIndexKey] = new \MailSo\Cache\CacheClient();

$oDriver = null;
$sDriver = \strtoupper(\trim($this->oConfig->Get('cache', 'fast_cache_driver', 'files')));

switch (true) {
default:
case $bForceFile:
$oDriver = new \MailSo\Cache\Drivers\File(
\trim($this->oConfig->Get('cache', 'path', '')) ?: APP_PRIVATE_DATA . 'cache',
$sKey
);
break;

case ('APCU' === $sDriver) &&
\MailSo\Base\Utils::FunctionsCallable(array(
'apcu_store', 'apcu_fetch', 'apcu_delete', 'apcu_clear_cache')):

$oDriver = new \MailSo\Cache\Drivers\APCU($sKey);
break;

case ('MEMCACHE' === $sDriver || 'MEMCACHED' === $sDriver) &&
(\class_exists('Memcache',false) || \class_exists('Memcached',false)):
$oDriver = new \MailSo\Cache\Drivers\Memcache(
$this->oConfig->Get('labs', 'fast_cache_memcache_host', '127.0.0.1'),
(int) $this->oConfig->Get('labs', 'fast_cache_memcache_port', 11211),
43200,
$sKey
);
break;

case 'REDIS' === $sDriver && \class_exists('Predis\Client'):
$oDriver = new \MailSo\Cache\Drivers\Redis(
$this->oConfig->Get('labs', 'fast_cache_redis_host', '127.0.0.1'),
(int) $this->oConfig->Get('labs', 'fast_cache_redis_port', 6379),
43200,
$sKey
);
break;
}

if ($oDriver) {
$this->aCachers[$sIndexKey]->SetDriver($oDriver);
$oDriver = $bForceFile ? null : $this->fabrica('cache');
if (!($oDriver instanceof \MailSo\Cache\DriverInterface)) {
$oDriver = new \MailSo\Cache\Drivers\File(
\trim($this->oConfig->Get('cache', 'path', '')) ?: APP_PRIVATE_DATA . 'cache'
);
}
// $sDriver = \strtoupper(\trim($this->oConfig->Get('cache', 'fast_cache_driver', 'files')));
$oDriver->setPrefix($sKey);

$this->aCachers[$sIndexKey]->SetDriver($oDriver);
$this->aCachers[$sIndexKey]->SetCacheIndex($this->oConfig->Get('cache', 'fast_cache_index', ''));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ protected function defaultValues() : array

'index' => array('v1', 'Additional caching key. If changed, cache is purged'),

'fast_cache_driver' => array('files', 'Can be: files, APCU, memcache, redis (beta)'),
'fast_cache_index' => array('v1', 'Additional caching key. If changed, fast cache is purged'),

'http' => array(true, 'Browser-level cache. If enabled, caching is maintainted without using files'),
Expand Down Expand Up @@ -431,10 +430,6 @@ protected function defaultValues() : array
'custom_login_link' => array(''),
'custom_logout_link' => array(''),
'http_client_ip_check_proxy' => array(false),
'fast_cache_memcache_host' => array('127.0.0.1'),
'fast_cache_memcache_port' => array(11211),
'fast_cache_redis_host' => array('127.0.0.1'),
'fast_cache_redis_port' => array(6379),
'use_local_proxy_for_external_images' => array(true),
'image_exif_auto_rotate' => array(false),
'cookie_default_path' => array(''),
Expand Down

0 comments on commit 4fc0464

Please sign in to comment.