All Projects → latrell → Lock

latrell / Lock

Licence: other
支持Redis与Memcached的并发锁。

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to Lock

distlock
The universal component of distributed locks in golang , support redis and postgresql
Stars: ✭ 60 (+93.55%)
Mutual labels:  lock
Bluetooth-Unlock
Simple script to unlock your Linux based Computer using a Bluetooth device when nearby
Stars: ✭ 37 (+19.35%)
Mutual labels:  lock
AtomicKit
Concurrency made simple in Swift.
Stars: ✭ 88 (+183.87%)
Mutual labels:  lock
mbapipy
MercedesME platform as a Custom Component for Home Assistant.
Stars: ✭ 25 (-19.35%)
Mutual labels:  lock
futex
File-based Ruby Mutex
Stars: ✭ 14 (-54.84%)
Mutual labels:  lock
django-admin-page-lock
Page Lock for Django Admin allows developers to implement customizable locking pages.
Stars: ✭ 13 (-58.06%)
Mutual labels:  lock
ToyDB
A ToyDB (for beginner) based on MIT 6.830 and CMU 15445
Stars: ✭ 25 (-19.35%)
Mutual labels:  lock
advisory-lock
Distributed locking using PostgreSQL advisory locks (Node.js)
Stars: ✭ 45 (+45.16%)
Mutual labels:  lock
aiorwlock
Read/Write Lock - synchronization primitive for asyncio
Stars: ✭ 90 (+190.32%)
Mutual labels:  lock
safe
C++11 header only RAII guards for mutexes and locks.
Stars: ✭ 119 (+283.87%)
Mutual labels:  lock
basicLibPP
A powerful library for inline-hook,lock,compress etc,and it is useful for anti-virus software.
Stars: ✭ 15 (-51.61%)
Mutual labels:  lock
angular-lock
No description or website provided.
Stars: ✭ 16 (-48.39%)
Mutual labels:  lock
toxic-decorators
Library of Javascript decorators
Stars: ✭ 26 (-16.13%)
Mutual labels:  lock
TAOMP
《多处理器编程的艺术》一书中的示例代码实现,带有注释与单元测试
Stars: ✭ 39 (+25.81%)
Mutual labels:  lock
mutex
Mutex lock implementation
Stars: ✭ 28 (-9.68%)
Mutual labels:  lock
composer-diff
Compares composer.lock changes and generates Markdown report so you can use it in PR description.
Stars: ✭ 51 (+64.52%)
Mutual labels:  lock
slow-down
A centralized Redis-based lock to help you wait on throttled resources
Stars: ✭ 21 (-32.26%)
Mutual labels:  lock
run exclusive
⚡🔒 Wait queue for function execution 🔒 ⚡
Stars: ✭ 22 (-29.03%)
Mutual labels:  lock
parallel-dfs-dag
A parallel implementation of DFS for Directed Acyclic Graphs (https://research.nvidia.com/publication/parallel-depth-first-search-directed-acyclic-graphs)
Stars: ✭ 29 (-6.45%)
Mutual labels:  lock
scroll-padlock
🔒 CSS variables-based scrollbars locker, compatible with all reactive frameworks
Stars: ✭ 12 (-61.29%)
Mutual labels:  lock

Lock

这是一个支持 Laravel 5 的并发锁拓展包。

该模块在 Redis 与 Memcache 上实现了锁机制。

注意:集群环境下,必须使用 Redis 驱动,否则由于 Memcache 的特性,锁可能出现上锁不准确的情况。

安装

composer require latrell/lock dev-master

使用 composer update 更新包列表,或使用 composer install 安装。

找到 config/app.php 配置文件中的 providers 键,注册服务提供者。

(Laravel 5.5 以上版本可跳过该步骤)

    'providers' => [
        // ...
        Latrell\Lock\LockServiceProvider::class,
    ]

找到 config/app.php 配置文件中的 aliases 键,注册别名。

    'aliases' => [
        // ...
        'Lock' => Latrell\Lock\Facades\Lock::class,
    ]

运行 php artisan vendor:publish 命令,发布配置文件到你的项目中。

使用

闭包方式

使用闭包的方式,可由方法内自动处理异常,并自动解锁,防止死锁。

但需注意,外部变量需要使用 use 引入才可在闭包中使用。

// 防止商品超卖。
$key = 'Goods:' . $goods_id;
Lock::granule($key, function() use($goods_id) {
	$goods = Goods::find($goods_id);
	if ( $goods->stock > 0 ) {
		// ...
	}
});

普通方式

提供手动上锁与解锁方式,方便应用在复杂环境。

// 锁名称。
$key = 'Goods:' . $goods_id;

// **注意:除非特别自信,否则一定要记得捕获异常,保证解锁动作。**
try {

	// 上锁。
	Lock::acquire($key);

	// 逻辑单元。
	$goods = Goods::find($goods_id);
	if ( $goods->stock > 0 ) {
		// ...
	}
} finally {
	// 解锁。
	Lock::release($key);
}

中间件

使用中间件的方式,让两个相同指纹的请求同步执行。

找到 app/Http/Kernel.php 中的 $routeMiddleware 配置,添加中间件配置。

	protected $routeMiddleware = [
		// ...
		'synchronized' => \Latrell\Lock\Middleware\SynchronizationRequests::class,
	];
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].