public function createPhoneUser($tel,$data=[])
{
//redis 锁
$redisCache = Cache::store('redis');
$time = time();
while ($time + 5 > time() && $redisCache->has($tel . '_lock')) {
// 存在锁定则等待
usleep(200000);
}
// 锁定
$redisCache->set($tel . '_lock', true,1);
$phoneUserData = $this->getUserByTel($tel);
if(empty($phoneUserData)){
$form = [
'tel' => $tel,
'name' => '',
'nickname' => '',
'open_id' => '',
'headimg' => '',
'unionid' => '',
'username' => '',
'password' => '',
'is_del' => 0,
'school_part_id' => 0,
'monetary' => 0,
'company' => '',
'vocation' => '',
'appellation' => '',
'unqid' => '',
'referrer_id' => 0,
'source_from' => '0', //注册来着H5端
'email' => '',
'create_time' => time(),
'from_type' => 5
];
$form = array_merge($form, $data);
$user_id = $this->insertUser($form);
// 解锁
$redisCache->rm( $tel. '_lock');
return $user_id > 0 ? $this->getUserByTel($tel) : false;
}
// 解锁
$redisCache->rm( $tel. '_lock');
return $phoneUserData;
}
https://github.com/he426100/think-lock/blob/master/Lock.php
<?php
namespace org;
use think\Cache;
use think\Config;
/**
* 以Cache为基础实现的锁
*/
class Lock
{
private $lockKey;
private $initialTtl;
/**
* 锁
*
* @param string $lockKey
* @param integer $initialTtl
*/
public function __construct($lockKey, $initialTtl = 0)
{
$this->lockKey = $lockKey;
$this->initialTtl = $initialTtl;
}
/**
* 锁定
*
* @param mixed $lockValue 如不传则默认为当前时间戳
* @param integer $ttl 如不传则使用初始值
* @return boolean true表示锁定成功,false表示加锁失败
*/
public function lock($lockValue = null, $ttl = null)
{
if ($ttl === null) {
$ttl = $this->initialTtl;
}
//不存在才锁定
$expire = [‘nx’];
//
if (is_int($ttl) && $ttl > 0) {
$expire[‘ex’] = $ttl;
}
return $this->set($this->lockKey, $lockValue === null ? time() : $lockValue, $expire);
}
/**
* 不阻塞锁
* 这里指的是锁存在的情况下可以更新锁
*
* @param mixed $lockValue
* @param integer $ttl
* @return boolean true表示锁定成功,false表示加锁失败
*/
public function unblockLock($lockValue = null, $ttl = null)
{
if ($ttl === null) {
$ttl = $this->initialTtl;
}
return Cache::set($this->lockKey, $lockValue === null ? time() : $lockValue, $ttl);
}
/**
* 锁值自增长
*
* @return integer
*/
public function inc()
{
return Cache::inc($this->lockKey);
}
/**
* 是否已锁
*
* @return boolean
*/
public function isLocked()
{
return Cache::has($this->lockKey);
}
/**
* 获取锁值
*
* @return mixed
*/
public function getLockValue()
{
return Cache::get($this->lockKey);
}
/**
* 释放锁
*
*/
public function release()
{
return Cache::rm($this->lockKey);
}
/**
* 设置Cache值,代替think\Cache::set
*
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param array $expire 有效时间(秒) Array(‘nx’, ‘ex’=>10)
* @return boolean
*/
protected function set($name, $value, $expire = [‘nx’])
{
$cache = Cache::init();
$options = Config::get(‘cache’);
$key = $options[‘prefix’] . $name;;
$value = is_scalar($value) ? $value : ‘think_serialize:’ . serialize($value);
return $cache->handler()->set($key, $value, $expire);
}
}
(文章今日已有 1 人访问,总访问量 17 ::>_<::)