220 lines
6.9 KiB
C#
220 lines
6.9 KiB
C#
using Newtonsoft.Json;
|
|
using StackExchange.Redis;
|
|
|
|
|
|
using ServerCore;
|
|
using ServerBase;
|
|
|
|
|
|
using META_ID = System.UInt32;
|
|
using USER_GUID = System.String;
|
|
using LAND_AUCTION_NUMBER = System.Int32;
|
|
|
|
|
|
namespace ServerCommon;
|
|
|
|
public class LandAuctionCache : CacheBase
|
|
{
|
|
public META_ID LandMetaId { get; set; } = 0;
|
|
|
|
public LAND_AUCTION_NUMBER AuctionNumber { get; set; } = 0;
|
|
|
|
public LandAuctionState LandAuctionState { get; set; } = LandAuctionState.None;
|
|
|
|
public LandAuctionResult LandAuctionResult { get; set; } = LandAuctionResult.None;
|
|
|
|
public DateTime ProcessVersionTime { get; set; } = DateTimeHelper.MinTime;
|
|
|
|
public LandAuctionCache()
|
|
{ }
|
|
}
|
|
|
|
|
|
public class LandAuctionCacheRequest : RedisRequestSharedBase
|
|
{
|
|
private readonly META_ID m_land_meta_id;
|
|
|
|
private LandAuctionCache? m_land_auction_cache;
|
|
|
|
public LandAuctionCacheRequest(META_ID landMetaId, RedisConnector redisConnector)
|
|
: base(landMetaId.ToString(), redisConnector)
|
|
{
|
|
m_land_meta_id = landMetaId;
|
|
}
|
|
|
|
public LandAuctionCacheRequest(LandAuctionCache landAuctionCache, RedisConnector redisConnector)
|
|
: base(landAuctionCache.LandMetaId.ToString(), redisConnector)
|
|
{
|
|
m_land_meta_id = landAuctionCache.LandMetaId;
|
|
m_land_auction_cache = landAuctionCache;
|
|
}
|
|
|
|
public async Task<Result> upsertLandAuction(double cacheExpiryMinutes)
|
|
{
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
|
|
try
|
|
{
|
|
result = await onPrepareRequest();
|
|
if (result.isFail())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
var location_cache_json_string = getLandAuctionCache()?.toJsonString();
|
|
|
|
var database = getDatabase();
|
|
NullReferenceCheckHelper.throwIfNull(database, () => $"database is null !!! - {toBasicString()}");
|
|
|
|
var expiry = TimeSpan.FromMinutes(cacheExpiryMinutes);
|
|
if (expiry.TotalMilliseconds <= 0) expiry = TimeSpan.FromMilliseconds(1);
|
|
|
|
if (false == await database.StringSetAsync(getKey(), location_cache_json_string, expiry))
|
|
{
|
|
err_msg = $"Failed to set LandAuctionCache to Redis !!! : redisKey:{getKey()} - {toBasicString()}";
|
|
result.setFail(ServerErrorCode.LandAuctionRedisCacheSetFailed, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
|
|
return result;
|
|
}
|
|
|
|
err_msg = $"LandAuctionCache save to Cache !!! : redisKey:{getKey()}, ttlMinutes:{cacheExpiryMinutes} - {toBasicString()}";
|
|
Log.getLogger().info(err_msg);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
var error_code = ServerErrorCode.TryCatchException;
|
|
err_msg = $"Failed to process in upsertLandAuction() !!! : errorCode{error_code}, exception:{e} - redisKey:{getKey()}, {toBasicString()}";
|
|
result.setFail(error_code, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<Result> fetchLandAuction()
|
|
{
|
|
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 !!!");
|
|
|
|
var redis_value = await database.StringGetAsync(getKey(), CommandFlags.PreferReplica);
|
|
if (true == redis_value.HasValue)
|
|
{
|
|
var curr_buff_cache = JsonConvert.DeserializeObject<LandAuctionCache>(redis_value.ToString());
|
|
if (null == curr_buff_cache)
|
|
{
|
|
err_msg = $"Failed to convert DeserializeObject of Json !!! : {redis_value.ToString()} - {toBasicString()}";
|
|
result.setFail(ServerErrorCode.JsonConvertDeserializeFailed, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
|
|
return result;
|
|
}
|
|
|
|
m_land_auction_cache = curr_buff_cache;
|
|
|
|
err_msg = $"LandAuctionCache fetch from Cache !!! : redisKey:{getKey()} - {toBasicString()}";
|
|
Log.getLogger().info(err_msg);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
var error_code = ServerErrorCode.TryCatchException;
|
|
err_msg = $"Failed to process in fetchLandAuction() !!! : errorCode{error_code}, exception:{e} - redisKey:{getKey()}, {toBasicString()}";
|
|
result.setFail(error_code, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<Result> deleteLandAuction()
|
|
{
|
|
var result = new Result();
|
|
string err_msg;
|
|
|
|
try
|
|
{
|
|
result = await onPrepareRequest();
|
|
if (result.isFail())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
m_land_auction_cache = null;
|
|
|
|
var database = getDatabase();
|
|
|
|
var is_success = await database.KeyDeleteAsync(getKey());
|
|
if (false == is_success)
|
|
{
|
|
err_msg = $"Failed to KeyDeleteAsync() !!! : redisKey:{getKey()} - {toBasicString()}";
|
|
Log.getLogger().debug(err_msg);
|
|
|
|
return result;
|
|
}
|
|
|
|
err_msg = $"LandAuctionCache deleted from Cache - redisKey:{getKey()}, {toBasicString()}";
|
|
Log.getLogger().debug(err_msg);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
var error_code = ServerErrorCode.TryCatchException;
|
|
err_msg = $"Failed to process in deleteLandAuction() !!! : errorCode{error_code}, execption:{e} - redisKey:{getKey()}, {toBasicString()}";
|
|
result.setFail(error_code, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
protected override string onMakeKey()
|
|
{
|
|
return $"{{{m_land_meta_id}}}:land_auction";
|
|
}
|
|
|
|
public async Task<bool> isExistKey()
|
|
{
|
|
try
|
|
{
|
|
var result = await onPrepareRequest();
|
|
if (result.isFail()) return false;
|
|
|
|
var database = getDatabase();
|
|
return await database.KeyExistsAsync(onMakeKey());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
var error_code = ServerErrorCode.TryCatchException;
|
|
var err_msg = $"Failed to process in isExistKey() !!! : errorCode{error_code}, execption:{e} - redisKey:{getKey()}, {toBasicString()}";
|
|
Log.getLogger().error(err_msg);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public string toKey()
|
|
{
|
|
return onMakeKey();
|
|
}
|
|
|
|
public LandAuctionCache? getLandAuctionCache() => m_land_auction_cache;
|
|
|
|
public void setLandAuctionCache(LandAuctionCache cache) => m_land_auction_cache = cache;
|
|
|
|
public override string toBasicString()
|
|
{
|
|
return $"{this.getTypeName()} - LandMetaId:{m_land_meta_id}";
|
|
}
|
|
} |