php
49 lines · 7 steps
Caching per-tenant settings in Laravel
A service that loads a tenant's settings once, caches them for an hour, and reads them back cheaply.
Explained by
highlit
1<?php
2
3namespace App\Services;
4
5use App\Models\Tenant;
6use Illuminate\Contracts\Cache\Repository as Cache;
7use Illuminate\Support\Facades\DB;
8
9class TenantSettings
10{
11 protected ?Tenant $tenant = null;
12
13 protected array $settings = [];
14
15 public function __construct(protected Cache $cache)
16 {
17 }
18
19 public function warm(Tenant $tenant): void
20 {
21 $this->tenant = $tenant;
22
23 $this->settings = $this->cache->remember(
24 "tenant:{$tenant->id}:settings",
25 now()->addHour(),
26 fn () => DB::table('tenant_settings')
27 ->where('tenant_id', $tenant->id)
28 ->pluck('value', 'key')
29 ->all(),
30 );
31 }
32
33 public function get(string $key, mixed $default = null): mixed
34 {
35 return $this->settings[$key] ?? $default;
36 }
37
38 public function enabled(string $feature): bool
39 {
40 return filter_var($this->get("feature.{$feature}"), FILTER_VALIDATE_BOOL);
41 }
42
43 public function flush(): void
44 {
45 if ($this->tenant) {
46 $this->cache->forget("tenant:{$this->tenant->id}:settings");
47 }
48 }
49}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Cache remember collapses a check-then-fetch into one call while keeping the expensive query inside a closure that only runs on a miss.
- 2Namespacing cache keys by tenant id keeps every tenant's data isolated and individually invalidatable.
- 3Loading settings once into an in-memory array makes repeated lookups free for the rest of the request.
Related explainers
typescript
import { registerLocaleData } from '@angular/common'; import localeFr from '@angular/common/locales/fr'; import localeFrExtra from '@angular/common/locales/extra/fr'; import localeDe from '@angular/common/locales/de';
Locale-aware bootstrapping in Angular
i18n
localization
dependency-injection
Intermediate
8 steps
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/caching-per-tenant-settings-in-laravel-explained-php-3b04/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.