Download the PHP package mpyw/laravel-cached-database-stickiness without Composer
On this page you can find all versions of the php package mpyw/laravel-cached-database-stickiness. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mpyw/laravel-cached-database-stickiness
More information about mpyw/laravel-cached-database-stickiness
Files in mpyw/laravel-cached-database-stickiness
Package laravel-cached-database-stickiness
Short Description Guarantee database stickiness over the same user's consecutive requests
License MIT
Informations about the package laravel-cached-database-stickiness
Laravel Cached Database Stickiness
Guarantee database stickiness over the same user's consecutive requests.
Requirements
- PHP:
^8.0
- Laravel:
^9.0 || ^10.0
Installing
[!IMPORTANT] The default implementation is provided by
ConnectionServiceProvider
, however, package discovery is not available. Be careful that you MUST register it inconfig/app.php
by yourself.
Then select the proper cache driver:
Driver | Is eligible? | Description |
---|---|---|
redis |
😄 | Very fast, scalable and reliable driver (Cluster mode must be disabled) |
memcached |
😄 | Alternative for Redis |
dynamodb |
😧 | It works but not so suitable for short-term caching ( ConsistentRead must be enabled) |
apc |
😧 | It works unless PHP processes are running in multiple machines or containers |
file |
😧 | It works unless PHP processes are running in multiple machines or containers |
database |
🤮 | We'll get into a chicken-or-egg situation |
array |
🤮 | Just for testing |
Features
This library provides the following features.
- Make HTTP server to take over the database sticky state from the previous user's request within the last 5 seconds.
- Make queue worker into referring to master by default.
- Make queue worker into referring to slave by implementing
ShouldAssumeFresh
on your Queueable (jobs, listeners, notifications and mailables).
Diagrams
Default
Sticky
Sticky Cached
Advanced Usage
Customize Stickiness TTL
[!NOTE] The default stickiness TTL is
5
seconds.
You can configure this value to addstickiness_ttl
directive to yourconfig/database.php
.
Customize Connection Implementation
[!TIP] You can configure Connection implementation.
- Make sure
ConnectionServiceProvider
to be removed fromconfig/app.php
.- Extend Connection with
DispatchesConnectionEvents
trait by yourself.
Customize Stickiness Source
[!TIP] You can register the
StickinessResolverInterface
implementation to change the source for stickiness determination.
Source | Middleware | |
---|---|---|
IpBasedResolver (Default) |
Remote IP address | |
AuthBasedResolver |
Authenticated User ID | Required |
[!IMPORTANT] You must add
ResolveStickinessOnResolvedConnections
middleware afterAuthenticate
when you useAuthBasedResolver
.
Customize Worker Behavior
[!TIP] You can register the
JobInitializerInterface
implementation to change workers' behavior.
General Queueable | ShouldAssumeFresh Queueable |
ShouldAssumeModified Queueable |
|
---|---|---|---|
AlwaysModifiedInitializer (Default) |
Master | Slave | Master |
AlwaysFreshInitializer |
Slave | Slave | Master |
Attention
[!CAUTION]
Don't call
Schema::defaultStringLength()
inServiceProvider::boot()
Problem
Assume that you have the following ServiceProvider
.
If you run composer install
or directly call php artisan pacakge:discover
, it will unexpectedly use caches. It will trigger errors when we execute the command in the environment unreachable to the cache repository.
Solution
Directly use Illuminate\Database\Schema\Builder
. Don't call via Illuminate\Support\Facades\Schema
Facade.
All versions of laravel-cached-database-stickiness with dependencies
ext-pdo Version *
illuminate/container Version ^9.0 || ^10.0 || ^11.0
illuminate/contracts Version ^9.0 || ^10.0 || ^11.0
illuminate/database Version ^9.0 || ^10.0 || ^11.0
illuminate/queue Version ^9.0 || ^10.0 || ^11.0
illuminate/support Version ^9.0 || ^10.0 || ^11.0