Skip to content

Commit

Permalink
Adding Redis Support for cache and sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed Aug 23, 2024
1 parent 9fd512b commit 9ac2732
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 24 deletions.
1 change: 1 addition & 0 deletions app/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ protected function registerBaseServiceProviders()

$this->register(new \Leantime\Core\Providers\Logging($this));
$this->register(new \Leantime\Core\Providers\Events($this));
$this->register(new \Leantime\Core\Providers\Redis($this));

$this->register(new \Leantime\Core\Providers\Cache($this));
$this->register(new \Leantime\Core\Providers\Session($this));
Expand Down
26 changes: 26 additions & 0 deletions app/Core/DefaultConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,32 @@ class DefaultConfig
*/
public string $redisUrl = '';

/**
* @var string Redis Host
*/
public string $redisHost = '127.0.0.1';

/**
* @var string Redis Port
*/
public string $redisPort = '6379';

/**
* @var string Redis Password
*/
public string $redisPassword = '';

/**
* @var string Redis Cluster
*/
public string $redisCluster = '';

/**
* @var string Redis Prefix
*/
public string $redisPrefix = 'ltRedis';

# Security/Rate Limiting Settings ===============================================================================
/**
* @var string trusted Proxies
*/
Expand Down
24 changes: 12 additions & 12 deletions app/Core/Providers/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ public function register()
* then memcached if available,
* then fileStore
*/
$this->app->singleton(\Illuminate\Cache\CacheManager::class, function ($app) {
$this->app->singleton(\Illuminate\Cache\CacheManager::class, function () {

//installation cache is per server
$this->app['config']['cache.stores.installation'] = [
'driver' => 'file',
app('config')['cache.stores.installation'] = [
'driver' => !empty(app('config')->useRedis) && (bool)app('config')->useRedis === true ? 'redis' : 'file',
'connection' => 'default',
'path' => APP_ROOT . '/cache/installation',
];

//Instance is per company id
$instanceStore = fn () =>
$this->app['config']['cache.stores.instance'] = [
'driver' => 'file',
app('config')['cache.stores.instance'] = [
'driver' => !empty(app('config')->useRedis) && (bool)app('config')->useRedis === true ? 'redis' : 'file',
'connection' => 'default',
'path' => APP_ROOT . "/cache/" . $this->app->make(SettingsService::class)->getCompanyId(),
'path' => APP_ROOT . "/cache/" . app()->make(SettingsService::class)->getCompanyId(),
];

if ($this->app->make(IncomingRequest::class) instanceof CliRequest) {
if (empty($this->app->make(SettingsService::class)->getCompanyId())) {
if (app()->make(IncomingRequest::class) instanceof CliRequest) {
if (empty(app()->make(SettingsService::class)->getCompanyId())) {
throw new \RuntimeException('You can\'t run this CLI command until you have installed Leantime.');
}

Expand All @@ -61,16 +61,16 @@ function () use ($instanceStore) {
);
}

$cacheManager = new \Illuminate\Cache\CacheManager($app);
$cacheManager = new \Illuminate\Cache\CacheManager(app());

$cacheManager->setDefaultDriver('instance');

return $cacheManager;
});


$this->app->singleton('cache.store', fn ($app) => $app['cache']->driver());
$this->app->singleton('cache.psr6', fn ($app) => new \Symfony\Component\Cache\Adapter\Psr16Adapter($app['cache.store']));
$this->app->singleton('cache.store', fn () => app('cache')->driver());
$this->app->singleton('cache.psr6', fn () => new \Symfony\Component\Cache\Adapter\Psr16Adapter(app('cache.store')));
$this->app->singleton('memcached.connector', fn () => new MemcachedConnector());

$this->app->alias(\Illuminate\Cache\CacheManager::class, 'cache');
Expand All @@ -81,7 +81,7 @@ function () use ($instanceStore) {

public function boot() {

$currentVersion = $this->app->make(AppSettings::class)->appVersion;
$currentVersion = app()->make(AppSettings::class)->appVersion;
$cachedVersion = \Illuminate\Support\Facades\Cache::store('installation')->rememberForever('version', fn () => $currentVersion);

if ($currentVersion == $cachedVersion) {
Expand Down
87 changes: 87 additions & 0 deletions app/Core/Providers/Redis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Leantime\Core\Providers;

use Illuminate\Cache\MemcachedConnector;
use Illuminate\Contracts\Redis\Factory;
use Illuminate\Redis\RedisManager;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;
use Leantime\Core\AppSettings;
use Leantime\Core\CliRequest;
use Leantime\Core\Events;
use Leantime\Core\IncomingRequest;
use Leantime\Domain\Setting\Services\Setting as SettingsService;

class Redis extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{

/**
* @todo the following should eventually automatically turn caches into redis if available,
* then memcached if available,
* then fileStore
*/
$this->app->singleton(RedisManager::class, function ($app) {


app('config')['redis'] = [
'client' => 'predis',
'options' => [
'cluster' => app('config')->redisCluster ?? 'redis',
'prefix' => app('config')->redisPrefix ?? 'ltRedis',
],
'default' => [
'url' => app('config')->redisUrl ?? '',
'host' => app('config')->redisHost ?? '127.0.0.1',
'password' => app('config')->redisPassword ?? null,
'port' => app('config')->redisPort ?? '6379',
'database' => '0',
'prefix' => 'c:'
],
'session' => [
'url' => app('config')->redisUrl ?? '',
'host' => app('config')->redisHost ?? '127.0.0.1',
'password' => app('config')->redisPassword ?? null,
'port' => app('config')->redisPort ?? '6379',
'database' => '0',
'prefix' => 's:'
],
];

$redisManager = new RedisManager(app(), 'predis', app('config')['redis']);
return $redisManager;

});

$this->app->alias(RedisManager::class, 'redis');
$this->app->alias(RedisManager::class, Factory::class);
$this->app->bind('redis.connection', function ($app) {
return $app['redis']->connection();
});

}

public function boot() {


}

/**
* Manages the instance cache.
*
* @return void
*/
public function checkCacheVersion(): void
{


}

}
27 changes: 15 additions & 12 deletions app/Core/Providers/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class Session extends ServiceProvider
*/
public function register()
{
$this->app->singleton(\Illuminate\Encryption\Encrypter::class, function ($app) {
$configKey = $app['config']->sessionPassword;
$this->app->singleton(\Illuminate\Encryption\Encrypter::class, function () {

$configKey = app('config')->sessionPassword;

if (strlen($configKey) > 32) {
$configKey = substr($configKey, 0, 32);
Expand All @@ -30,22 +31,24 @@ public function register()
$configKey = str_pad($configKey, 32, "x", STR_PAD_BOTH);
}

$app['config']['app_key'] = $configKey;
app('config')['app_key'] = $configKey;

$encrypter = new \Illuminate\Encryption\Encrypter($app['config']['app_key'], "AES-256-CBC");
$encrypter = new \Illuminate\Encryption\Encrypter(app('config')['app_key'], "AES-256-CBC");
return $encrypter;
});

$this->app->singleton(\Illuminate\Session\SessionManager::class, function ($app) {
$this->app->singleton(\Illuminate\Session\SessionManager::class, function () {


$app['config']['session'] = array(
'driver' => "file",
'lifetime' => $app['config']->sessionExpiration,
app('config')['session'] = array(
'driver' => !empty( app('config')->useRedis) && (bool) app('config')->useRedis === true ? 'redis' : 'file',
'lifetime' => app('config')->sessionExpiration,
'connection' => !empty(app('config')->useRedis) && (bool) app('config')->useRedis === true ? 'session' : null,
'expire_on_close' => false,
'encrypt' => false,
'files' => APP_ROOT . '/cache/sessions',
'store' => "instance",
'block_store' => 'instance',
'store' => "installation",
'block_store' => 'installation',
'block_lock_seconds' => 10,
'block_wait_seconds' => 10,
'lottery' => [2, 100],
Expand All @@ -57,12 +60,12 @@ public function register()
'same_site' => "Lax",
);

$sessionManager = new \Illuminate\Session\SessionManager($app);
$sessionManager = new \Illuminate\Session\SessionManager(app());

return $sessionManager;
});

$this->app->singleton('session.store', fn($app) => $app['session']->driver());
$this->app->singleton('session.store', fn() => app('session')->driver());
$this->app->singleton(SymfonySessionDecorator::class, SymfonySessionDecorator::class);
$this->app->alias(\Illuminate\Session\SessionManager::class, 'session');

Expand Down

0 comments on commit 9ac2732

Please sign in to comment.