<?php
class RedisLock{
protected $redis;
protected $config = [
'host'=>'127.0.0.1',
'port'=>'6379',
'timeout'=>'0',
'auth'=>'',
'reserved'=>null,
'retry_interval'=>100,
];
public function __construct($config = []){
$this->config = !empty($config)?array_merge($this->config,$config):$this->config;
$this->redis = $this->connect($this->config);
}
public function lock($name,$expire=5){
$is_lock = $this->redis->setnx($name,time()+$expire);//判断是否已经设置,未设置的话同时设置,返回1,如果已经设置返回0
if(!empty($is_lock)){
$lock_time = $this->redis->get($name);
if(time()>$lock_time){
$this->unlock($name);
$is_lock = $
Redis锁防止并发
最新推荐文章于 2024-05-16 15:53:15 发布