using Newtonsoft.Json; using Google.Protobuf; using Google.Protobuf.WellKnownTypes; using StackExchange.Redis; using ServerCore; using ServerBase; using ServerCommon; using ServerCommon.BusinessLogDomain; using MetaAssets; namespace ServerCommon; public class PartyCache : CacheBase { public string PartyGuid { get; set; } = string.Empty; public string PartyName { get; set; } = string.Empty; public string PartyLeaderCharGuid { get; set; } = string.Empty; public string PartyLeaderNickname { get; set; } = string.Empty; public DateTime CreatePartyTime { get; set; } = DateTimeHelper.Current; public Timestamp? LastVoteTime { get; set; } public string toBasicString() { return $"PartyCache: PartyGuid:{PartyGuid}, PartyName:{PartyName}, PartyLeaderCharGuid:{PartyLeaderCharGuid}, LastVoteTime:{LastVoteTime?.toBasicString() ?? string.Empty}"; } } public class PartyCacheRequest : RedisRequestSharedBase { // in private readonly string m_party_guid; // out private PartyCache? m_party_cache_nullable { get; set; } public PartyCacheRequest(string party_guid, RedisConnector redisConnector) : base(party_guid, redisConnector) { m_party_guid = party_guid; } protected override string onMakeKey() => $"party:{m_party_guid}:info"; public override string toBasicString() => $"LoginCache: In:PartyGuid:{m_party_guid}, Out:{m_party_cache_nullable?.toBasicString()}"; public PartyCache? getPartyCache() => m_party_cache_nullable; public async Task keepPartyCache() { var result = new Result(); string err_msg; try { result = await onPrepareRequest(); if (result.isFail()) return result; var database = getDatabase(); ArgumentNullException.ThrowIfNull(database); var redis_value = await database.KeyExpireAsync(getKey(), TimeSpan.FromMilliseconds(Constant.KEEP_PARTY_TIME)); if (false == redis_value) { err_msg = $"Failed to set expire PartyCache in Redis !!! : Key:{getKey()} - {nameof(keepPartyCache)}"; result.setFail(ServerErrorCode.RedisGlobalPartyCacheWriteFailed, err_msg); Log.getLogger().error(err_msg); } } catch (Exception e) { var error_code = ServerErrorCode.TryCatchException; err_msg = $"Failed to keep PartyCache from Redis !!! : : errorCode{error_code}, errMsg:{e.Message} - {nameof(keepPartyCache)}"; result.setFail(error_code, err_msg); Log.getLogger().error(err_msg); } return result; } public async Task fetchPartyCache() { var result = new Result(); string err_msg; try { result = await onPrepareRequest(); if (result.isFail()) return result; var database = getDatabase(); ArgumentNullException.ThrowIfNull(database); var redis_value = await database.StringGetAsync(getKey(), CommandFlags.PreferReplica); if (false == redis_value.HasValue) { err_msg = $"Failed to get PartyCache in Redis !!! : Key:{getKey()} - {nameof(fetchPartyCache)}"; result.setFail(ServerErrorCode.RedisGlobalPartyCacheGetFailed, err_msg); Log.getLogger().error(err_msg); return result; } var curr_party_cache = JsonConvert.DeserializeObject(redis_value.ToString()); if (null == curr_party_cache) { err_msg = $"Failed to convert DeserializeObject of Json !!! : {toBasicString()} - {nameof(fetchPartyCache)}"; result.setFail(ServerErrorCode.JsonConvertDeserializeFailed, err_msg); Log.getLogger().error(err_msg); return result; } m_party_cache_nullable = curr_party_cache; return result; } catch (Exception e) { var error_code = ServerErrorCode.TryCatchException; err_msg = $"Failed to get PartyCache from Redis !!! : : errorCode{error_code}, errMsg:{e.Message} - {nameof(fetchPartyCache)}"; result.setFail(error_code, err_msg); Log.getLogger().error(err_msg); } return result; } public async Task createPartyCache(string leader_guid, string leader_nickname) { var result = new Result(); string err_msg; try { result = await onPrepareRequest(); if (result.isFail()) return result; m_party_cache_nullable ??= new PartyCache(); m_party_cache_nullable.PartyGuid = m_party_guid; m_party_cache_nullable.PartyName = string.Empty; m_party_cache_nullable.PartyLeaderCharGuid = leader_guid; m_party_cache_nullable.PartyLeaderNickname = leader_nickname; var cache = JsonConvert.SerializeObject(m_party_cache_nullable); var database = getDatabase(); ArgumentNullException.ThrowIfNull(database); var redis_value = await database.StringSetAsync(getKey(), cache, TimeSpan.FromMilliseconds(Constant.KEEP_PARTY_TIME)); if (false == redis_value) { err_msg = $"Failed to get PartyCache in Redis !!! : Key:{getKey()} - {nameof(createPartyCache)}"; result.setFail(ServerErrorCode.RedisGlobalPartyCacheWriteFailed, err_msg); Log.getLogger().error(err_msg); return result; } return result; } catch (Exception e) { var error_code = ServerErrorCode.TryCatchException; err_msg = $"Failed to get PartyCache from Redis !!! : : errorCode{error_code}, errMsg:{e.Message} - {nameof(createPartyCache)}"; result.setFail(error_code, err_msg); Log.getLogger().error(err_msg); } return result; } public async Task deletePartyCache() { var result = new Result(); string err_msg; try { result = await onPrepareRequest(); if (result.isFail()) return result; m_party_cache_nullable = null; var database = getDatabase(); ArgumentNullException.ThrowIfNull(database); _ = await database.KeyDeleteAsync(getKey() , CommandFlags.FireAndForget); return result; } catch (Exception e) { var error_code = ServerErrorCode.TryCatchException; err_msg = $"Failed to delete PartyCache from Redis !!! : : errorCode{error_code}, errMsg:{e.Message} - {nameof(deletePartyCache)}"; result.setFail(error_code, err_msg); Log.getLogger().error(err_msg); } return result; } public async Task upsertPartyCache() { var result = new Result(); string err_msg; try { result = await onPrepareRequest(); if (result.isFail()) return result; var cache = JsonConvert.SerializeObject(m_party_cache_nullable); var database = getDatabase(); ArgumentNullException.ThrowIfNull(database); var redis_value = await database.StringSetAsync(getKey(), cache, TimeSpan.FromMilliseconds(Constant.KEEP_PARTY_TIME)); if (false == redis_value) { err_msg = $"Failed to set PartyCache in Redis !!! : Key:{getKey()} - {nameof(upsertPartyCache)}"; result.setFail(ServerErrorCode.RedisGlobalPartyCacheWriteFailed, err_msg); Log.getLogger().error(err_msg); return result; } return result; } catch (Exception e) { var error_code = ServerErrorCode.TryCatchException; err_msg = $"Failed to get PartyCache from Redis !!! : : errorCode{error_code}, errMsg:{e.Message} - {nameof(upsertPartyCache)}"; result.setFail(error_code, err_msg); Log.getLogger().error(err_msg); } return result; } }