초기커밋
This commit is contained in:
170
ServerCommon/Cache/LocationCacheRequest.cs
Normal file
170
ServerCommon/Cache/LocationCacheRequest.cs
Normal file
@@ -0,0 +1,170 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using StackExchange.Redis;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
|
||||
|
||||
using ServerCore;
|
||||
using ServerBase;
|
||||
|
||||
|
||||
namespace ServerCommon
|
||||
{
|
||||
public class LocationCache : CacheBase, ICopyCacheFromEntityAttribute
|
||||
{
|
||||
public ChannelServerLocation LastestChannelServerLocation = new();
|
||||
public List<IndunLocation> ReturnIndunLocations = new();
|
||||
public IndunLocation EnterIndunLocation = new();
|
||||
public Timestamp MoveChannelTime = DateTimeHelper.MinTime.ToTimestamp();
|
||||
|
||||
public LocationCache()
|
||||
: base()
|
||||
{
|
||||
}
|
||||
|
||||
public bool copyCacheFromEntityAttribute(EntityAttributeBase entityAttributeBase)
|
||||
{
|
||||
var err_msg = string.Empty;
|
||||
var to_cast_string = typeof(LocationAttribute).Name;
|
||||
|
||||
var location_attribute = entityAttributeBase as LocationAttribute;
|
||||
if (null == location_attribute)
|
||||
{
|
||||
err_msg = $"Failed to copyCacheFromEntityAttribute() !!!, location_attribute is null :{to_cast_string}";
|
||||
Log.getLogger().error(err_msg);
|
||||
return false;
|
||||
}
|
||||
|
||||
//=====================================================================================
|
||||
// Attribute => Cache
|
||||
//=====================================================================================
|
||||
LastestChannelServerLocation = location_attribute.LastestChannelServerLocation.clone();
|
||||
ReturnIndunLocations = location_attribute.ReturnIndunLocations.Select(x => x.clone()).ToList();
|
||||
EnterIndunLocation = location_attribute.EnterIndunLocation.clone();
|
||||
MoveChannelTime = location_attribute.MoveChannelTime;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public string toBasicString()
|
||||
{
|
||||
return $"Location: ";
|
||||
}
|
||||
}
|
||||
|
||||
public class LocationCacheRequest : RedisRequestPrivateBase
|
||||
{
|
||||
public static readonly double LOCATION_CACHE_EXPIRY_TIME = ConstValue.default_1_min_to_sec * ConstValue.default_1_sec_to_milisec;
|
||||
|
||||
// In
|
||||
private string m_user_guid;
|
||||
|
||||
// Out
|
||||
private LocationCache? m_location_cache_nullable;
|
||||
|
||||
public LocationCacheRequest(UserBase owner, RedisConnector redisConnector)
|
||||
: base(owner, redisConnector)
|
||||
{
|
||||
var user_attribute = owner.getEntityAttribute<UserAttribute>();
|
||||
NullReferenceCheckHelper.throwIfNull(user_attribute, () => $"user_attribute is null !!! - {owner.toBasicString()}");
|
||||
|
||||
m_user_guid = user_attribute.UserGuid;
|
||||
}
|
||||
|
||||
public override string toBasicString()
|
||||
{
|
||||
return $"Location: In:UserGuid:{m_user_guid}, Out:{m_location_cache_nullable?.toBasicString()}";
|
||||
}
|
||||
|
||||
protected override string onMakeKey()
|
||||
{
|
||||
return $"location:{m_user_guid}";
|
||||
}
|
||||
|
||||
public LocationCache? getLocationCache() => m_location_cache_nullable;
|
||||
|
||||
public async Task<Result> fetchLocation()
|
||||
{
|
||||
var result = new Result();
|
||||
var err_msg = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
result = await onPrepareRequest();
|
||||
if (result.isFail())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
var database = getDatabase();
|
||||
NullReferenceCheckHelper.throwIfNull(database, () => $"database is null !!! - {getOwner().toBasicString()}");
|
||||
var redis_value = await database.StringGetAsync(getKey(), CommandFlags.PreferReplica);
|
||||
if (true == redis_value.HasValue)
|
||||
{
|
||||
var location_cache = JsonConvert.DeserializeObject<LocationCache>(redis_value.ToString());
|
||||
if (null == location_cache)
|
||||
{
|
||||
err_msg = $"Failed to convert DeserializeObject of Json !!! : {toBasicString()} - {getOwner().toBasicString()}";
|
||||
result.setFail(ServerErrorCode.JsonConvertDeserializeFailed, err_msg);
|
||||
Log.getLogger().error(err_msg);
|
||||
return result;
|
||||
}
|
||||
|
||||
m_location_cache_nullable = location_cache;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_location_cache_nullable = new();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var error_code = ServerErrorCode.TryCatchException;
|
||||
err_msg = $"Failed to get LocationCache from Redis !!! : : errorCode{error_code}, errMsg:{e.Message} - {getOwner().toBasicString()}";
|
||||
result.setFail(error_code, err_msg);
|
||||
Log.getLogger().error(err_msg);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<Result> upsertLocation()
|
||||
{
|
||||
var result = new Result();
|
||||
var err_msg = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
var location_cache_json_string = getLocationCache()?.toJsonString();
|
||||
|
||||
var database = getDatabase();
|
||||
NullReferenceCheckHelper.throwIfNull(database, () => $"database is null !!! - {getOwner().toBasicString()}");
|
||||
|
||||
if (false == await database.StringSetAsync(getKey(), location_cache_json_string, TimeSpan.FromMilliseconds(LOCATION_CACHE_EXPIRY_TIME)))
|
||||
{
|
||||
err_msg = $"Failed to set LoactionCacheInfo to Redis !!! : redisKey:{getKey()} - {getOwner().toBasicString()}";
|
||||
result.setFail(ServerErrorCode.RedisLocationCacheSetFailed, err_msg);
|
||||
Log.getLogger().error(err_msg);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
err_msg = $"Failed to set LocationCache in Redis !!! : message{e} - {getOwner().toBasicString()}";
|
||||
result.setFail(ServerErrorCode.TryCatchException, err_msg);
|
||||
Log.getLogger().error(result.toBasicString());
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user