Download the PHP package laragear/attempt-once without Composer

On this page you can find all versions of the php package laragear/attempt-once. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package attempt-once

Deprecated

Use Cache::funnel() with 1 attempt instead.

Once

Latest Version on Packagist Latest stable test run Codecov coverage Maintainability Sonarcloud Status Laravel Octane Compatibility

Run and manage callbacks across multiple app instances, atomically.

Become a sponsor

Your support allows me to keep this package free, up-to-date and maintainable.

Requirements

Installation

You can install the package via Composer.

How does this work?

It uses Laravel's Rate Limiter behind the scenes to allow callbacks to run once. This way all app instances are aware of the callback execution, ensuring only runs once inside a given window of time, which is 1 minute by default.

Usage

The simplest way to use attempt_once() is to just issue a callback, which will be executed only once for 30 seconds. A key will be computed from the callback as laragear|attempt_once:{hash} based on the callback unique position in your application.

Adding a second argument after the callback will set the amount of time to "hold" the execution. You may use a DateTimeInterface instance, a DateInterval object or just an amount of seconds.

[!IMPORTANT]

When using a single callback, the hash computed takes into account the place where is called. Duplicating the code will make both callbacks hashes different, even if they do exactly the same.

While the above may suffice for most simple scenarios, you may have more granular control on the execution by calling attempt_once() with a key to identify the execution, or a string backed enum. You will receive a builder instance to configure how to run it:

Custom key

The attempt_once() method allows to identify the callback to run once with a simple string. With a constant string, you may check the execution status elsewhere in your app.

You can issue multiple strings or arrays, which is great when you need to separate executions programmatically. The resulting key will be concatenated using |. If you pass an array, the key and value will be concatenated as {key}:{value}.

If you pass an Eloquent Model as second parameter, it will be used to identify the callback as {class}:{key}, essentially making the callback unique for each model you pass.

When using an Enum as key, the class name and case name will be used as part of the key as {class}:{case}.

Time

You may change the window of time using for() with the amount of seconds. You may also use DateTimeInterface instance, or a DateInterval instance.

[!IMPORTANT]

You may change the window of time programmatically, but consider that only previous successful execution will set the time. Since subsequent executions won't run, the time won't be updated.

Callback result

When the callback runs, its result will be returned. When the callback does not run because it's rate limited, false will be returned.

Default result

Sometimes you will want to return another value rather than false if the callback is not executed. For that, use the or() method.

Checking execution

To check if the callback was executed, you may use the wasExecuted() and wasNotExecuted() methods, which is only accessible using a named key.

To check how many seconds remain to execute the callback again, use availableIn(), or readyAt() to get a Carbon instance.

Cache Store

By default, Laravel's Rate Limiter uses the application default cache store. If you want to change the cache store to use, configure the Rate Limiter in your app configuration.

Laravel Octane compatibility

There should be no problems using this package with Laravel Octane.

Security

If you discover any security-related issues, issue a Security Advisory.

License

This specific package version is licensed under the terms of the MIT License, at the time of publishing.

Laravel is a Trademark of Taylor Otwell. Copyright © 2011-2026 Laravel LLC.


All versions of attempt-once with dependencies

PHP Build Version
Package Version
Requires php Version ^8.4
laragear/meta Version ^4.0.0
laragear/meta-model Version ^2.0.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package laragear/attempt-once contains the following files

Loading the files please wait ...