Code review at scale is broken. Here’s how Augment Code is fixing it.

Once

spatie/once image

Once stats

Downloads
19.3M
Stars
1,220
Open Issues
1
Forks
51

View on GitHub →

A magic memoization function

Spatie once Package Summary

The Spatie once package introduces an effective way to implement memoization in PHP applications, particularly with Laravel. By using the once function, developers can ensure that specific computations are only executed once per request, caching the result for subsequent calls within the same execution scope.

Key Features

  • Memoization Simplified: Easily cache the results of functions to optimize performance, especially useful when dealing with expensive operations like database calls or complex computations.
  • Easy to Use: The package provides a straightforward interface where you simply wrap your callable within the once function to utilize caching.
  • Context-Aware Caching: It differentiates cache based on the function and method context, ensuring that cache storage is unique to each callable’s use-case.
  • Flush and Disable Cache: Offers methods to flush the entire cache or temporarily disable caching, useful for scenarios like testing where fresh results are needed.
  • Supports Static Contexts: Works seamlessly in both non-static and static method contexts, providing flexibility in how it's integrated into your application.
  • Octane Compatibility: Beta support for Laravel Octane, ensuring that the package works correctly in long-lived applications by flushing cache post-request.

Installation

Install the package via composer:

composer require spatie/once

Usage Example

Here’s a basic usage example where the result of a random number generation is cached:

$myClass = new class() {
public function getNumber(): int {
return once(function () {
return rand(1, 10000);
});
}
};

Calling $myClass->getNumber() repeatedly within the same request will always return the same number.

Advanced Usage

  • Flushing the Cache: Clear all cached results.
    Spatie\Once\Cache::getInstance()->flush();
  • Disabling the Cache: Useful for development or tests.
    Spatie\Once\Cache::getInstance()->disable();
    Spatie\Once\Cache::getInstance()->enable();

Under the Hood

  • Utilizes PHP's Weakmap for efficient, automatic garbage collection of cache entries.
  • Employs the backtrace of function calls to uniquely identify caching scenarios.

Community and Contributions

The package is open for contributions, and the maintainers encourage feedback and community engagement through postcards and discussions on GitHub.

For a more visual explanation of how once works and its benefits, the documentation provides a video tutorial link.

Overall, Spatie's once package is an essential tool for developers looking to optimize PHP applications by reducing redundant processing and improving response times through effective function result caching.

spatie photo

We create open source, digital products and courses for the developer community


Spatie Once Related Articles

Memoization and Function Caching with this PHP Package image

Memoization and Function Caching with this PHP Package

Read article
Share Error Package for Laravel's New Exception Page image

Share Error Package for Laravel's New Exception Page

Read article
The once() Memoization Helper is Coming to Laravel 11 image

The once() Memoization Helper is Coming to Laravel 11

Read article
Laravel Collective HTML package is abandoned image

Laravel Collective HTML package is abandoned

Read article
Learn how to upload files in Laravel like a Pro image

Learn how to upload files in Laravel like a Pro

Read article
From idea, to blog, to live without leaving your IDE image

From idea, to blog, to live without leaving your IDE

Read article
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell