mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Add a basic singleton to save on queries.
This commit is contained in:
@@ -29,9 +29,9 @@ use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\UserGroup;
|
||||
use FireflyIII\Support\Facades\Preferences;
|
||||
use FireflyIII\Support\Singleton\PreferencesSingleton;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use NumberFormatter;
|
||||
|
||||
/**
|
||||
@@ -116,11 +116,20 @@ class Amount
|
||||
|
||||
public function convertToPrimary(?User $user = null): bool
|
||||
{
|
||||
$instance = PreferencesSingleton::getInstance();
|
||||
$pref = $instance->getPreference('convert_to_primary');
|
||||
if (null === $pref) {
|
||||
if (!$user instanceof User) {
|
||||
return true === Preferences::get('convert_to_primary', false)->data && true === config('cer.enabled');
|
||||
$res = true === Preferences::get('convert_to_primary', false)->data && true === config('cer.enabled');
|
||||
$instance->setPreference('convert_to_primary', $res);
|
||||
return $res;
|
||||
}
|
||||
|
||||
return true === Preferences::getForUser($user, 'convert_to_primary', false)->data && true === config('cer.enabled');
|
||||
$res = true === Preferences::getForUser($user, 'convert_to_primary', false)->data && true === config('cer.enabled');
|
||||
$instance->setPreference('convert_to_primary', $res);
|
||||
return $res;
|
||||
}
|
||||
return $pref;
|
||||
}
|
||||
|
||||
public function getPrimaryCurrency(): TransactionCurrency
|
||||
|
@@ -25,6 +25,7 @@ namespace FireflyIII\Support;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Preference;
|
||||
use FireflyIII\Support\Singleton\PreferencesSingleton;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Contracts\Encryption\DecryptException;
|
||||
use Illuminate\Contracts\Encryption\EncryptException;
|
||||
@@ -283,6 +284,12 @@ class Preferences
|
||||
*/
|
||||
public function lastActivity(): string
|
||||
{
|
||||
$instance = PreferencesSingleton::getInstance();
|
||||
$pref = $instance->getPreference('last_activity');
|
||||
if(null !== $pref) {
|
||||
// Log::debug(sprintf('Found last activity in singleton: %s', $pref));
|
||||
return $pref;
|
||||
}
|
||||
$lastActivity = microtime();
|
||||
$preference = $this->get('lastActivity', microtime());
|
||||
|
||||
@@ -292,13 +299,16 @@ class Preferences
|
||||
if (is_array($lastActivity)) {
|
||||
$lastActivity = implode(',', $lastActivity);
|
||||
}
|
||||
|
||||
return hash('sha256', (string) $lastActivity);
|
||||
$setting = hash('sha256', (string) $lastActivity);
|
||||
$instance->setPreference('last_activity', $setting);
|
||||
return $setting;
|
||||
}
|
||||
|
||||
public function mark(): void
|
||||
{
|
||||
$this->set('lastActivity', microtime());
|
||||
$instance = PreferencesSingleton::getInstance();
|
||||
$instance->setPreference('last_activity', microtime());
|
||||
Session::forget('first');
|
||||
}
|
||||
|
||||
|
35
app/Support/Singleton/PreferencesSingleton.php
Normal file
35
app/Support/Singleton/PreferencesSingleton.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace FireflyIII\Support\Singleton;
|
||||
|
||||
class PreferencesSingleton
|
||||
{
|
||||
private static ?PreferencesSingleton $instance = null;
|
||||
|
||||
private array $preferences = [];
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
// Private constructor to prevent direct instantiation.
|
||||
}
|
||||
|
||||
public static function getInstance(): PreferencesSingleton
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new PreferencesSingleton();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function setPreference(string $key, mixed $value): void
|
||||
{
|
||||
$this->preferences[$key] = $value;
|
||||
}
|
||||
|
||||
public function getPreference(string $key): mixed
|
||||
{
|
||||
return $this->preferences[$key] ?? null;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user