悲观锁
- select * from Post where id= for update
乐观锁
- Entity::version
- update Post set content='...', version=version+1 where version= and id=
php lock
分布式锁
Symfony::Doctrine 集成解决方案
class EntityManagerXieLock
{
private $em = null;
private $lockFactory = null;
private $locks = [];
public function __construct(EntityManager $em, LockFactory $lockFactory)
{
$this->em = $em;
$this->lockFactory = $lockFactory;
}
public function __destruct()
{
$this->release();
}
public function find(string $entityClass, int $id)
{
$randomKey = $entityClass . $id . uniqid();
$lock = $this->lockFactory->createLock($randomKey, $ttl = 300);
$this->locks[] += $lock;
if ( ! $lock->acquire())
return null;
return $this->em->getRepo($entityClass)->find($id);
}
public function release()
{
foreach ($this->locks as $lock) {
$lock->release();
}
$this->locks = [];
}
}