151 lines
4.7 KiB
C#
151 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Amazon.OpenSearchService.Model.Internal.MarshallTransformations;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using ServerControlCenter;
|
|
using StackExchange.Redis;
|
|
|
|
|
|
using ServerCore;
|
|
using ServerBase;
|
|
|
|
|
|
namespace ServerCommon;
|
|
|
|
|
|
public class FriendInterlockCacheRequest : RedisRequestPrivateBase
|
|
{
|
|
string m_guid = string.Empty;
|
|
string m_friend_guid = string.Empty;
|
|
|
|
|
|
public FriendInterlockCacheRequest(UserBase owner, RedisConnector redisConnector, string userGuid, string friendGuid)
|
|
: base(owner, redisConnector)
|
|
{
|
|
m_guid = userGuid;
|
|
m_friend_guid = friendGuid;
|
|
}
|
|
|
|
protected override string onMakeKey()
|
|
{
|
|
return $"friendrequestaction:{getSuffix()}";
|
|
}
|
|
|
|
public async Task<Result> getLock()
|
|
{
|
|
var key = onMakeKey();
|
|
var database = getDatabase();
|
|
|
|
var result = new Result();
|
|
string err_msg;
|
|
|
|
try
|
|
{
|
|
if (false == await database.SortedSetAddAsync(key, m_guid, DateTimeOffset.Now.ToUnixTimeMilliseconds()))
|
|
{
|
|
err_msg = $"Failed to add FriendCache from Redis !!! : key : {key}, guid : {m_guid}";
|
|
//result.setFail(ServerErrorCode.TryCatchException, err_msg);
|
|
Log.getLogger().warn(err_msg);
|
|
//return result;
|
|
}
|
|
else
|
|
{
|
|
Log.getLogger().debug($"Success to add FriendCache from Redis !!! : key : {key}, guid : {m_guid}");
|
|
}
|
|
if (false == await database.KeyExpireAsync(key, TimeSpan.FromMilliseconds(10000)))
|
|
{
|
|
err_msg = $"Failed to set expire FriendCache from Redis !!! : key : {key}, guid : {m_guid}";
|
|
result.setFail(ServerErrorCode.TryCatchException, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
return result;
|
|
}
|
|
|
|
Int32 retry_cnt = 3;
|
|
Int32 millisecondTimeout = 100;
|
|
for (Int32 i = 0; i < retry_cnt; i++)
|
|
{
|
|
var values = await database.SortedSetRangeByRankAsync(key, 0, 0, Order.Ascending, CommandFlags.PreferMaster);
|
|
if (false == values[0].HasValue)
|
|
{
|
|
Log.getLogger().warn($"Not Exist Value m_guid : {m_guid}, m_friend_guid : {m_friend_guid}");
|
|
System.Threading.Thread.Sleep(millisecondTimeout);
|
|
continue;
|
|
}
|
|
|
|
if (true == values[0].Equals(m_guid))
|
|
{
|
|
return result;
|
|
|
|
}
|
|
else
|
|
{
|
|
Log.getLogger().warn($"Not Exist Value m_guid : {m_guid}, m_friend_guid : {m_friend_guid}, values[0] : {values[0]}");
|
|
System.Threading.Thread.Sleep(millisecondTimeout);
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
err_msg = $"Failed to get FriendCache from Redis !!! : : errMsg:{e.Message} - {getOwner().toBasicString()}";
|
|
result.setFail(ServerErrorCode.TryCatchException, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
return result;
|
|
}
|
|
|
|
err_msg = $"Failed to get First Data from Redis !!! : - {getOwner().toBasicString()}";
|
|
result.setFail(ServerErrorCode.FriendInfoNotExist, err_msg);
|
|
return result;
|
|
}
|
|
|
|
public async Task<Result> removeFirstCache()
|
|
{
|
|
string key = onMakeKey();
|
|
var database = getDatabase();
|
|
|
|
var result = new Result();
|
|
string err_msg = string.Empty;
|
|
|
|
try
|
|
{
|
|
bool ret = await database.SortedSetRemoveAsync(key, m_guid);
|
|
if (!ret)
|
|
{
|
|
err_msg = $"Failed to remove First Data from Redis !!! : - {getOwner().toBasicString()}";
|
|
result.setFail(ServerErrorCode.ServerLogicError, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
return result;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
err_msg = $"Failed to get FriendCache from Redis !!! : : errMsg:{e.Message} - {getOwner().toBasicString()}";
|
|
result.setFail(ServerErrorCode.TryCatchException, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
return result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public override string toBasicString()
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
private string getSuffix()
|
|
{
|
|
string[] strings = new string[2];
|
|
strings[0] = m_guid;
|
|
strings[1] = m_friend_guid;
|
|
Array.Sort(strings);
|
|
string suffix = strings[0] + strings[1];
|
|
return suffix;
|
|
}
|
|
}
|