In PHP 7.4, the difference set of the calculated array is checked with an index to compare whether the request parameters in the body are equal to the arrays in the cache
1. In postman postman :http://api.pcs-api.localhost/v1/mobile/rtcs/invite-accept, the data in the body is written in the redis cache in the format of an array, as shown in Figure 1
The data in the body will change every time the request is requested, and the implementation logic in the program is as follows:
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. Check the difference set of the calculated array with an index to compare whether the array is equal. Since the data in the body is rigorously verified, it is possible to ensure that the data in the body will only be less than or equal to the data in the cache. The code is implemented as follows:
// 设置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 that includes all values in array1 but not in any other parameter array. Note that the difference between array_diff() is that the key name is also used for comparison. Print the array in the cache, the request data in the body, and the difference set of the calculated array with index checks. in line with expectations. The overwrite cache data is only reset when the difference set with an index check calculation array is not empty.
$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"
}
