没有不值得去解决的问题,也没有不值得去学习的技术!

Compare Request Body Parameters With a Cached Array Using Indexed Array Diff in PHP 7.4

In Postman, POST to http://api.pcs-api.localhost/v1/mobile/rtcs/invite-accept, and write the data from the Body into the Redis cache as an array

1. In Postman, POST to http://api.pcs-api.localhost/v1/mobile/rtcs/invite-accept, and write the data from the Body into the Redis cache as an array, as shown in Figure 1.

In Postman, POST to http://api.pcs-api.localhost/v1/mobile/rtcs/invite-accept, and write the data from the Body into the Redis cache as an array
Figure 1

2. With each request, the data in the Body changes. The implementation logic in the program is as follows:

JSON

RTC 邀请接受通话:/mobile/rtcs/invite-accept ( mobile/rtc/invite-accept )

1、请求参数列表
( 1 ) user_name:必填,用户名称
( 2 ) call_mode:必填,通话方式,voice_call:语音通话;video_call:视频通话

2、输入数据验证规则
( 1 ) 必填:user_name、call_mode
( 2 ) 字符串:user_name、call_mode
( 3 ) 范围([voice_call, video_call]):call_mode
( 4 ) 判断房间是否存在,是:响应失败

3、操作数据
( 1 ) 设置RTC 邀请接受通话数据的缓存键
( 2 ) 从缓存中取回RTC 邀请接受通话数据
A、如果不存在,则序列化,将RTC 邀请接受通话数据存放到缓存供下次使用,在缓存中永久保留
B、如果存在,则反序列化,带索引检查计算数组的差集,如果差集不为空,将RTC 邀请接受通话数据存放到缓存供下次使用,在缓存中永久保留

3. Compute the difference of arrays with an index check to compare whether the arrays are equal. Since the data in the Body has undergone strict validation, it is guaranteed that the data in the Body will only be less than or equal to the data in the cache. The code implementation is as follows:

PHP
// 设置RTC 邀请接受通话数据的缓存键
$redisCache = Yii::$app->redisCache;
$gisRtcInviteAcceptCacheKey = implode(':', ['gis', 'rtc', 'invite', 'accept', $identity->group_id]);
 
// 从缓存中取回RTC 邀请接受通话数据
$gisRtcInviteAcceptCacheData = $redisCache[$gisRtcInviteAcceptCacheKey];
 
$requestGisRtcInviteAccept = [
    'user_name' => $user_name,
    'call_mode' => $call_mode,
];
 
if ($gisRtcInviteAcceptCacheData === false) {
    // 将RTC 邀请接受通话数据存放到缓存供下次使用,在缓存中永久保留
    $redisCache->set($gisRtcInviteAcceptCacheKey, serialize($requestGisRtcInviteAccept));
} else {
    $gisRtcInviteAccept = unserialize($gisRtcInviteAcceptCacheData);
 
    // 带索引检查计算数组的差集
    $result = array_diff_assoc($gisRtcInviteAccept, $requestGisRtcInviteAccept);
 
    if (!empty($result)) {
        // 将RTC 邀请接受通话数据存放到缓存供下次使用,在缓存中永久保留
        $redisCache->set($gisRtcInviteAcceptCacheKey, serialize($requestGisRtcInviteAccept));
    }
}

4. array_diff_assoc() returns an array containing all the values from array1 that are not present in any of the other argument arrays. Note that, unlike array_diff(), the keys are also used in the comparison. Print the array from the cache, the request data from the Body, and the computed array difference with the index check, respectively. The results match expectations. The cache data is overwritten only when the computed array difference with the index check is not empty.

Plaintext

$gisRtcInviteAccept
Array
(
    [user_name] => huaqiyun
    [call_mode] => voice_call
)

{
    "user_name": "huaqiyun1",
    "call_mode": "voice_call"
}

array(1) {
  ["user_name"]=>
  string(8) "huaqiyun"
}



$gisRtcInviteAccept
Array
(
    [user_name] => huaqiyun
    [call_mode] => voice_call
)

{
    "user_name": "huaqiyun",
    "call_mode": "video_call"
}

array(1) {
  ["call_mode"]=>
  string(10) "voice_call"
}



$gisRtcInviteAccept
Array
(
    [user_name] => huaqiyun
    [call_mode] => voice_call
)

{
    "user_name": "huaqiyun2",
    "call_mode": "video_call"
}

array(2) {
  ["user_name"]=>
  string(8) "huaqiyun"
  ["call_mode"]=>
  string(10) "voice_call"
}

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系