PHP code example of bensedev / laravel-inflight-query-lock
1. Go to this page and download the library: Download bensedev/laravel-inflight-query-lock library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bensedev / laravel-inflight-query-lock example snippets
// Each request executes this independently
$stats = Order::where('status', 'completed')
->whereBetween('created_at', [now()->subDays(30), now()])
->with('customer', 'items')
->get();
return [
// Cache store (must support locks, e.g., Redis)
'cache_store' => env('INFLIGHT_QUERY_CACHE_STORE', 'redis'),
// Prefix for cache keys
'cache_prefix' => env('INFLIGHT_QUERY_CACHE_PREFIX', 'inflight_query'),
// Maximum time to wait for query result (seconds)
'lock_timeout' => env('INFLIGHT_QUERY_LOCK_TIMEOUT', 30),
// Time between cache polls (microseconds)
'poll_interval' => env('INFLIGHT_QUERY_POLL_INTERVAL', 100000), // 100ms
// Queue for async query execution
'queue' => env('INFLIGHT_QUERY_QUEUE', 'default'),
// Queue connection
'queue_connection' => env('INFLIGHT_QUERY_QUEUE_CONNECTION', null),
// Default TTL for cached results (seconds)
'default_ttl' => env('INFLIGHT_QUERY_DEFAULT_TTL', 3600),
// Enable logging
'enable_logging' => env('INFLIGHT_QUERY_ENABLE_LOGGING', false),
];
use Bensedev\LaravelInflightQueryLock\Traits\HasInflightLock;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
use HasInflightLock;
}
// Cache for 10 minutes (600 seconds)
$orders = Order::where('status', 'completed')
->inflight(ttl: 600)
->get();
// With relationships
$orders = Order::with('customer', 'items')
->where('total', '>', 1000)
->inflight(ttl: 300)
->get();
// Complex queries
$stats = Order::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(total) as revenue')
->where('status', 'completed')
->groupBy('date')
->inflight(ttl: 3600)
->get();
// Heavy aggregation query
$dashboardStats = Order::query()
->selectRaw('
COUNT(*) as total_orders,
SUM(total) as revenue,
AVG(total) as avg_order_value
')
->where('created_at', '>=', now()->subDays(30))
->inflight(ttl: 600) // 10 minutes
->first();
// Short TTL for frequently changing data
$recentOrders = Order::latest()->inflight(ttl: 60)->limit(10)->get();
// Long TTL for stable data
$categories = Category::with('products')->inflight(ttl: 3600)->get();