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

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Spatie Once Related Articles

Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup
Lucky Media logo

Lucky Media

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

Lucky Media
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge