Cached some model properties and remove useless eager loading (#253)

* Cached some model properties and remove useless eager loading

* Remove ray call

* Remove double loading of forms

* Add disableCache feature when needed
This commit is contained in:
Julien Nahum 2023-12-02 14:51:08 +01:00 committed by GitHub
parent 8fd85776c6
commit a18077934c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 291 additions and 95 deletions

View File

@ -34,7 +34,7 @@ class FormController extends Controller
$this->authorize('viewAny', Form::class); $this->authorize('viewAny', Form::class);
$workspaceIsPro = $workspace->is_pro; $workspaceIsPro = $workspace->is_pro;
$forms = $workspace->forms()->with(['creator','views','submissions']) $forms = $workspace->forms()
->orderByDesc('updated_at') ->orderByDesc('updated_at')
->paginate(10)->through(function (Form $form) use ($workspace, $workspaceIsPro){ ->paginate(10)->through(function (Form $form) use ($workspace, $workspaceIsPro){
@ -66,7 +66,7 @@ class FormController extends Controller
$this->authorize('viewAny', Form::class); $this->authorize('viewAny', Form::class);
$workspaceIsPro = $workspace->is_pro; $workspaceIsPro = $workspace->is_pro;
$newForms = $workspace->forms()->with(['creator','views','submissions'])->get()->map(function (Form $form) use ($workspace, $workspaceIsPro){ $newForms = $workspace->forms()->get()->map(function (Form $form) use ($workspace, $workspaceIsPro){
// Add attributes for faster loading // Add attributes for faster loading
$form->extra = (object) [ $form->extra = (object) [
'loadedWorkspace' => $workspace, 'loadedWorkspace' => $workspace,

View File

@ -25,7 +25,6 @@ class FormResource extends JsonResource
} }
$ownerData = $this->userIsFormOwner() ? [ $ownerData = $this->userIsFormOwner() ? [
'creator' => new UserResource($this->creator),
'views_count' => $this->views_count, 'views_count' => $this->views_count,
'submissions_count' => $this->submissions_count, 'submissions_count' => $this->submissions_count,
'notifies' => $this->notifies, 'notifies' => $this->notifies,

View File

@ -4,6 +4,8 @@ namespace App\Models\Forms;
use App\Events\Models\FormCreated; use App\Events\Models\FormCreated;
use App\Models\Integration\FormZapierWebhook; use App\Models\Integration\FormZapierWebhook;
use App\Models\Traits\CachableAttributes;
use App\Models\Traits\CachesAttributes;
use App\Models\User; use App\Models\User;
use App\Models\Workspace; use App\Models\Workspace;
use Database\Factories\FormFactory; use Database\Factories\FormFactory;
@ -17,8 +19,9 @@ use Stevebauman\Purify\Facades\Purify;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
class Form extends Model class Form extends Model implements CachableAttributes
{ {
use CachesAttributes;
const DARK_MODE_VALUES = ['auto', 'light', 'dark']; const DARK_MODE_VALUES = ['auto', 'light', 'dark'];
const THEMES = ['default', 'simple', 'notion']; const THEMES = ['default', 'simple', 'notion'];
const WIDTHS = ['centered', 'full']; const WIDTHS = ['centered', 'full'];
@ -126,6 +129,12 @@ class Form extends Model
'removed_properties' 'removed_properties'
]; ];
protected $cachableAttributes = [
'is_pro',
'submissions_count',
'views_count',
];
/** /**
* The event map for the model. * The event map for the model.
* *
@ -137,7 +146,9 @@ class Form extends Model
public function getIsProAttribute() public function getIsProAttribute()
{ {
return optional($this->workspace)->is_pro; return $this->remember('is_pro', 15, function(): bool {
return optional($this->workspace)->is_pro;
});
} }
public function getShareUrlAttribute() public function getShareUrlAttribute()
@ -155,19 +166,21 @@ class Form extends Model
public function getSubmissionsCountAttribute() public function getSubmissionsCountAttribute()
{ {
return $this->submissions()->count(); return $this->remember('submissions_count', 5, function(): int {
return $this->submissions()->count();
});
} }
public function getViewsCountAttribute() public function getViewsCountAttribute()
{ {
if (env('DB_CONNECTION') == 'pgsql') { return $this->remember('views_count', 5, function(): int {
if (env('DB_CONNECTION') == 'mysql') {
return (int)($this->views()->count() +
$this->statistics()->sum(DB::raw("json_extract(data, '$.views')")));
}
return $this->views()->count() + return $this->views()->count() +
$this->statistics()->sum(DB::raw("cast(data->>'views' as integer)")); $this->statistics()->sum(DB::raw("cast(data->>'views' as integer)"));
} elseif (env('DB_CONNECTION') == 'mysql') { });
return (int)($this->views()->count() +
$this->statistics()->sum(DB::raw("json_extract(data, '$.views')")));
}
return 0;
} }
public function setDescriptionAttribute($value) public function setDescriptionAttribute($value)
@ -205,6 +218,7 @@ class Form extends Model
public function getMaxNumberOfSubmissionsReachedAttribute() public function getMaxNumberOfSubmissionsReachedAttribute()
{ {
$this->disableCache('submissions_count');
return ($this->max_submissions_count && $this->max_submissions_count <= $this->submissions_count); return ($this->max_submissions_count && $this->max_submissions_count <= $this->submissions_count);
} }

View File

@ -0,0 +1,45 @@
<?php
namespace App\Models\Traits;
use Closure;
interface CachableAttributes
{
/**
* Get an item from the cache, or execute the given Closure and store the result.
*
* @param string $key
* @param int|null $ttl
* @param Closure $callback
*
* @return mixed
*/
public function remember(string $key, ?int $ttl, Closure $callback);
/**
* Get an item from the cache, or execute the given Closure and store the result forever.
*
* @param string $key
* @param \Closure $callback
*
* @return mixed
*/
public function rememberForever(string $key, Closure $callback);
/**
* Remove an item from the cache.
*
* @param string $key
*
* @return bool
*/
public function forget(string $key): bool;
/**
* Remove all items from the cache.
*
* @return bool
*/
public function flush(): bool;
}

View File

@ -0,0 +1,112 @@
<?php
namespace App\Models\Traits;
use Closure;
use Illuminate\Contracts\Cache\Factory as CacheFactoryContract;
use Illuminate\Contracts\Cache\Repository as CacheRepository;
use Illuminate\Database\Eloquent\Model;
use InvalidArgumentException;
/**
* @property string|null $attributeCachePrefix
* @property string|null $attributeCacheStore
* @property string[]|null $cachableAttributes
*
* @mixin Model
*/
trait CachesAttributes
{
/** @var array<string, mixed> */
protected $attributeCache = [];
protected $disabledCache = [];
public static function bootCachesAttributes(): void
{
static::deleting(function (Model $model): void {
/** @var Model|CachableAttributes $model */
$model->flush();
});
}
public function disableCache($key)
{
$this->disabledCache[] = $key;
return $this;
}
public function remember(string $attribute, ?int $ttl, Closure $callback)
{
if (in_array($attribute, $this->disabledCache)) {
return value($callback);
}
if ($ttl === 0 || ! $this->exists) {
if (! isset($this->attributeCache[$attribute])) {
$this->attributeCache[$attribute] = value($callback);
}
return $this->attributeCache[$attribute];
}
if ($ttl === null) {
return $this->getCacheRepository()->rememberForever($this->getCacheKey($attribute), $callback);
}
if ($ttl < 0) {
throw new InvalidArgumentException("The TTL has to be null, 0 or any positive number - you provided `{$ttl}`.");
}
return $this->getCacheRepository()->remember($this->getCacheKey($attribute), $ttl, $callback);
}
public function rememberForever(string $attribute, Closure $callback)
{
return $this->remember($attribute, null, $callback);
}
public function forget(string $attribute): bool
{
unset($this->attributeCache[$attribute]);
if (! $this->exists) {
return true;
}
return $this->getCacheRepository()->forget($this->getCacheKey($attribute));
}
public function flush(): bool
{
$result = true;
foreach ($this->cachableAttributes ?? [] as $attribute) {
$result = $this->forget($attribute) ? $result : false;
}
return $result;
}
protected function getCacheKey(string $attribute): string
{
return implode('.', [
$this->attributeCachePrefix ?? 'model_attribute_cache',
$this->getConnectionName() ?? 'connection',
$this->getTable(),
$this->getKey(),
$attribute,
$this->updated_at?->timestamp ?? '0'
]);
}
protected function getCacheRepository(): CacheRepository
{
return $this->getCacheFactory()->store($this->attributeCacheStore);
}
protected function getCacheFactory(): CacheFactoryContract
{
return app('cache');
}
}

View File

@ -59,8 +59,6 @@ class User extends Authenticatable implements JWTSubject
'photo_url', 'photo_url',
]; ];
protected $withCount = ['workspaces'];
public function ownsForm(Form $form) public function ownsForm(Form $form)
{ {
return $this->workspaces()->find($form->workspace_id) !== null; return $this->workspaces()->find($form->workspace_id) !== null;

View File

@ -4,12 +4,14 @@ namespace App\Models;
use App\Http\Requests\AnswerFormRequest; use App\Http\Requests\AnswerFormRequest;
use App\Models\Forms\Form; use App\Models\Forms\Form;
use App\Models\Traits\CachableAttributes;
use App\Models\Traits\CachesAttributes;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class Workspace extends Model class Workspace extends Model implements CachableAttributes
{ {
use HasFactory; use HasFactory, CachesAttributes;
const MAX_FILE_SIZE_FREE = 5000000; // 5 MB const MAX_FILE_SIZE_FREE = 5000000; // 5 MB
const MAX_FILE_SIZE_PRO = 50000000; // 50 MB const MAX_FILE_SIZE_PRO = 50000000; // 50 MB
@ -32,20 +34,14 @@ class Workspace extends Model
'custom_domains' => 'array', 'custom_domains' => 'array',
]; ];
public function getIsProAttribute() protected $cachableAttributes = [
{ 'is_pro',
if(is_null(config('cashier.key'))){ 'is_enterprise',
return true; // If no paid plan so TRUE for ALL 'is_risky',
} 'submissions_count',
'max_file_size',
// Make sure at least one owner is pro 'custom_domain_count'
foreach ($this->owners as $owner) { ];
if ($owner->is_subscribed) {
return true;
}
}
return false;
}
public function getMaxFileSizeAttribute() public function getMaxFileSizeAttribute()
{ {
@ -53,18 +49,20 @@ class Workspace extends Model
return self::MAX_FILE_SIZE_PRO; return self::MAX_FILE_SIZE_PRO;
} }
// Return max file size depending on subscription return $this->remember('max_file_size', 15, function(): int {
foreach ($this->owners as $owner) { // Return max file size depending on subscription
if ($owner->is_subscribed) { foreach ($this->owners as $owner) {
if ($license = $owner->activeLicense()) { if ($owner->is_subscribed) {
// In case of special License if ($license = $owner->activeLicense()) {
return $license->max_file_size; // In case of special License
return $license->max_file_size;
}
} }
return self::MAX_FILE_SIZE_PRO;
} }
return self::MAX_FILE_SIZE_PRO;
}
return self::MAX_FILE_SIZE_FREE; return self::MAX_FILE_SIZE_FREE;
});
} }
public function getCustomDomainCountLimitAttribute() public function getCustomDomainCountLimitAttribute()
@ -73,18 +71,36 @@ class Workspace extends Model
return null; return null;
} }
// Return max file size depending on subscription return $this->remember('custom_domain_count', 15, function(): int {
foreach ($this->owners as $owner) { foreach ($this->owners as $owner) {
if ($owner->is_subscribed) { if ($owner->is_subscribed) {
if ($license = $owner->activeLicense()) { if ($license = $owner->activeLicense()) {
// In case of special License // In case of special License
return $license->custom_domain_limit_count; return $license->custom_domain_limit_count;
}
} }
return self::MAX_DOMAIN_PRO;
} }
return self::MAX_DOMAIN_PRO;
return 0;
});
}
public function getIsProAttribute()
{
if(is_null(config('cashier.key'))){
return true; // If no paid plan so TRUE for ALL
} }
return 0; return $this->remember('is_pro', 15, function(): bool {
// Make sure at least one owner is pro
foreach ($this->owners as $owner) {
if ($owner->is_subscribed) {
return true;
}
}
return false;
});
} }
public function getIsEnterpriseAttribute() public function getIsEnterpriseAttribute()
@ -93,34 +109,41 @@ class Workspace extends Model
return true; // If no paid plan so TRUE for ALL return true; // If no paid plan so TRUE for ALL
} }
foreach ($this->owners as $owner) { return $this->remember('is_enterprise', 15, function(): bool {
if ($owner->has_enterprise_subscription) { // Make sure at least one owner is pro
return true; foreach ($this->owners as $owner) {
if ($owner->has_enterprise_subscription) {
return true;
}
} }
} return false;
return false; });
} }
public function getIsRiskyAttribute() public function getIsRiskyAttribute()
{ {
// A workspace is risky if all of his users are risky return $this->remember('is_risky', 15, function(): bool {
foreach ($this->owners as $owner) { // A workspace is risky if all of his users are risky
if (!$owner->is_risky) { foreach ($this->owners as $owner) {
return false; if (!$owner->is_risky) {
return false;
}
} }
}
return true; return true;
});
} }
public function getSubmissionsCountAttribute() public function getSubmissionsCountAttribute()
{ {
$total = 0; return $this->remember('submissions_count', 15, function(): int {
foreach ($this->forms as $form) { $total = 0;
$total += $form->submissions_count; foreach ($this->forms as $form) {
} $total += $form->submissions_count;
}
return $total; return $total;
});
} }
/** /**

View File

@ -30,7 +30,7 @@ class WorkspacePolicy
*/ */
public function view(User $user, Workspace $workspace) public function view(User $user, Workspace $workspace)
{ {
return $user->workspaces()->find($workspace->id)!==null; return $workspace->users()->find($user->id)!==null;
} }
/** /**

View File

@ -10,34 +10,37 @@
> >
{{ appName }}</span> {{ appName }}</span>
</router-link> </router-link>
<workspace-dropdown class="ml-6"/> <workspace-dropdown class="ml-6" />
</div> </div>
<div v-if="showAuth" class="hidden md:block ml-auto relative"> <div v-if="showAuth" class="hidden md:block ml-auto relative">
<router-link :to="{name:'templates'}" v-if="$route.name !== 'templates'" <router-link v-if="$route.name !== 'templates'" :to="{name:'templates'}"
class="text-sm text-gray-600 dark:text-white hover:text-gray-800 cursor-pointer mt-1 mr-8"> class="text-sm text-gray-600 dark:text-white hover:text-gray-800 cursor-pointer mt-1 mr-8"
>
Templates Templates
</router-link> </router-link>
<router-link :to="{name:'aiformbuilder'}" v-if="$route.name !== 'aiformbuilder'" <router-link v-if="$route.name !== 'aiformbuilder'" :to="{name:'aiformbuilder'}"
class="text-sm text-gray-600 dark:text-white hidden lg:inline hover:text-gray-800 cursor-pointer mt-1 mr-8"> class="text-sm text-gray-600 dark:text-white hidden lg:inline hover:text-gray-800 cursor-pointer mt-1 mr-8"
>
AI Form Builder AI Form Builder
</router-link> </router-link>
<router-link :to="{name:'pricing'}" v-if="paidPlansEnabled && (user===null || (user && workspace && !workspace.is_pro)) && $route.name !== 'pricing'" <router-link v-if="paidPlansEnabled && (user===null || (user && workspace && !workspace.is_pro)) && $route.name !== 'pricing'" :to="{name:'pricing'}"
class="text-sm text-gray-600 dark:text-white hover:text-gray-800 cursor-pointer mt-1 mr-8"> class="text-sm text-gray-600 dark:text-white hover:text-gray-800 cursor-pointer mt-1 mr-8"
>
<span v-if="user">Upgrade</span> <span v-if="user">Upgrade</span>
<span v-else>Pricing</span> <span v-else>Pricing</span>
</router-link> </router-link>
<a href="#" class="text-sm text-gray-600 dark:text-white hover:text-gray-800 cursor-pointer mt-1" <a v-if="hasCrisp" href="#"
@click.prevent="openCrisp" v-if="hasCrisp" class="text-sm text-gray-600 dark:text-white hover:text-gray-800 cursor-pointer mt-1" @click.prevent="openCrisp"
> >
Help Help
</a> </a>
<a :href="helpUrl" class="text-sm text-gray-600 dark:text-white hover:text-gray-800 cursor-pointer mt-1" <a v-else :href="helpUrl"
target="_blank" v-else class="text-sm text-gray-600 dark:text-white hover:text-gray-800 cursor-pointer mt-1" target="_blank"
> >
Help Help
</a> </a>
</div> </div>
<div v-if="showAuth" class="hidden md:block pl-5 border-gray-300 border-r h-5"></div> <div v-if="showAuth" class="hidden md:block pl-5 border-gray-300 border-r h-5" />
<div v-if="showAuth" class="block"> <div v-if="showAuth" class="block">
<div class="flex items-center"> <div class="flex items-center">
<div class="ml-3 mr-4 relative"> <div class="ml-3 mr-4 relative">
@ -107,7 +110,7 @@
{{ $t('logout') }} {{ $t('logout') }}
</a> </a>
</dropdown> </dropdown>
<div class="flex gap-2" v-else> <div v-else class="flex gap-2">
<router-link v-if="$route.name !== 'login'" :to="{ name: 'login' }" <router-link v-if="$route.name !== 'login'" :to="{ name: 'login' }"
class="text-gray-600 dark:text-white hover:text-gray-800 dark:hover:text-white px-0 sm:px-3 py-2 rounded-md text-sm" class="text-gray-600 dark:text-white hover:text-gray-800 dark:hover:text-white px-0 sm:px-3 py-2 rounded-md text-sm"
active-class="text-gray-800 dark:text-white" active-class="text-gray-800 dark:text-white"
@ -115,10 +118,9 @@
{{ $t('login') }} {{ $t('login') }}
</router-link> </router-link>
<v-button size="small" :to="{ name: 'forms.create.guest' }" color="outline-blue" v-track.nav_create_form_click :arrow="true"> <v-button v-track.nav_create_form_click size="small" :to="{ name: 'forms.create.guest' }" color="outline-blue" :arrow="true">
Create a form Create a form
</v-button> </v-button>
</div> </div>
</div> </div>
</div> </div>
@ -130,7 +132,7 @@
</template> </template>
<script> <script>
import {mapGetters} from 'vuex' import { mapGetters, mapState } from 'vuex'
import Dropdown from './common/Dropdown.vue' import Dropdown from './common/Dropdown.vue'
import WorkspaceDropdown from './WorkspaceDropdown.vue' import WorkspaceDropdown from './WorkspaceDropdown.vue'
@ -141,13 +143,13 @@ export default {
}, },
data: () => ({ data: () => ({
appName: window.config.appName, appName: window.config.appName
}), }),
computed: { computed: {
githubUrl: () => window.config.links.github_url, githubUrl: () => window.config.links.github_url,
helpUrl: () => window.config.links.help_url, helpUrl: () => window.config.links.help_url,
form() { form () {
if (this.$route.name && this.$route.name.startsWith('forms.show_public')) { if (this.$route.name && this.$route.name.startsWith('forms.show_public')) {
return this.$store.getters['open/forms/getBySlug'](this.$route.params.slug) return this.$store.getters['open/forms/getBySlug'](this.$route.params.slug)
} }
@ -156,13 +158,13 @@ export default {
workspace () { workspace () {
return this.$store.getters['open/workspaces/getCurrent']() return this.$store.getters['open/workspaces/getCurrent']()
}, },
paidPlansEnabled() { paidPlansEnabled () {
return window.config.paid_plans_enabled return window.config.paid_plans_enabled
}, },
showAuth() { showAuth () {
return this.$route.name && !this.$route.name.startsWith('forms.show_public') return this.$route.name && !this.$route.name.startsWith('forms.show_public')
}, },
hasNavbar() { hasNavbar () {
if (this.isIframe) return false if (this.isIframe) return false
if (this.$route.name && this.$route.name.startsWith('forms.show_public')) { if (this.$route.name && this.$route.name.startsWith('forms.show_public')) {
@ -177,22 +179,25 @@ export default {
} }
return !this.$root.navbarHidden return !this.$root.navbarHidden
}, },
isIframe() { isIframe () {
return window.location !== window.parent.location || window.frameElement return window.location !== window.parent.location || window.frameElement
}, },
...mapGetters({ ...mapGetters({
user: 'auth/user' user: 'auth/user'
}), }),
userOnboarded() { ...mapState({
return this.user && this.user.workspaces_count > 0 workspacesLoading: state => state['open/workspaces'].loading
}),
userOnboarded () {
return this.user && (this.workspacesLoading || this.workspace)
}, },
hasCrisp() { hasCrisp () {
return window.config.crisp_website_id return window.config.crisp_website_id
}, }
}, },
methods: { methods: {
async logout() { async logout () {
// Log out the user. // Log out the user.
await this.$store.dispatch('auth/logout') await this.$store.dispatch('auth/logout')
@ -201,7 +206,7 @@ export default {
this.$store.dispatch('open/forms/resetState') this.$store.dispatch('open/forms/resetState')
// Redirect to login. // Redirect to login.
this.$router.push({name: 'login'}) this.$router.push({ name: 'login' })
}, },
openCrisp () { openCrisp () {
window.$crisp.push(['do', 'chat:show']) window.$crisp.push(['do', 'chat:show'])