0
0
Laravelframework~5 mins

Storing and retrieving cache in Laravel

Choose your learning style9 modes available
Introduction

Cache helps your app remember data so it can load faster next time. It saves time by not doing the same work again.

When you want to save database query results to speed up page loading.
When you want to store user settings temporarily for quick access.
When you want to keep API response data to avoid repeated calls.
When you want to save computed values that take time to generate.
When you want to reduce server load by reusing stored data.
Syntax
Laravel
<?php
use Illuminate\Support\Facades\Cache;

// Store data in cache
Cache::put('key', 'value', $seconds);

// Retrieve data from cache
$value = Cache::get('key', 'default');

// Store data forever
Cache::forever('key', 'value');

// Remove data from cache
Cache::forget('key');

Use Cache::put to save data with a time limit in seconds or as a DateTime instance.

Use Cache::get to get data; it returns a default if the key is missing.

Examples
Stores a username for 1 hour and then retrieves it.
Laravel
<?php
Cache::put('username', 'alice', 3600); // Store 'alice' for 1 hour
$name = Cache::get('username');
Tries to get a key that does not exist, returns 'guest' as default.
Laravel
<?php
$value = Cache::get('non_existing_key', 'guest');
Saves data permanently until manually removed.
Laravel
<?php
Cache::forever('site_name', 'My Website');
$name = Cache::get('site_name');
Deletes the 'username' key and tries to get it, returning 'not found'.
Laravel
<?php
Cache::forget('username');
$name = Cache::get('username', 'not found');
Sample Program

This program shows how to store data temporarily and permanently in cache, retrieve it, and handle expiration and removal.

Laravel
<?php
use Illuminate\Support\Facades\Cache;

// Store a value in cache for 10 seconds
Cache::put('greeting', 'Hello, Laravel!', 10);

// Retrieve and print the cached value
echo "Cached greeting: " . Cache::get('greeting', 'No greeting found') . "\n";

// Wait 11 seconds to let cache expire
sleep(11);

// Try to get the expired cache
echo "After expiration: " . Cache::get('greeting', 'Cache expired') . "\n";

// Store a value forever
Cache::forever('welcome', 'Welcome to the site!');

// Retrieve and print the forever cached value
echo "Forever cached message: " . Cache::get('welcome') . "\n";

// Remove the forever cached value
Cache::forget('welcome');

// Try to get the removed cache
echo "After removal: " . Cache::get('welcome', 'No message found') . "\n";
OutputSuccess
Important Notes

Cache::put has time complexity O(1) for storing data.

Cache::get also works in O(1) time to retrieve data.

Common mistake: forgetting to set expiration time, which uses the default TTL from config.

Use cache for data that is expensive to get or compute but changes occasionally.

Summary

Cache stores data to speed up your app by avoiding repeated work.

Use Cache::put to save with expiration, Cache::get to retrieve safely with default.

Use Cache::forever for permanent storage and Cache::forget to remove data.