Reworked query caching for L5

This commit is contained in:
Hillel Coren 2015-04-08 16:19:17 +03:00
parent bb27ca7178
commit 4750bacd71
9 changed files with 163 additions and 269 deletions

View File

@ -11,6 +11,7 @@ use Utils;
use Validator;
use View;
use stdClass;
use Cache;
use App\Models\User;
use App\Models\Activity;
@ -129,30 +130,16 @@ class AccountController extends BaseController
public function showSection($section = ACCOUNT_DETAILS, $subSection = false)
{
if ($section == ACCOUNT_DETAILS) {
/* Update Remember Function
$data = [
'account' => Account::with('users')->findOrFail(Auth::user()->account_id),
'countries' => Country::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'sizes' => Size::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'timezones' => Timezone::remember(DEFAULT_QUERY_CACHE)->orderBy('location')->get(),
'dateFormats' => DateFormat::remember(DEFAULT_QUERY_CACHE)->get(),
'datetimeFormats' => DatetimeFormat::remember(DEFAULT_QUERY_CACHE)->get(),
'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'languages' => Language::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'showUser' => Auth::user()->id === Auth::user()->account->users()->first()->id,
];
*/
$data = [
'account' => Account::with('users')->findOrFail(Auth::user()->account_id),
'countries' => Country::orderBy('name')->get(),
'sizes' => Size::orderBy('id')->get(),
'industries' => Industry::orderBy('name')->get(),
'timezones' => Timezone::orderBy('location')->get(),
'dateFormats' => DateFormat::get(),
'datetimeFormats' => DatetimeFormat::get(),
'currencies' => Currency::orderBy('name')->get(),
'languages' => Language::orderBy('name')->get(),
'countries' => Cache::get('countries'),
'sizes' => Cache::get('sizes'),
'industries' => Cache::get('industries'),
'timezones' => Cache::get('timezones'),
'dateFormats' => Cache::get('dateFormats'),
'datetimeFormats' => Cache::get('datetimeFormats'),
'currencies' => Cache::get('currencies'),
'languages' => Cache::get('languages'),
'showUser' => Auth::user()->id === Auth::user()->account->users()->first()->id,
];
@ -210,8 +197,6 @@ class AccountController extends BaseController
$invoice->invoice_items = [$invoiceItem];
$data['invoice'] = $invoice;
//$data['invoiceDesigns'] = InvoiceDesign::remember(DEFAULT_QUERY_CACHE, 'invoice_designs_cache_'.Auth::user()->maxInvoiceDesignId())
// ->where('id', '<=', Auth::user()->maxInvoiceDesignId())->orderBy('id')->get();
$data['invoiceDesigns'] = InvoiceDesign::where('id', '<=', Auth::user()->maxInvoiceDesignId())->orderBy('id')->get();
} else if ($subSection == ACCOUNT_EMAIL_TEMPLATES) {
$data['invoiceEmail'] = $account->getEmailTemplate(ENTITY_INVOICE);
@ -391,7 +376,7 @@ class AccountController extends BaseController
$count = 0;
$hasHeaders = Input::get('header_checkbox');
$countries = Country::remember(DEFAULT_QUERY_CACHE)->get();
$countries = Cache::get('countries');
$countryMap = [];
foreach ($countries as $country) {

View File

@ -9,6 +9,7 @@ use Validator;
use Input;
use Session;
use Redirect;
use Cache;
use App\Models\Activity;
use App\Models\Client;
@ -178,23 +179,12 @@ class ClientController extends BaseController
private static function getViewModel()
{
/* Remember function no longer works
return [
'sizes' => Size::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
'paymentTerms' => PaymentTerm::remember(DEFAULT_QUERY_CACHE)->orderBy('num_days')->get(['name', 'num_days']),
'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'countries' => Country::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'customLabel1' => Auth::user()->account->custom_client_label1,
'customLabel2' => Auth::user()->account->custom_client_label2,
];
*/
return [
'sizes' => Size::orderBy('id')->get(),
'paymentTerms' => PaymentTerm::orderBy('num_days')->get(['name', 'num_days']),
'industries' => Industry::orderBy('name')->get(),
'currencies' => Currency::orderBy('name')->get(),
'countries' => Country::orderBy('name')->get(),
'sizes' => Cache::get('sizes'),
'paymentTerms' => Cache::get('paymentTerms'),
'industries' => Cache::get('industries'),
'currencies' => Cache::get('currencies'),
'countries' => Cache::get('countries'),
'customLabel1' => Auth::user()->account->custom_client_label1,
'customLabel2' => Auth::user()->account->custom_client_label2,
];

View File

@ -7,7 +7,7 @@ use Session;
use Utils;
use View;
use Validator;
zz
use App\Models\Client;
use App\Ninja\Repositories\CreditRepository;
@ -84,7 +84,6 @@ class CreditController extends BaseController
'method' => 'POST',
'url' => 'credits',
'title' => trans('texts.new_credit'),
//'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
//'invoices' => Invoice::scope()->with('client', 'invoice_status')->orderBy('invoice_number')->get(),
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), );
@ -102,7 +101,6 @@ class CreditController extends BaseController
'method' => 'PUT',
'url' => 'credits/'.$publicId,
'title' => 'Edit Credit',
//'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), );
return View::make('credit.edit', $data);

View File

@ -321,42 +321,16 @@ class InvoiceController extends BaseController
}
}
/*
return [
'account' => Auth::user()->account,
'products' => Product::scope()->orderBy('id')->get(array('product_key', 'notes', 'cost', 'qty')),
'countries' => Country::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'clients' => Client::scope()->with('contacts', 'country')->orderBy('name')->get(),
'taxRates' => TaxRate::scope()->orderBy('name')->get(),
'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'sizes' => Size::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
'paymentTerms' => PaymentTerm::remember(DEFAULT_QUERY_CACHE)->orderBy('num_days')->get(['name', 'num_days']),
'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'invoiceDesigns' => InvoiceDesign::remember(DEFAULT_QUERY_CACHE, 'invoice_designs_cache_'.Auth::user()->maxInvoiceDesignId())
->where('id', '<=', Auth::user()->maxInvoiceDesignId())->orderBy('id')->get(),
'frequencies' => array(
1 => 'Weekly',
2 => 'Two weeks',
3 => 'Four weeks',
4 => 'Monthly',
5 => 'Three months',
6 => 'Six months',
7 => 'Annually',
),
'recurringHelp' => $recurringHelp
];
*/
return [
'account' => Auth::user()->account,
'products' => Product::scope()->orderBy('id')->get(array('product_key', 'notes', 'cost', 'qty')),
'countries' => Country::orderBy('name')->get(),
'countries' => Cache::get('countries'),
'clients' => Client::scope()->with('contacts', 'country')->orderBy('name')->get(),
'taxRates' => TaxRate::scope()->orderBy('name')->get(),
'currencies' => Cache::get('currencies'),
'sizes' => Size::orderBy('id')->get(),
'paymentTerms' => PaymentTerm::orderBy('num_days')->get(['name', 'num_days']),
'industries' => Industry::orderBy('name')->get(),
'sizes' => Cache::get('sizes'),
'paymentTerms' => Cache::get('paymentTerms'),
'industries' => Cache::get('industries'),
'invoiceDesigns' => InvoiceDesign::where('id', '<=', Auth::user()->maxInvoiceDesignId())->orderBy('id')->get(),
'frequencies' => array(
1 => 'Weekly',
@ -593,7 +567,7 @@ class InvoiceController extends BaseController
'invoice' => $invoice,
'versionsJson' => json_encode($versionsJson),
'versionsSelect' => $versionsSelect,
'invoiceDesigns' => InvoiceDesign::remember(DEFAULT_QUERY_CACHE, 'invoice_designs_cache_'.Auth::user()->maxInvoiceDesignId())->where('id', '<=', Auth::user()->maxInvoiceDesignId())->orderBy('id')->get(),
'invoiceDesigns' => InvoiceDesign::where('id', '<=', Auth::user()->maxInvoiceDesignId())->orderBy('id')->get(),
];
return View::make('invoices.history', $data);

View File

@ -10,6 +10,7 @@ use Validator;
use Omnipay;
use CreditCard;
use URL;
use Cache;
use App\Models\Invoice;
use App\Models\Invitation;
@ -151,8 +152,7 @@ class PaymentController extends BaseController
'method' => 'POST',
'url' => "payments",
'title' => trans('texts.new_payment'),
//'paymentTypes' => PaymentType::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
'paymentTypes' => PaymentType::orderBy('id')->get(),
'paymentTypes' => Cache::get('paymentTypes'),
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), );
return View::make('payments.edit', $data);
@ -172,8 +172,7 @@ class PaymentController extends BaseController
'method' => 'PUT',
'url' => 'payments/'.$publicId,
'title' => trans('texts.edit_payment'),
//'paymentTypes' => PaymentType::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
'paymentTypes' => PaymentType::orderBy('id')->get(),
'paymentTypes' => Cache::get('paymentTypes'),
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), );
return View::make('payments.edit', $data);
@ -368,8 +367,7 @@ class PaymentController extends BaseController
'paymentLibrary' => $paymentLibrary,
'gateway' => $gateway,
'acceptedCreditCardTypes' => $acceptedCreditCardTypes,
//'countries' => Country::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'countries' => Country::orderBy('name')->get(),
'countries' => Cache::get('countries'),
'currencyId' => $client->currency_id,
'account' => $client->account
];
@ -418,8 +416,7 @@ class PaymentController extends BaseController
'paymentLibrary' => $paymentLibrary,
'gateway' => $gateway,
'acceptedCreditCardTypes' => $acceptedCreditCardTypes,
//'countries' => Country::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'countries' => Country::orderBy('name')->get(),
'countries' => Cache::get('countries'),
'currencyId' => 1,
'paymentTitle' => $affiliate->payment_title,
'paymentSubtitle' => $affiliate->payment_subtitle,

