241 lines
8.6 KiB
C#
241 lines
8.6 KiB
C#
using Google.Protobuf;
|
|
using Google.Protobuf.WellKnownTypes;
|
|
using StackExchange.Redis;
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using ServerCore;
|
|
using ServerBase;
|
|
using ServerCommon;
|
|
using ServerCommon.BusinessLogDomain;
|
|
using MetaAssets;
|
|
|
|
|
|
namespace ServerCommon;
|
|
|
|
public class PartyInstanceCache : CacheBase
|
|
{
|
|
public int InstanceId { get; set; } = 0;
|
|
public string RoomId { get; set; } = string.Empty;
|
|
public Timestamp StartTime { get; set; } = new();
|
|
public Timestamp EndTime { get; set; } = new();
|
|
public int JoinMemberCount { get; set; } = 0;
|
|
|
|
public string toBasicString()
|
|
{
|
|
return $"PartyInstanceCache: InstanceId:{InstanceId}, RoomId:{RoomId}, StartTime:{StartTime}, EndTime:{EndTime}, JoinMemberCount:{JoinMemberCount}";
|
|
}
|
|
}
|
|
|
|
public class PartyInstanceCacheRequest : RedisRequestSharedBase
|
|
{
|
|
// in
|
|
private readonly string m_party_guid;
|
|
|
|
// out
|
|
private PartyInstanceCache? m_party_instance_cache_nullable { get; set; }
|
|
|
|
public PartyInstanceCacheRequest(string party_guid, RedisConnector redisConnector) : base(party_guid,
|
|
redisConnector)
|
|
{
|
|
m_party_guid = party_guid;
|
|
}
|
|
|
|
protected override string onMakeKey() => $"party:{m_party_guid}:instance";
|
|
public override string toBasicString() => $"PartyInstanceCache: In:PartyGuid:{m_party_guid}, Out:{m_party_instance_cache_nullable?.toBasicString()}";
|
|
|
|
public PartyInstanceCache? getPartyInstanceCache() => m_party_instance_cache_nullable;
|
|
|
|
public async Task<Result> keepPartyInstanceCache()
|
|
{
|
|
var result = new Result();
|
|
string err_msg;
|
|
|
|
if (null == m_party_instance_cache_nullable) return result;
|
|
|
|
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 PartyInstanceCache in Redis !!! : Key:{getKey()} - {nameof(keepPartyInstanceCache)}";
|
|
result.setFail(ServerErrorCode.RedisGlobalPartyCacheWriteFailed, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
var error_code = ServerErrorCode.TryCatchException;
|
|
err_msg = $"Failed to keep PartyInstanceCache from Redis !!! : : errorCode{error_code}, errMsg:{e.Message} - {nameof(keepPartyInstanceCache)}";
|
|
result.setFail(error_code, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<Result> fetchPartyInstanceCache()
|
|
{
|
|
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 PartyInstanceCache in Redis !!! : Key:{getKey()} - {nameof(fetchPartyInstanceCache)}";
|
|
result.setFail(ServerErrorCode.RedisGlobalPartyCacheGetFailed, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
return result;
|
|
}
|
|
|
|
var curr_party_instance_cache = JsonConvert.DeserializeObject<PartyInstanceCache>(redis_value.ToString());
|
|
if (null == curr_party_instance_cache)
|
|
{
|
|
err_msg = $"Failed to convert DeserializeObject of Json !!! : {toBasicString()} - {nameof(fetchPartyInstanceCache)}";
|
|
result.setFail(ServerErrorCode.JsonConvertDeserializeFailed, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
return result;
|
|
}
|
|
|
|
m_party_instance_cache_nullable = curr_party_instance_cache;
|
|
return result;
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
var error_code = ServerErrorCode.TryCatchException;
|
|
err_msg = $"Failed to get PartyInstanceCache from Redis !!! : : errorCode{error_code}, errMsg:{e.Message} - {nameof(fetchPartyInstanceCache)}";
|
|
result.setFail(error_code, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<Result> createPartyInstanceCache(int instanceId, string roomId, Timestamp startTime, Timestamp endTime, int joinCount)
|
|
{
|
|
var result = new Result();
|
|
string err_msg;
|
|
|
|
try
|
|
{
|
|
result = await onPrepareRequest();
|
|
if (result.isFail()) return result;
|
|
|
|
m_party_instance_cache_nullable ??= new PartyInstanceCache();
|
|
m_party_instance_cache_nullable.InstanceId = instanceId;
|
|
m_party_instance_cache_nullable.RoomId = roomId;
|
|
m_party_instance_cache_nullable.StartTime = startTime;
|
|
m_party_instance_cache_nullable.EndTime = endTime;
|
|
m_party_instance_cache_nullable.JoinMemberCount = joinCount;
|
|
var cache = JsonConvert.SerializeObject(m_party_instance_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 PartyInstanceCache in Redis !!! : Key:{getKey()} - {nameof(createPartyInstanceCache)}";
|
|
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 PartyInstanceCache from Redis !!! : : errorCode{error_code}, errMsg:{e.Message} - {nameof(createPartyInstanceCache)}";
|
|
result.setFail(error_code, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<Result> deletePartyInstanceCache()
|
|
{
|
|
var result = new Result();
|
|
string err_msg;
|
|
|
|
try
|
|
{
|
|
result = await onPrepareRequest();
|
|
if (result.isFail()) return result;
|
|
|
|
m_party_instance_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 PartyInstanceCache from Redis !!! : : errorCode{error_code}, errMsg:{e.Message} - {nameof(deletePartyInstanceCache)}";
|
|
result.setFail(error_code, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<Result> upsertPartyInstanceCache()
|
|
{
|
|
var result = new Result();
|
|
string err_msg;
|
|
|
|
try
|
|
{
|
|
result = await onPrepareRequest();
|
|
if (result.isFail()) return result;
|
|
|
|
var cache = JsonConvert.SerializeObject(m_party_instance_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 PartyInstanceCache in Redis !!! : Key:{getKey()} - {nameof(upsertPartyInstanceCache)}";
|
|
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 PartyInstanceCache from Redis !!! : : errorCode{error_code}, errMsg:{e.Message} - {nameof(upsertPartyInstanceCache)}";
|
|
result.setFail(error_code, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
} |