Download the PHP package aaronfrancis/reservable without Composer

On this page you can find all versions of the php package aaronfrancis/reservable. 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 reservable

Reservable

Latest Version on Packagist Tests Total Downloads PHP Version License

Eloquent model reservation/locking through Laravel's cache lock system.

This package allows you to temporarily "reserve" Eloquent models using Laravel's atomic cache locks. This is useful when you need to ensure exclusive access to a model for a period of time, such as during background processing.

Installation

Publish the migration:

Optionally publish the config:

Requirements

This package requires Laravel's database cache driver. Make sure your cache_locks table exists (created by Laravel's default cache migration).

The published migration adds generated columns to parse reservation keys into queryable columns. This allows efficient querying of reserved/unreserved models.

Supported databases: PostgreSQL, MySQL/MariaDB, SQLite

Usage

Add the Reservable trait to your model:

Reserve a model

The reserve() method returns true if the lock was acquired, or false if the model is already reserved.

Check if reserved

Release a reservation

Blocking reserve

Wait for a lock to become available instead of failing immediately:

Returns true if the lock was acquired, false if the wait time expired.

Reserve with callback

Automatically release the lock when your work is done:

The lock is released even if the callback throws an exception. Returns false if the lock couldn't be acquired.

Extend a reservation

Add more time to an existing reservation without releasing it:

Only reservations acquired by the current process can be extended.

Returns true if the reservation was extended, false if no active reservation exists or the current process does not own the lock.

Query scopes

Find unreserved models:

Find reserved models:

Note: These scopes return point-in-time results. By the time you try to reserve a model from the results, another process may have already reserved it. Use reserveFor for atomic find-and-reserve operations.

Find and reserve in one query:

The reserveFor scope filters to unreserved models, then atomically reserves each one that's returned. Models that can't be reserved (race condition) are filtered out.

Note: Because of race conditions, reserveFor()->limit(5) may return fewer than 5 results. If another process reserves a model between the query and the lock attempt, that model is excluded from the results.

Key types

Reservation keys can be strings, enums, or objects:

Multiple reservation types

A model can have multiple different reservation types simultaneously:

How it works

Reservable builds on Laravel's atomic cache locks, which use the cache_locks table to provide database-level mutual exclusion.

Lock key format

When you call $video->reserve('processing'), Reservable creates a cache lock with a specially formatted key:

For example: reservation:App\Models\Video:42:processing

Generated columns

The challenge with cache locks is that they're not inherently queryable by model. You can't efficiently ask "give me all Videos that aren't locked" because the lock table doesn't know about your models.

Reservable solves this by adding generated columns to the cache_locks table that parse the key format:

Column Extracted From Example Value
is_reservation Key contains reservation: true
model_type Second segment App\Models\Video
model_id Third segment 42
type Fourth segment processing

These columns are computed automatically by the database whenever a row is inserted or updated. The exact SQL varies by database engine (PostgreSQL uses split_part(), MySQL uses SUBSTRING_INDEX(), SQLite uses substr()).

Efficient queries

With generated columns in place, the reserved() and unreserved() scopes become simple JOIN queries:

This is much more efficient than fetching all videos and checking each lock individually in PHP.

Atomic reservations

The reserve() method uses Laravel's Lock::get() which performs an atomic database operation—either the lock is acquired or it isn't. There's no window where two processes can both think they have the lock.

The reserveFor scope combines this with query filtering: it finds unreserved models, then attempts to reserve each one, filtering out any that fail due to race conditions.

The CacheLock model

Reservable uses an Eloquent model (AaronFrancis\Reservable\Models\CacheLock) to query the cache_locks table. This model provides the reservations() relationship on your reservable models, allowing you to access active locks:

You can swap this for a custom model in the config if you need to add functionality.

Configuration

Testing

License

MIT


All versions of reservable with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
illuminate/cache Version ^11.0|^12.0
illuminate/database Version ^11.0|^12.0
illuminate/support Version ^11.0|^12.0
nesbot/carbon Version ^3.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 aaronfrancis/reservable contains the following files

Loading the files please wait ...