PHP code example of kabiroman / ttlsemlock-client

1. Go to this page and download the library: Download kabiroman/ttlsemlock-client 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/ */

    

kabiroman / ttlsemlock-client example snippets






use TtlSemLock\TtlSemLock;

// Initialize client
$lock = new TtlSemLock('localhost', 8765);

// Acquire lock
if ($lock->acquire('user-123', 300)) {
    // Critical section - your code here
    echo "Lock acquired! Doing work...\n";
    
    // Release lock when done
    $lock->release('user-123');
}



use TtlSemLock\TtlSemLock;

// Initialize client with API key
$lock = new TtlSemLock('localhost', 8765, 'your-production-api-key');

// All operations work the same way
if ($lock->acquire('secure-resource', 300)) {
    echo "Secure lock acquired!\n";
    $lock->release('secure-resource');
}

// Multiple environments support
$prodLock = new TtlSemLock('prod.example.com', 8765, 'prod-key-12345');
$devLock = new TtlSemLock('localhost', 8765); // No auth in development



use TtlSemLock\TtlSemLock;

$lock = new TtlSemLock('localhost', 8765);

// Acquire with automatic owner and extend TTL
$owner = $lock->acquireWithOwner('process-lock', 60);
if ($owner) {
    echo "Lock acquired by: {$owner}\n";
    
    // Extend TTL if needed
    $lock->extend('process-lock', 120, $owner);
    
    // Use callback pattern for automatic cleanup
    $result = $lock->withLock('callback-lock', 30, function($data) {
        // Your critical code here
        return "processed: {$data}";
    }, 'input-data');
    
    $lock->release('process-lock');
}

// Install: composer t\Lock\LockFactory;
use TtlSemLock\TtlSemLock;
use TtlSemLock\Store\TtlSemLockStore;

// Create TtlSemLock client
$client = new TtlSemLock('localhost', 8765);

// Create Symfony Lock store adapter
$store = new TtlSemLockStore($client, 300); // 300s default TTL

// Use with Symfony Lock Factory
$factory = new LockFactory($store);
$lock = $factory->createLock('resource-name', 60);

if ($lock->acquire()) {
    // Critical section - only one process can be here
    // All Symfony Lock features work: TTL, refresh, blocking, etc.
    $lock->refresh(120); // Extend TTL
    $lock->release();
}


use TtlSemLock\TtlSemLock;

// Production with API key
$client = new TtlSemLock('prod.example.com', 8765, 'production-key-32-chars-minimum');

// Development without auth (backward compatible)
$devClient = new TtlSemLock('localhost', 8765);

// Environment-based configuration
$apiKey = getenv('TTLSEMLOCK_API_KEY') ?: null;
$client = new TtlSemLock('localhost', 8765, $apiKey);


use TtlSemLock\TtlSemLock;
use TtlSemLock\Exceptions\ConnectionException;

try {
    $client = new TtlSemLock('prod.example.com', 8765, 'wrong-key');
    $client->acquire('test-lock', 60);
} catch (ConnectionException $e) {
    if (strpos($e->getMessage(), 'authentication failed') !== false) {
        error_log('Invalid API key - check configuration');
        // Fallback to development server or handle error
    }
}

new TtlSemLock(string $host = 'localhost', int $port = 8765, int $timeout = 5)

$lock = new TtlSemLock();

// Simple acquire/release
if ($lock->acquire('my-lock', 60)) {
    // Do work
    $lock->release('my-lock');
}

// Check if lock exists
if ($lock->exists('my-lock')) {
    echo "Lock is active\n";
}

// Get server stats
$stats = $lock->stats();
echo "Active locks: {$stats['ActiveLocks']}\n";

$lock = new TtlSemLock();

$owner = $lock->acquireWithOwner('long-task', 60);
if ($owner) {
    // Start long-running task
    processLargeFile();
    
    // Need more time? Extend TTL
    $lock->extend('long-task', 120, $owner);
    
    // Continue processing
    processMoreData();
    
    $lock->release('long-task');
}

$lock = new TtlSemLock();

// Try to acquire with retries
$owner = $lock->acquireWithRetry('contested-resource', 30, 5, 200000);
if ($owner) {
    echo "Got lock after retries!\n";
    $lock->release('contested-resource');
} else {
    echo "Failed to acquire lock after retries\n";
}

$lock = new TtlSemLock();

try {
    $result = $lock->withLock('critical-section', 60, function($userId) {
        // This code runs while locked
        return updateUserProfile($userId);
    }, 12345);
    
    echo "Result: {$result}\n";
} catch (TtlSemLockException $e) {
    echo "Lock failed: {$e->getMessage()}\n";
}

use TtlSemLock\Exceptions\TtlSemLockException;
use TtlSemLock\Exceptions\ConnectionException;

try {
    $lock = new TtlSemLock('unreachable-host');
    $lock->acquire('test', 60);
} catch (ConnectionException $e) {
    echo "Connection failed: {$e->getMessage()}\n";
} catch (TtlSemLockException $e) {
    echo "Lock operation failed: {$e->getMessage()}\n";
}

// Custom connection settings
$lock = new TtlSemLock(
    host: 'semlock.example.com',
    port: 8765,
    timeout: 10  // seconds
);

// Get connection info
$info = $lock->getConnectionInfo();
print_r($info);
bash
# Run compatibility test
php vendor/ttlsemlock/client/examples/compatibility_test.php

# Expected output for PHP 8.1 + Symfony 6.4:
# ✅ All compatibility tests passed
# 📊 Performance: ~4000 ops/sec
bash
php examples/performance_test.php
bash
# Basic usage
php examples/basic_usage.php

# Advanced features
php examples/advanced_usage.php

# Performance benchmarks
php examples/performance_test.php