Hi! Could we please enable some services and cookies to improve your experience and our website?
No, thanks.
Okay!
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
create table if not exists lock_service (`id` char(128) unique, `ttl` int(11) unsigned) engine innodb default character set utf8;
insert into lock_service (id, ttl) values ('teste', 300);
select * from lock_service;
SQL Server:
MySQL 8.0
MySQL 8.0 Sakila (ReadOnly)
MySQL 9.3.0
MariaDB 11.4
MariaDB 11.8
MariaDB 10
MariaDB 10 Sakila (ReadOnly)
SQLite 3
SQLite 3 Preloaded
PostgreSQL 10 Bookings (ReadOnly)
PostgreSQL 13
PostgreSQL 14
PostgreSQL 15
PostgreSQL 16
PostgreSQL 17 + PostGIS
PostgreSQL 17 + PostGIS WorkShop (ReadOnly)
MS SQL Server 2017
MS SQL Server 2019
MS SQL Server 2022
MS SQL Server 2022 AdventureWorks (ReadOnly)
Firebird 4.0
Firebird 4.0 (Employee)
RedDatabase 5.0
Oracle Database 19c (HR)
Oracle Database 21c
Oracle Database 23c Free
SOQOL
ClickHouse
Run SQL code
Save snippet
ER Diagram
<?php
class Lock
{
/**
* Summary of key
* @var string
*/
public $key;
/**
* Summary of ttl
* @var int
*/
public $ttl = 0;
/**
* Summary of __construct
* @param string $key
* @param int $ttl
*/
public function __construct($key, $ttl = 0)
{
$this->key = $key;
$this->ttl = $ttl;
}
}
interface LockService
{
/**
* Summary of acquire
* @param string $key
* @param int $ttl
* @return Lock|null
*/
public function acquire($key, $ttl = 0);
public function release(Lock $lock);
}
class MysqlLockService implements LockService
{
/**
* Summary of table_name
* @var string
*/
private $table_name;
/**
* @var PDO $pdo
*/
private $pdo;
public function __construct(PDO $pdo, $table_name = 'lock_service')
{
$this->table_name = $table_name;
$this->pdo = $pdo;
$this->init();
}
private function init() {
$this->pdo->setAttribute(
PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION
);
$this->pdo->exec("CREATE TABLE IF NOT EXISTS `{$this->table_name}` (`id` char(128) unique, `ttl` int(11) unsigned) engine innodb default character set utf8");
}
private function cleanup()
{
$stmt = $this->pdo->prepare("DELETE FROM `{$this->table_name}` WHERE ttl < ?");
$stmt->execute(array(time()));
}
/**
* Summary of acquire
* @param string $key
* @param int $ttl
* @return Lock|null
*/
public function acquire($key, $ttl = 300)
{
try {
$this->cleanup();
$id = hash('sha512', $key);
$stmt = $this->pdo->prepare("INSERT INTO `{$this->table_name}` (`id`, `ttl`) VALUES (?, ?)");
$stmt->execute(array($id, time() + $ttl));
return new Lock($key, $ttl);
} catch (\Exception $th) {
var_dump($th);
return null;
}
}
/**
* @param Lock $lock
* @return mixed
*/
public function release(Lock $lock)
{
$id = hash('sha512', $lock->key);
$stmt = $this->pdo->prepare("DELETE FROM `{$this->table_name}` WHERE `id` = ?");
$stmt->execute(array($id));
$this->cleanup();
}
}
try {
$lock_service = new MysqlLockService($pdo);
$lock = $lock_service->acquire("teste");
var_dump($lock);
} catch (\Exception $e) {
var_dump($e);
}
PHP version :
PHP 7.4
PHP 8.0
PHP 8.1
PHP 8.2
PHP 8.3
PHP 8.4
Run PHP Code
Save snippet