초기커밋
This commit is contained in:
178
ServerCommon/Cache/PartyInvitePartyRecvCacheRequest.cs
Normal file
178
ServerCommon/Cache/PartyInvitePartyRecvCacheRequest.cs
Normal file
@@ -0,0 +1,178 @@
|
||||
using System.Globalization;
|
||||
|
||||
|
||||
using Google.Protobuf;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
using StackExchange.Redis;
|
||||
|
||||
|
||||
using ServerCore;
|
||||
using ServerBase;
|
||||
using ServerCommon;
|
||||
using ServerCommon.BusinessLogDomain;
|
||||
using MetaAssets;
|
||||
|
||||
|
||||
using USER_GUID = System.String;
|
||||
|
||||
|
||||
namespace ServerCommon;
|
||||
|
||||
public class PartyInvitePartyRecvCache : CacheBase
|
||||
{
|
||||
public Dictionary<string, DateTime> PartyInvitePartyRecvs { get; set; } = new();
|
||||
|
||||
public string toBasicString()
|
||||
{
|
||||
return $"PartyCache: PartyInvitePartyRecvs:{PartyInvitePartyRecvs}";
|
||||
}
|
||||
}
|
||||
|
||||
public class PartyInvitePartyRecvCacheRequest : RedisRequestPrivateBase
|
||||
{
|
||||
private readonly USER_GUID m_user_guid;
|
||||
|
||||
// out
|
||||
private PartyInvitePartyRecvCache? m_party_invite_party_recv_cache_nullable { get; set; }
|
||||
|
||||
public PartyInvitePartyRecvCacheRequest(UserBase owner, RedisConnector redisConnector) : base(owner, redisConnector)
|
||||
{
|
||||
var account_attribute = owner.getEntityAttribute<AccountAttribute>();
|
||||
ArgumentNullException.ThrowIfNull(account_attribute);
|
||||
|
||||
m_user_guid = account_attribute.UserGuid;
|
||||
}
|
||||
|
||||
protected override string onMakeKey() => $"inviteparty:recvlist:{m_user_guid}";
|
||||
|
||||
public override string toBasicString() => $"PartyInvitePartyRecvCache: Out:{m_party_invite_party_recv_cache_nullable?.toBasicString()}";
|
||||
|
||||
public PartyInvitePartyRecvCache? getPartyInvitePartyRecvCache() => m_party_invite_party_recv_cache_nullable;
|
||||
|
||||
public async Task<Result> fetchInvitePartyRecvCache()
|
||||
{
|
||||
var result = new Result();
|
||||
string err_msg;
|
||||
|
||||
try
|
||||
{
|
||||
result = await onPrepareRequest();
|
||||
if (result.isFail()) return result;
|
||||
|
||||
var database = getDatabase();
|
||||
ArgumentNullException.ThrowIfNull(database);
|
||||
|
||||
var delete_time = DateTime.UtcNow.AddMilliseconds(-1 * Constant.KEEP_INVITEPARTY_TIME);
|
||||
var curr_party_invite_party_recvs = new Dictionary<string, DateTime>();
|
||||
var delete_list = new List<string>();
|
||||
|
||||
var redis_value = await database.HashGetAllAsync(getKey(), CommandFlags.PreferReplica);
|
||||
foreach (var send_value in redis_value)
|
||||
{
|
||||
var recv_time = send_value.Value.ToString().toUtcTime();
|
||||
if (recv_time < delete_time)
|
||||
{
|
||||
delete_list.Add(send_value.Name.ToString());
|
||||
continue;
|
||||
}
|
||||
curr_party_invite_party_recvs.Add(send_value.Name.ToString(), recv_time);
|
||||
}
|
||||
|
||||
m_party_invite_party_recv_cache_nullable ??= new PartyInvitePartyRecvCache();
|
||||
m_party_invite_party_recv_cache_nullable.PartyInvitePartyRecvs.Clear();
|
||||
foreach (var send in curr_party_invite_party_recvs)
|
||||
{
|
||||
m_party_invite_party_recv_cache_nullable.PartyInvitePartyRecvs.Add(send.Key, send.Value);
|
||||
}
|
||||
|
||||
result = await deleteInvitePartyRecvCache(delete_list);
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var error_code = ServerErrorCode.TryCatchException;
|
||||
err_msg = $"Failed to get PartyInvitePartyRecvCache from Redis !!! : : errorCode{error_code}, errMsg:{e.Message} - {nameof(fetchInvitePartyRecvCache)}";
|
||||
result.setFail(error_code, err_msg);
|
||||
Log.getLogger().error(err_msg);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<Result> addInvitePartyRecvCache(string party_guid, DateTime recv_time)
|
||||
{
|
||||
var result = new Result();
|
||||
string err_msg;
|
||||
|
||||
try
|
||||
{
|
||||
result = await onPrepareRequest();
|
||||
if (result.isFail()) return result;
|
||||
|
||||
var database = getDatabase();
|
||||
ArgumentNullException.ThrowIfNull(database);
|
||||
|
||||
await database.HashSetAsync(getKey(), party_guid, recv_time.ToString(CultureInfo.InvariantCulture));
|
||||
|
||||
m_party_invite_party_recv_cache_nullable ??= new PartyInvitePartyRecvCache();
|
||||
m_party_invite_party_recv_cache_nullable.PartyInvitePartyRecvs.Add(party_guid, recv_time);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var error_code = ServerErrorCode.TryCatchException;
|
||||
err_msg = $"Failed to add party invite party recv from Redis !!! : : errorCode{error_code}, errMsg:{e.Message} - {nameof(addInvitePartyRecvCache)}";
|
||||
result.setFail(error_code, err_msg);
|
||||
Log.getLogger().error(err_msg);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<Result> deleteInvitePartyRecvCache(IReadOnlyList<string> delete_user_guids)
|
||||
{
|
||||
var result = new Result();
|
||||
string err_msg;
|
||||
|
||||
try
|
||||
{
|
||||
result = await onPrepareRequest();
|
||||
if (result.isFail()) return result;
|
||||
|
||||
var database = getDatabase();
|
||||
ArgumentNullException.ThrowIfNull(database);
|
||||
|
||||
var delete_values = new List<RedisValue>(delete_user_guids.Select(x => new RedisValue(x)));
|
||||
_ = await database.HashDeleteAsync(getKey(), delete_values.ToArray(), CommandFlags.PreferReplica);
|
||||
|
||||
foreach (var delete in delete_user_guids)
|
||||
{
|
||||
m_party_invite_party_recv_cache_nullable?.PartyInvitePartyRecvs.Remove(delete);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var error_code = ServerErrorCode.TryCatchException;
|
||||
err_msg = $"Failed to delete party invite party recv from Redis !!! : : errorCode{error_code}, errMsg:{e.Message} - {nameof(deleteInvitePartyRecvCache)}";
|
||||
result.setFail(error_code, err_msg);
|
||||
Log.getLogger().error(err_msg);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<List<string>?> organizeInvitePartyRecvCache()
|
||||
{
|
||||
if (null == m_party_invite_party_recv_cache_nullable) return null;
|
||||
|
||||
var delete_time = DateTime.UtcNow.AddMilliseconds(-1 * Constant.KEEP_INVITEPARTY_TIME);
|
||||
var delete_sends = (from recv in m_party_invite_party_recv_cache_nullable.PartyInvitePartyRecvs where recv.Value < delete_time select recv.Key).ToList();
|
||||
_ = await deleteInvitePartyRecvCache(delete_sends);
|
||||
|
||||
return delete_sends;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user