초기커밋

This commit is contained in:
2025-05-01 07:20:41 +09:00
commit 98bb2e3c5c
2747 changed files with 646947 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using ServerCore;
namespace ServerBase;
public partial class RedisConnector : RedisConnectorBase, IModule, IInitializer
{
public class ConfigParam : IConfigParam
{
public string ServiceType { get; set; } = string.Empty;
public string ConnectionString { get; set; } = string.Empty;
public Result tryReadFromJsonOrDefault(JObject jObject)
{
var result = new Result();
var err_msg = string.Empty;
try
{
ServiceType = jObject["ServiceType"]?.Value<string>() ?? string.Empty;
ConnectionString = jObject["Redis"]?.Value<string>() ?? string.Empty;
return result;
}
catch (Exception e)
{
var error_code = ServerErrorCode.TryCatchException;
err_msg = $"Exception !!!, Failed to perform in tryReadFromJsonOrDefault() !!! : errorCode:{error_code}, exception:{e} - {this.getTypeName()}";
result.setFail(error_code, err_msg);
Log.getLogger().error(err_msg);
return result;
}
}
public string toBasicString()
{
return $"ConfigParam: ServiceType:{ServiceType}, ConnectionStringOfRedis:{ConnectionString}";
}
};
}

View File

@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServerCore;
using MODULE_ID = System.UInt32;
using SORT_ORDER_NO = System.Int32;
namespace ServerBase;
public partial class RedisConnector : RedisConnectorBase, IModule, IInitializer
{
private readonly ModuleContext m_module_context;
public RedisConnector(ModuleContext context)
: base(validateAndGetConnectionString(context))
{
m_module_context = context;
}
private static string validateAndGetConnectionString(ModuleContext context)
{
if (context.getConfigParam() is not ConfigParam param)
throw new InvalidCastException("IConfigParam must be of type RedisConnectorBase.ConfigParam !!!");
return param.ConnectionString;
}
public async Task<Result> onInit()
{
var err_msg = string.Empty;
var result = new Result();
var module_context = getModuleContext();
var config_param = module_context.getConfigParam() as ConfigParam;
NullReferenceCheckHelper.throwIfNull(config_param, () => $"config_param is null !!!");
var is_success = await tryConnectAsync();
if (false == is_success)
{
err_msg = $"Failed to tryConnectAsync() !!! : {config_param.toBasicString()}";
result.setFail(ServerErrorCode.RedisServerConnectFailed, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
return result;
}
public async Task<Result> startModule()
{
var result = new Result();
return await Task.FromResult(result);
}
public async Task<Result> stopModule()
{
var result = new Result();
return await Task.FromResult(result);
}
public ModuleContext getModuleContext() => m_module_context;
}

View File

@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IIS.Core;
using ServerCore; using ServerBase;
namespace ServerBase;
public abstract partial class RedisRequestBase : IInitializer
{
private readonly RedisConnector m_redis_connector;
private string m_key = string.Empty;
private string m_member = string.Empty;
public RedisRequestBase(RedisConnector redisConnector)
{
m_redis_connector = redisConnector;
}
public virtual Task<Result> onInit()
{
var result = new Result();
return Task.FromResult(result);
}
protected abstract string onMakeKey();
protected virtual string onMakeMember() { return m_member; }
public virtual async Task<Result> onPrepareRequest()
{
var result = new Result();
var err_msg = string.Empty;
result = await Task.Run( () =>
{
if(true == m_key.isNullOrWhiteSpace())
{
m_key = onMakeKey();
}
if (0 >= m_key.Length)
{
err_msg = $"Failed to prepare Reuqest of Redis !!! - {toBasicString()}";
result.setFail(ServerErrorCode.RedisRequestKeyIsEmpty, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
return result;
});
return result;
}
public virtual async Task<Result> onDoRequest()
{
await Task.CompletedTask;
var result = new Result();
return result;
}
public abstract string toBasicString();
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServerCore; using ServerBase;
namespace ServerBase;
public abstract partial class RedisRequestPrivateBase : RedisRequestBase
{
private readonly IActor m_owner;
public RedisRequestPrivateBase(IActor entityBase, RedisConnector redisConnector)
: base(redisConnector)
{
m_owner = entityBase;
}
}

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServerCore; using ServerBase;
namespace ServerBase;
public abstract partial class RedisRequestSharedBase : RedisRequestBase
{
private readonly string m_shared_key;
public RedisRequestSharedBase(string sharedKey, RedisConnector redisConnector)
: base(redisConnector)
{
m_shared_key = sharedKey;
}
}

View File

@@ -0,0 +1,278 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using StackExchange.Redis;
using ServerCore; using ServerBase;
using REDIS_REQUEST_HANDLER_TYPE = System.UInt32;
using REDIS_KEY_FORMAT_TYPE = System.UInt32;
using REDIS_KEY_FORMAT = System.String;
using REDIS_KEY_PLACE_HOLDER = System.String;
using REDIS_KEY_REPLACE = System.String;
using REDIS_KEY = System.String;
namespace ServerBase;
public class RedisKeyFormat
{
public REDIS_KEY_FORMAT_TYPE KeyType { get; set; }
public string Format { get; set; } = string.Empty;
public RedisKeyFormat(REDIS_KEY_FORMAT_TYPE keyType, REDIS_KEY_FORMAT format)
{
KeyType = keyType;
Format = format;
}
}
public class RedisExecutionHandler<TConditionContext>
{
private readonly RedisConnector m_redis_connector;
private readonly List<RedisKeyFormat> m_template_formats = new();
public Func<TConditionContext, Task<Result>> checkCondition { get; }
public Func<REDIS_KEY[], object[], Task<IWithResult>> doAction { get; }
public RedisExecutionHandler( RedisConnector redisConnector
, Func<TConditionContext, Task<Result>> condition
, RedisKeyFormat[] redisKeyFormats
, Func<REDIS_KEY[], object[], Task<IWithResult>> action )
{
m_redis_connector = redisConnector;
checkCondition = condition;
m_template_formats.AddRange(redisKeyFormats);
doAction = action;
}
public REDIS_KEY[] replaceKey(Dictionary<REDIS_KEY_FORMAT_TYPE, Dictionary<REDIS_KEY_PLACE_HOLDER, REDIS_KEY_REPLACE>> formatTypeToReplaceParams)
{
ConditionValidCheckHelper.throwIfFalseWithCondition( () => m_template_formats.Count >= formatTypeToReplaceParams.Count
, () => $"Invalid Count of KeyFormats and ReplaceParam !!! : keyFormatsCount:{m_template_formats.Count} >= replaceParamCount:{formatTypeToReplaceParams.Count}");
var redis_keys = new List<REDIS_KEY>();
foreach(var each in formatTypeToReplaceParams)
{
var format_type = each.Key;
var place_holder_to_replace = each.Value;
m_template_formats.ForEach((templateformat) =>
{
if(format_type == templateformat.KeyType)
{
var resolved_key = TemplateReplacer.replace(templateformat.Format, place_holder_to_replace);
redis_keys.Add(resolved_key);
}
});
}
return redis_keys.ToArray();
}
}
public abstract class RedisRequestWithLambdaBase<TConditionContext>
{
private readonly RedisConnector m_redis_connector;
private readonly ConcurrentDictionary<REDIS_REQUEST_HANDLER_TYPE, RedisExecutionHandler<TConditionContext>> m_handlers = new();
public RedisRequestWithLambdaBase(RedisConnector redisConnector)
{
m_redis_connector = redisConnector;
}
public bool registerRequestHandler( REDIS_REQUEST_HANDLER_TYPE handlerType
, RedisKeyFormat[] redisKeyFormats
, Func<TConditionContext, Task<Result>> condition
, Func<REDIS_KEY[], object[], Task<IWithResult>> handler )
{
var redis_connector = getRedisConnector();
NullReferenceCheckHelper.throwIfNull(redis_connector, () => $"redis_connector is null !!! - {toBasicString()}");
if (false == m_handlers.TryAdd(handlerType, new RedisExecutionHandler<TConditionContext>( getRedisConnector()
, condition
, redisKeyFormats, handler ) ) )
{
Log.getLogger().error($"Failed to TryAdd() !!!, in registerRequestHandler(), Already registered : handlerType:{handlerType} - {toBasicString()}");
return false;
}
return true;
}
public bool registerRequestHandler( REDIS_REQUEST_HANDLER_TYPE handlerType
, RedisKeyFormat[] redisKeyFormats
, Func<REDIS_KEY[], object[], Task<IWithResult>> handler )
{
return registerRequestHandler( handlerType
, redisKeyFormats
, _ => Task.FromResult(new Result())
, handler );
}
public async Task<IWithResult> executeRequestHandlerByHandlerType( REDIS_REQUEST_HANDLER_TYPE handlerType
, TConditionContext conditionContext
, Dictionary<REDIS_KEY_FORMAT_TYPE, Dictionary<REDIS_KEY_PLACE_HOLDER, REDIS_KEY_REPLACE>> formatTypeToReplaceParams
, params object[] handlerParams )
{
var result = new Result();
var err_msg = string.Empty;
if(false == m_handlers.TryGetValue(handlerType, out var found_handler))
{
err_msg = $"Failed to TryGetValue() !!!, in executeHandlerByHandlerType(), Not found RedisExecutionHandler : handlerType:{handlerType} - {toBasicString()}";
result.setFail(ServerErrorCode.RedisRequestHandlerNotFound, err_msg);
Log.getLogger().error(result.toBasicString());
return new ResultOnly(result);
}
NullReferenceCheckHelper.throwIfNull(found_handler, () => $"found_handler is null !!! - {toBasicString()}");
result = await found_handler.checkCondition(conditionContext);
if(result.isFail())
{
err_msg = $"Failed to checkCondition() !!!, in executeHandlerByHandlerType() : : handlerType:{handlerType}, {result.toBasicString()} - {toBasicString()}";
Log.getLogger().error(result.toBasicString());
return new ResultOnly(result);
}
var redis_keys = found_handler.replaceKey(formatTypeToReplaceParams);
var with_result = await found_handler.doAction(redis_keys, handlerParams);
if(with_result.Result.isFail())
{
err_msg = $"Failed to doAction() !!!, in executeHandlerByHandlerType() : handlerType:{handlerType}, {with_result.Result.toBasicString()} - {toBasicString()}";
Log.getLogger().error(err_msg);
return with_result;
}
return with_result;
}
public async Task<IWithResult> executeRequestHandlerByHandlerType( REDIS_REQUEST_HANDLER_TYPE handlerType
, Dictionary<REDIS_KEY_FORMAT_TYPE, Dictionary<REDIS_KEY_PLACE_HOLDER, REDIS_KEY_REPLACE>> formatTypeToReplaceParams
, params object[] handlerParams)
{
var result = new Result();
var err_msg = string.Empty;
if(false == m_handlers.TryGetValue(handlerType, out var found_handler))
{
err_msg = $"Failed to TryGetValue() !!!, in executeHandlerByHandlerType(), Not found RedisExecutionHandler : handlerType:{handlerType} - {toBasicString()}";
result.setFail(ServerErrorCode.RedisRequestHandlerNotFound, err_msg);
Log.getLogger().error(result.toBasicString());
return new ResultOnly(result);
}
NullReferenceCheckHelper.throwIfNull(found_handler, () => $"found_handler is null !!! - {toBasicString()}");
var redis_keys = found_handler.replaceKey(formatTypeToReplaceParams);
var with_result = await found_handler.doAction(redis_keys, handlerParams);
if(with_result.Result.isFail())
{
err_msg = $"Failed to doAction() !!!, in executeHandlerByHandlerType() : handlerType:{handlerType}, {result.toBasicString()} - {toBasicString()}";
Log.getLogger().error(err_msg);
return with_result;
}
return with_result;
}
public async Task<IWithResult> executeRequestHandlerByHandlerType( REDIS_REQUEST_HANDLER_TYPE handlerType
, REDIS_KEY[] redisKeys
, params object[] handlerParams)
{
var result = new Result();
var err_msg = string.Empty;
if(false == m_handlers.TryGetValue(handlerType, out var found_handler))
{
err_msg = $"Failed to TryGetValue() !!!, in executeHandlerByHandlerType(), Not found RedisExecutionHandler : handlerType:{handlerType} - {toBasicString()}";
result.setFail(ServerErrorCode.RedisRequestHandlerNotFound, err_msg);
Log.getLogger().error(result.toBasicString());
return new ResultOnly(result);
}
NullReferenceCheckHelper.throwIfNull(found_handler, () => $"found_handler is null !!! - {toBasicString()}");
var with_result = await found_handler.doAction(redisKeys, handlerParams);
if(with_result.Result.isFail())
{
err_msg = $"Failed to doAction() !!!, in executeHandlerByHandlerType() : handlerType:{handlerType}, {with_result.Result.toBasicString()} - {toBasicString()}";
Log.getLogger().error(err_msg);
return with_result;
}
return with_result;
}
public async Task<(bool, Dictionary<REDIS_REQUEST_HANDLER_TYPE, IWithResult>)> executeRequestHanlderAllByCondition( TConditionContext conditionContext
, Dictionary<REDIS_KEY_FORMAT_TYPE, Dictionary<REDIS_KEY_PLACE_HOLDER, REDIS_KEY_REPLACE>> formatTypeToReplaceParams
, params object[] handlerParams )
{
var is_success = false;
var err_msg = string.Empty;
var results = new Dictionary<REDIS_REQUEST_HANDLER_TYPE, IWithResult>();
foreach (var each in m_handlers)
{
var handler_type = each.Key;
var handler = each.Value;
var result_check = await handler.checkCondition(conditionContext);
if (result_check.isFail())
{
err_msg = $"Failed to checkCondition() !!!, in executeFunctionAllByCondition() : : handlerType:{handler_type}, {result_check.toBasicString()} - {toBasicString()}";
Log.getLogger().debug(err_msg);
continue;
}
var redis_keys = handler.replaceKey(formatTypeToReplaceParams);
var with_result = await handler.doAction(redis_keys, handlerParams);
if (with_result.Result.isFail())
{
err_msg = $"Failed to doAction() !!!, in executeFunctionAllByCondition() : handlerType:{handler_type}, {with_result.Result.toBasicString()} - {toBasicString()}";
Log.getLogger().error(err_msg);
continue;
}
results.Add(handler_type, with_result);
is_success = true;
}
return (is_success, results);
}
public REDIS_KEY[] replaceByHandlerType( REDIS_REQUEST_HANDLER_TYPE handlerType
, Dictionary<REDIS_KEY_FORMAT_TYPE, Dictionary<REDIS_KEY_PLACE_HOLDER, REDIS_KEY_REPLACE>> formatTypeToReplaceParams)
{
var err_msg = string.Empty;
if (false == m_handlers.TryGetValue(handlerType, out var found_handler))
{
err_msg = $"Failed to TryGetValue() !!!, in resolveByHandlerType(), Not found RedisExecutionHandler : handlerType:{handlerType} - {toBasicString()}";
Log.getLogger().error(err_msg);
return Array.Empty<REDIS_KEY>();
}
NullReferenceCheckHelper.throwIfNull(found_handler, () => $"found_handler is null !!! - {toBasicString()}");
return found_handler.replaceKey(formatTypeToReplaceParams);
}
public RedisConnector getRedisConnector() => m_redis_connector;
public string toBasicString()
{
return $"redisRequestor:{this.getTypeName()}";
}
}

View File

@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using ServerCore;
namespace ServerBase;
public partial class RedisWithLuaScriptExecutor : RedisWithLuaScriptExecutorBase, IModule
{
public class ConfigParam : IConfigParam
{
public string ServiceType { get; set; } = string.Empty;
public Result tryReadFromJsonOrDefault(JObject jObject)
{
var result = new Result();
var err_msg = string.Empty;
try
{
ServiceType = jObject["ServiceType"]?.Value<string>() ?? string.Empty;
return result;
}
catch (Exception e)
{
var error_code = ServerErrorCode.TryCatchException;
err_msg = $"Exception !!!, Failed to perform in tryReadFromJsonOrDefault() !!! : errorCode:{error_code}, exception:{e} - {this.getTypeName()}";
result.setFail(error_code, err_msg);
Log.getLogger().error(err_msg);
return result;
}
}
public string toBasicString()
{
return $"ConfigParam: ServiceType:{ServiceType}";
}
};
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServerCore;
namespace ServerBase;
public partial class RedisWithLuaScriptExecutor : RedisWithLuaScriptExecutorBase, IModule
{
private readonly ModuleContext m_module_context;
public RedisWithLuaScriptExecutor(RedisConnector redisConnector, ModuleContext moduleContext)
: base(redisConnector)
{
m_module_context = moduleContext;
}
public async Task<Result> startModule()
{
var result = new Result();
return await Task.FromResult(result);
}
public async Task<Result> stopModule()
{
var result = new Result();
return await Task.FromResult(result);
}
public ModuleContext getModuleContext() => m_module_context;
public string toBasicString()
{
return $"{this.getTypeName()}";
}
}