代码功能更新

This commit is contained in:
root
2025-05-18 07:42:27 +00:00
parent 7fc1d422a1
commit 4a90c41da2
2083 changed files with 695218 additions and 3 deletions
+57
View File
@@ -0,0 +1,57 @@
<?php
namespace laytp\library;
use think\facade\Cache;
/**
* Redis锁
*/
class Redis
{
/**
* 得到一个redis锁,循环获取锁,直到获取到锁为止
* @param $name string 锁名称
* @param $ttl int 锁存在的时间,单位秒,默认60秒
* @return bool
*/
public static function getLock($name, $ttl=60)
{
set_time_limit(0);
$redis = Cache::store('redis')->handler();
while(true){
if($redis->set($name, 1, ['NX', 'EX'=>$ttl])){
break;
}
}
return true;
}
/**
* 得到一个redis锁,仅尝试获取一次
*
* @param $name string 锁名称
* @param $ttl int 锁存在的时间,单位秒,默认60秒
* @return bool
*/
public static function getOnceLock($name, $ttl=60)
{
set_time_limit(0);
$redis = Cache::store('redis')->handler();
if($redis->set($name, 1, ['NX', 'EX'=>$ttl])){
return true;
}
return false;
}
/**
* 删除一个redis锁
* @param $name string 锁名称
* @return bool
*/
public static function delLock($name)
{
$redis = Cache::store('redis')->handler();
$redis->del($name);
return true;
}
}