Skip to the main content
{}
Back to all articles

Technical article

Laravel caching on shared hosting without Redis

Use file, database, browser and CDN caching deliberately on shared hosting, with explicit keys, TTLs and invalidation.

By Codegenie2 minute read
  • Performance
  • Laravel
  • caching
  • shared
  • hosting
  • Redis
Connected cloud servers and website components with data flows.
In this article

Laravel can cache application data without Redis on shared hosting using available file or database storage. Choose a key, lifetime and invalidation rule per dataset. Configuration, route and view caches accelerate different work and do not replace data-cache policy.

Choose storage that actually exists

File cache needs private writable storage and suits one application server. Database cache needs its table and adds query load. Check permissions, space, backup policy and post-release behaviour. Do not assume separate servers share a file cache. Laravel does not support cache tags for file and database stores; use explicit keys.

Illustrative store choice for one server.envdotenv
CACHE_STORE=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file

CACHE_STORE controls application cache. Queue and session settings are separate decisions; do not change them as a side effect of optimisation. Review existing configuration and build configuration cache only with the correct environment values.

Illustrative service list with explicit invalidation

The illustrative Service model assumes id, title, slug, locale, position and is_public. Adapt the query and public fields to your schema.

Cache only public fields as arraysapp/Http/Controllers/ServiceIndexController.phpphp
use Illuminate\Support\Facades\Cache;

$locale = app()->getLocale();
$services = Cache::remember(
    'services.public.v1.'.$locale,
    now()->addMinutes(15),
    fn () => Service::query()
        ->where('is_public', true)
        ->where('locale', $locale)
        ->orderBy('position')
        ->get(['id', 'title', 'slug'])
        ->toArray(),
);

// After a successful content update and transaction commit:
// Cache::forget('services.public.v1.'.$changedLocale);

The key includes language because the example returns translated fields. Add other dimensions only when they affect results. Forget the affected language key after a successful content change, after commit. A fifteen-minute TTL bounds missed invalidation but does not replace it. Also test that changes to non-public fields cannot leak.

Laravel 13 documents an explicit allowlist for cached object deserialisation. This example therefore uses public arrays instead of full Eloquent objects. Review existing cache format during upgrades and clear only relevant keys when format changes.

A cache failure is a failure path too

Test cold cache, warm cache, expiry and content changes. Simulate unwritable storage in a test environment. Cache::remember does not guarantee automatic fallback when storage fails; design one explicitly or return a controlled error. Measure reduced database work and response time before considering more infrastructure.

Example verification: PHP fragments were syntax-checked with PHP 8.5.11. Laravel execution and hosting behaviour were not tested; use a suitable test application and configuration.

Last updated:

Need technical context for your own project?

Codegenie helps with Laravel applications, existing codebases, shared-hosting deployments and technical improvements.