定制 mouf/utils.common.lock 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mouf/utils.common.lock

最新稳定版本:v1.0.0

Composer 安装命令:

composer require mouf/utils.common.lock

包简介

A simple package that provides functions to get a lock. Typically, you want to use locks when you want to be sure that 2 actions do not happen at the same time. For instance, if you regularly schedule cron tasks, you might want to be sure the last cron task finished before running the new one. A loc

关键字:

README 文档

README

The LockManager package is a simple package that provides functions to get a lock. Typically, you want to use locks when you want to be sure that 2 actions do not happen at the same time. For instance, if you regularly schedule cron tasks, you might want to be sure the last cron task is finished before running a new one. A lock can help you do that.

The Lock class

The Lock class represents a lock. You can acquire a lock to lock the resource and release the lock to set it free. Of course, if the lock has been already acquired by another user, you won't be able to acquire it yourself. You can optionnaly wait for the lock to be freed by the other process to acquire it yourself.

Internally, locks are acquired by putting a lock on a "file" that is hidden in the temp directory. When creating a Lock instance in Mouf, you will therefore have to find a unique temp name for that file.

If your PHP script crashes or exits without explicitly releasing the lock, the lock will be automatically released, so that other processes can use the lock.

Example

A first example: trying to acquire a lock without waiting

// Let's create the lock instance
$lock = new Lock("my_lock_name");

// Try to acquire lock without waiting
try {
	$lock->acquireLock();
	// ... Do some stuff ...
	$lock->releaseLock();
} catch (LockException $e) {
	// The lock could not be acquired... Let's ignore this.
}

A second example: acquire a lock and wait if the lock is not available

// Let's create the lock instance
$lock = new Lock("my_lock_name");

// Try to acquire lock and wait if the lock is not available
$lock->acquireLock(true);
// ... Do some stuff ...
$lock->releaseLock();

Good practices

A good practice is to create the lock object via a dependency injection mechanism. This way, you can share instances of your lock accross services that require it. The Mouf framework let's you do that. Let's assume you create a "myLock" instance:

mouf instance

Your code to use the lock would look this:

// You need to create an instance "myLock" of the Lock class in Mouf first.
$lock = Mouf::getMyLock();

// Try to acquire lock without waiting
try {
	$lock->acquireLock();
	// ... Do some stuff ...
	$lock->releaseLock();
} catch (LockException $e) {
	// The lock could not be acquired... Let's ignore this.
}

统计信息

  • 总下载量: 67.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 3
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 12
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-04-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固