<?php
/*----------------- 常量定义*/
define('USER', 'user:');
define('INVENTORY', 'inventory:');
define('MARKET', 'market');
/*----------------- 公共方法*/
function log_($message)
{
printf($message.PHP_EOL);
sleep(1);
}
function can_buy($handler, $buyer, $item)
{
$isItem = $handler->zScore(MARKET, $item);
if(false === $isItem) {
return false;
}
$money = $isItem;
$bar = $handler->hget(USER.$buyer, 'funds');
if($bar < $money) {
return false;
}
return $money;
}
function can_sell($handler, $user, $item)
{
return $handler->sIsMember(INVENTORY.$user, $item);
}
/*----------------- 使用watch来实现加锁的功能*/
$redis = new Redis();
try{
$redis->connect('127.0.0.1', 6379, 1, NULL, 100);
} catch (Exception $e) {
die('redis connect fail! '.$e->getMessage().PHP_EOL);
}
/**
* &
redis--分布式锁; watch实现[php]
最新推荐文章于 2023-05-31 20:55:34 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)