View File

@ -5,6 +5,7 @@ use Input;
use Redirect;
use Utils;
use View;
use Cache;
use App\Models\Account;
use App\Models\Client;
@ -139,35 +140,17 @@ class QuoteController extends BaseController
private static function getViewModel()
{
/*
return [
'entityType' => ENTITY_QUOTE,
'account' => Auth::user()->account,
'products' => Product::scope()->orderBy('id')->get(array('product_key', 'notes', 'cost', 'qty')),
'countries' => Country::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'countries' => Cache::get('countries'),
'clients' => Client::scope()->with('contacts', 'country')->orderBy('name')->get(),
'taxRates' => TaxRate::scope()->orderBy('name')->get(),
'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'sizes' => Size::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
'paymentTerms' => PaymentTerm::remember(DEFAULT_QUERY_CACHE)->orderBy('num_days')->get(['name', 'num_days']),
'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'invoiceDesigns' => InvoiceDesign::remember(DEFAULT_QUERY_CACHE, 'invoice_designs_cache_'.Auth::user()->maxInvoiceDesignId())
->where('id', '<=', Auth::user()->maxInvoiceDesignId())->orderBy('id')->get(),
'invoiceLabels' => Auth::user()->account->getInvoiceLabels()
];
*/
// TODO: Add Remember Cache
return [
'entityType' => ENTITY_QUOTE,
'account' => Auth::user()->account,
'products' => Product::scope()->orderBy('id')->get(array('product_key', 'notes', 'cost', 'qty')),
'countries' => Country::orderBy('name')->get(),
'clients' => Client::scope()->with('contacts', 'country')->orderBy('name')->get(),
'taxRates' => TaxRate::scope()->orderBy('name')->get(),
'currencies' => Currency::orderBy('name')->get(),
'sizes' => Size::orderBy('id')->get(),
'paymentTerms' => PaymentTerm::orderBy('num_days')->get(['name', 'num_days']),
'industries' => Industry::orderBy('name')->get(),
'currencies' => Cache::get('currencies'),
'sizes' => Cache::get('sizes'),
'paymentTerms' => Cache::get('paymentTerms'),
'industries' => Cache::get('industries'),
'invoiceDesigns' => InvoiceDesign::where('id', '<=', Auth::user()->maxInvoiceDesignId())->orderBy('id')->get(),
'invoiceLabels' => Auth::user()->account->getInvoiceLabels()
];

View File

@ -1,4 +1,4 @@
<?php namespace App\Http\Middleware;
<?php namespace app\Http\Middleware;
use Closure;
use Utils;
@ -9,12 +9,10 @@ use Redirect;
use Cache;
use Session;
use Event;
use App\Models\Currency;
use App\Events\UserSettingsChanged;
class StartupCheck {
class StartupCheck
{
/**
* Handle an incoming request.
*
@ -25,30 +23,45 @@ class StartupCheck {
public function handle($request, Closure $next)
{
// Ensure all request are over HTTPS in production
if (App::environment() == ENV_PRODUCTION)
{
if (!Request::secure())
{
if (App::environment() == ENV_PRODUCTION) {
if (!Request::secure()) {
return Redirect::secure(Request::getRequestUri());
}
}
// If the database doens't yet exist we'll skip the rest
if (!Utils::isNinja() && !Utils::isDatabaseSetup())
{
if (!Utils::isNinja() && !Utils::isDatabaseSetup()) {
return $next($request);
}
// Check currency data has been cached
if (!Cache::has('currencies'))
{
Cache::forever('currencies', Currency::orderBy('name')->get());
// Check data has been cached
$cachedTables = [
'currencies' => 'App\Models\Currency',
'sizes' => 'App\Models\Size',
'industries' => 'App\Models\Industry',
'timezones' => 'App\Models\Timezone',
'dateFormats' => 'App\Models\DateFormat',
'datetimeFormats' => 'App\Models\DatetimeFormat',
'languages' => 'App\Models\Language',
'paymentTerms' => 'App\Models\PaymentTerm',
'paymentTypes' => 'App\Models\PaymentType',
];
foreach ($cachedTables as $name => $class) {
if (!Cache::has($name)) {
$orderBy = 'id';
if ($name == 'paymentTerms') {
$orderBy = 'num_days';
} elseif (property_exists($class, 'name') && $name != 'paymentTypes') {
$orderBy = 'name';
}
Cache::forever($name, $class::orderBy($orderBy)->get());
}
}
// check the application is up to date and for any news feed messages
if (Auth::check())
{
if (Auth::check()) {
$count = Session::get(SESSION_COUNTER, 0);
Session::put(SESSION_COUNTER, ++$count);
@ -57,15 +70,15 @@ class StartupCheck {
if (Utils::isNinja()) {
$data = Utils::getNewsFeedResponse();
} else {
$file = @file_get_contents(NINJA_APP_URL . '/news_feed/' . Utils::getUserType() . '/' . NINJA_VERSION);
$file = @file_get_contents(NINJA_APP_URL.'/news_feed/'.Utils::getUserType().'/'.NINJA_VERSION);
$data = @json_decode($file);
}
if ($data) {
if ($data->version != NINJA_VERSION) {
$params = [
'user_version' => NINJA_VERSION,
'latest_version'=> $data->version,
'releases_link' => link_to(RELEASES_URL, 'Invoice Ninja', ['target' => '_blank'])
'latest_version' => $data->version,
'releases_link' => link_to(RELEASES_URL, 'Invoice Ninja', ['target' => '_blank']),
];
Session::put('news_feed_id', NEW_VERSION_AVAILABLE);
Session::put('news_feed_message', trans('texts.new_version_available', $params));
@ -81,51 +94,40 @@ class StartupCheck {
}
}
// Check if we're requesting to change the account's language
if (Input::has('lang'))
{
if (Input::has('lang')) {
$locale = Input::get('lang');
App::setLocale($locale);
Session::set(SESSION_LOCALE, $locale);
if (Auth::check())
{
if ($language = Language::whereLocale($locale)->first())
{
if (Auth::check()) {
if ($language = Language::whereLocale($locale)->first()) {
$account = Auth::user()->account;
$account->language_id = $language->id;
$account->save();
}
}
}
else if (Auth::check())
{
} elseif (Auth::check()) {
$locale = Session::get(SESSION_LOCALE, DEFAULT_LOCALE);
App::setLocale($locale);
}
// Make sure the account/user localization settings are in the session
if (Auth::check() && !Session::has(SESSION_TIMEZONE))
{
if (Auth::check() && !Session::has(SESSION_TIMEZONE)) {
Event::fire(new UserSettingsChanged());
}
// Check if the user is claiming a license (ie, additional invoices, white label, etc.)
$claimingLicense = Utils::startsWith($_SERVER['REQUEST_URI'], '/claim_license');
if (!$claimingLicense && Input::has('license_key') && Input::has('product_id'))
{
if (!$claimingLicense && Input::has('license_key') && Input::has('product_id')) {
$licenseKey = Input::get('license_key');
$productId = Input::get('product_id');
$data = trim(file_get_contents((Utils::isNinjaDev() ? 'http://ninja.dev' : NINJA_APP_URL) . "/claim_license?license_key={$licenseKey}&product_id={$productId}"));
$data = trim(file_get_contents((Utils::isNinjaDev() ? 'http://ninja.dev' : NINJA_APP_URL)."/claim_license?license_key={$licenseKey}&product_id={$productId}"));
if ($productId == PRODUCT_INVOICE_DESIGNS)
{
if ($data = json_decode($data))
{
foreach ($data as $item)
{
if ($productId == PRODUCT_INVOICE_DESIGNS) {
if ($data = json_decode($data)) {
foreach ($data as $item) {
$design = new InvoiceDesign();
$design->id = $item->id;
$design->name = $item->name;
@ -134,16 +136,13 @@ class StartupCheck {
}
if (!Utils::isNinjaProd()) {
Cache::forget('invoice_designs_cache_' . Auth::user()->maxInvoiceDesignId());
Cache::forget('invoice_designs_cache_'.Auth::user()->maxInvoiceDesignId());
}
Session::flash('message', trans('texts.bought_designs'));
}
}
else if ($productId == PRODUCT_WHITE_LABEL)
{
if ($data == 'valid')
{
} elseif ($productId == PRODUCT_WHITE_LABEL) {
if ($data == 'valid') {
$account = Auth::user()->account;
$account->pro_plan_paid = NINJA_DATE;
$account->save();
@ -155,5 +154,4 @@ class StartupCheck {
return $next($request);
}
}

View File

@ -193,7 +193,7 @@ class Account extends Eloquent
public function getLocale()
{
$language = Language::remember(DEFAULT_QUERY_CACHE)->where('id', '=', $this->account->language_id)->first();
$language = Language::where('id', '=', $this->account->language_id)->first();
return $language->locale;
}

View File

@ -176,37 +176,6 @@
@yield('content')
</div>
<div class="container">
<div class="footer" style="padding-top: 32px">
@if (false)
<div class="pull-right">
{!! Former::open('user/setTheme')->addClass('themeForm') !!}
<div style="display:none">
{!! Former::text('theme_id') !!}
{!! Former::text('path')->value(Request::url()) !!}
</div>
<div class="btn-group tr-action dropup">
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown">
Site Theme <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href="#" onclick="setTheme(0)">Default</a></li>
@foreach (Theme::remember(DEFAULT_QUERY_CACHE)->get() as $theme)
<li><a href="#" onclick="setTheme({{ $theme->id }})">{{ ucwords($theme->name) }}</a></li>
@endforeach
</ul>
</div>
{!! Former::close() !!}
</div>
@endif
<!--
Want something changed? We're {!! link_to('https://github.com/hillelcoren/invoice-ninja', 'open source', array('target'=>'_blank')) !!}, email us at {!! link_to('mailto:contact@invoiceninja.com', 'contact@invoiceninja.com') !!}.
-->
</div>
</div>
@if (!Auth::check() || !Auth::user()->registered)
<div class="modal fade" id="signUpModal" tabindex="-1" role="dialog" aria-labelledby="signUpModalLabel" aria-hidden="true">