private async Task <bool> SetCacheType(Nuid id, string entity_type) { IRedisDatabase db = GetCache(id); string key = CacheUtils.BuildEntities(id); return(await db.Database.HashSetAsync(key, id.Unique, entity_type)); }
private async Task <long> FindCacheRow <T>(Nuid id, string table_name, int col, T value) { if (!await CacheExist(id)) { return(Global.INVALID_ROW); } long row = Global.INVALID_ROW; try { IRedisDatabase db = GetCache(id); string key = CacheUtils.BuildTable(id, table_name); Dictionary <string, NList> row_values = await db.HashGetAllAsync <NList>(key); foreach (KeyValuePair <string, NList> pair in row_values) { if (pair.Value.Get <T>(col).Equals(value)) { row = long.Parse(pair.Key); break; } } } catch (Exception ex) { _Logger.LogError(ex, "'{0} FindCacheRow error for table={1}", id, table_name); } return(row); }
public void Init() { var services = new ServiceCollection(); var redisConfig = new RedisConfiguration { MaxValueLength = 2810, PoolSize = 20, AbortOnConnectFail = false, Database = 0, KeyPrefix = "lyl:", ConnectTimeout = 100, Hosts = new RedisHost[] { new RedisHost { Host = "198.185.15.16", Port = 6379 } }, }; redisConfig.ServerEnumerationStrategy = new ServerEnumerationStrategy() { Mode = ServerEnumerationStrategy.ModeOptions.All, TargetRole = ServerEnumerationStrategy.TargetRoleOptions.Any, UnreachableServerAction = ServerEnumerationStrategy.UnreachableServerActionOptions.Throw }; IRedisCacheConnectionPoolManager poolManager = new RedisCacheConnectionPoolManager(redisConfig); _redisDatabase = new RedisDatabase(connectionPoolManager: poolManager, new SystemTextJsonSerializer(), redisConfig.ServerEnumerationStrategy, 0, 0, redisConfig.KeyPrefix); }
/// <summary> /// /// </summary> /// <param name="dbcontext"></param> /// <param name="configuration"></param> /// <param name="log"></param> /// <param name="redis"></param> public UsersController(MyDbContext dbcontext, IConfiguration configuration, ILogger <UsersController> log, IRedisCacheClient redis) { _DbContext = dbcontext; _Configuration = configuration; _Log = log; _Redis = redis.GetDbFromConfiguration(); }
public async Task <CacheResponse> GetAsyncDownstream(string name) { IRedisDatabase redis = _cache.GetDbFromConfiguration(); string result = "", cacheStatus = "rediscache HIT for " + name; try { result = await redis.GetAsync <string>(name); if (result == null) { result = await DoDownStreamCall(); await redis.AddAsync <string>(name, result, DateTimeOffset.Now.AddMinutes(1)); cacheStatus = "rediscache MISS for " + name; } } catch (RedisConnectionException e) { _logger.LogWarning("Failed to connect to redis, falling back to backend", e); result = await DoDownStreamCall(); cacheStatus = "rediscache exception"; } return(new CacheResponse(result, cacheStatus));//"rediscache miss for " + name); }
public DishController( IRedisDatabase redisDatabase, IDishService dishService) { _redisDatabase = redisDatabase; _dishService = dishService; }
/// <summary> /// 如果不存在缓存项则添加,否则更新 /// </summary> /// <param name="key">缓存项标识</param> /// <param name="value">缓存项</param> /// <param name="timeSpan">缓存失效时间</param> public async Task SetAsync(string key, object value, TimeSpan timeSpan) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (value == null) { throw new ArgumentNullException(nameof(value)); } if (timeSpan == null) { throw new ArgumentNullException(nameof(timeSpan)); } key = key.ToLower(); if (value != null) { IRedisDatabase database = this.cache.Db0; if (database == null) { throw new NullReferenceException(nameof(database)); } await database.AddAsync(key, value, DateTimeOffset.Now.AddMinutes(10)); } }
/// <summary> /// Gets the item from the Cache or invokes given delegate to populate cache and return /// </summary> public static async Task <IQueryable <T> > GetOrCacheAside <T>( this IRedisDatabase cache, Func <IQueryable <T> > fetchDelegate, string hashKey, string hashPattern = "*", Func <T, string> keyBuilder = null, ILogger logger = null, IFeatureManager features = null) where T : ICacheable { if (features != null && !(await features.IsEnabledAsync(nameof(Features.Caching)))) { logger?.LogInformation("Caching was disabled"); return(fetchDelegate.Invoke()); } if (keyBuilder == null) { keyBuilder = x => x.ToCacheKeyString(); } logger?.LogDebug("Retrieving from cache"); var items = cache.HashScan <T>(hashKey, hashPattern).Select(x => x.Value).AsQueryable(); if (!items.Any()) { logger?.LogInformation("No Items were found in the cache, fetching and setting into cache"); items = fetchDelegate.Invoke(); await cache.HashSetAsync(hashKey, items.ToDictionary(keyBuilder, x => x)); } logger?.LogDebug($"Retrieving {items.Count()} from cache"); return(items); }
public RabbitMQEventBusEngine(IOptions <RabbitMQEngineOptions> options, ILoggerFactory loggerFactory, IRabbitMQConnectionManager connectionManager, IRedisDatabase redis) { _logger = loggerFactory.CreateLogger <RabbitMQEventBusEngine>(); _options = options.Value; _connectionManager = connectionManager; _redis = redis; //publish ILogger publishTaskManagerLogger = loggerFactory.CreateLogger <PublishTaskManager>(); foreach (RabbitMQConnectionSetting connectionSetting in _options.ConnectionSettings) { _publishManagers.Add(connectionSetting.BrokerName, new PublishTaskManager(connectionSetting, _connectionManager, _redis, publishTaskManagerLogger)); } //publish history ILogger historyTaskManagerLogger = loggerFactory.CreateLogger <HistoryTaskManager>(); foreach (RabbitMQConnectionSetting connectionSetting in _options.ConnectionSettings) { _historyManager.Add(connectionSetting.BrokerName, new HistoryTaskManager(connectionSetting, _connectionManager, _redis, historyTaskManagerLogger)); } //Consume _consumeTaskManagerLogger = loggerFactory.CreateLogger <ConsumeTaskManager>(); }
private async Task <string> GetCacheType(Nuid id) { IRedisDatabase db = GetCache(id); string key = CacheUtils.BuildEntities(id); return(await db.HashGetAsync <string>(key, id.Unique.ToString())); }
private async Task <bool> CacheExist(Nuid id) { IRedisDatabase db = GetCache(id); string key = CacheUtils.BuildEntities(id); return(await db.HashExistsAsync(key, id.Unique.ToString())); }
/// <summary> /// Using Extensions /// </summary> /// <param name="db"></param> /// <returns></returns> private static async Task GenerateClocks(IRedisDatabase db) { var sw = Stopwatch.StartNew(); var allClocks = new Dictionary <string, ClockInfo>(); var random = new Random(); for (var i = 0; i < Total; i++) { var partition = $"GT-{i}"; var clock = new ClockInfo { DeviceId = partition, SerialNumber = partition.ToLower(), ClientId = random.Next(10000, 99999), ClockGroupId = Guid.NewGuid() }; allClocks.Add(partition, clock); } await db.HashSetAsync("clocksPartition1", allClocks); await db.UpdateExpiryAsync("clocksPartition1", DateTimeOffset.Now.AddMinutes(5)); Console.WriteLine($"Storing {Total} (v2): {Math.Round(sw.Elapsed.TotalMilliseconds)}ms"); }
static async Task ObjectGetAsync(IRedisDatabase cache, string key) { Console.WriteLine($"Cache command : Get {key}"); var employee = await cache.GetAsync <Employee>(key); Console.WriteLine("Cache response : " + employee.Name); }
private static async Task LookupAllItems(IRedisDatabase db) { var sw = Stopwatch.StartNew(); var allClocks = await db.HashGetAllAsync <ClockInfo>("clocksPartition1"); Console.WriteLine( $"Get {allClocks.Count} Items back as List (v2): {Math.Round(sw.Elapsed.TotalMilliseconds)}ms"); }
public RmqlBackgroundService(IServiceProvider serviceProvider) { _configurations = Activator.CreateInstance <T>(); _serviceScope = serviceProvider.CreateScope(); _redisDatabase = _serviceScope.ServiceProvider .GetRequiredService <IRedisCacheClient>() .GetDb(_configurations.RedisDb); }
/// <summary> /// /// </summary> /// <param name="remoteip"></param> /// <param name="ws"></param> /// <param name="allsessions"></param> /// <param name="dbcontext"></param> /// <param name="ran"></param> /// <param name="redis"></param> /// <param name="mapaccessqueue"></param> public Session(string remoteip, WebSocket ws, List <Session> allsessions, MyDbContext dbcontext, Random ran, IRedisDatabase redis, TaskQueue mapaccessqueue) { _RemoteIP = remoteip; _WS = ws; _Ran = ran; _AllSession = allsessions; _DbContext = dbcontext; _Redis = redis; _MapAccessQueue = mapaccessqueue; }
private const int DEFAULT_LOCK_TTL = 300000; // 300 seconds. /// <summary> /// A general purpose ID locking system that utilizes Redis as its backend storage medium. This is just an in memory store /// that can be used to determine if some object (based upon its type (name) and unique identifier) are being used elsewhere /// in the system and thus cannot be accessed. /// </summary> /// <param name="redisCacheClient">Reference to a RedisCacheClient that can be used to communicate with the Redis infrastructure </param> /// <param name="redisDatabaseNumber">The number of the database that should be used to store locks in. It is recommended, but not required /// that locks be stored in a database all to their own. The reason, is if all locks need to be flushed it can be much faster to flush the /// entire Database than to work thru all the lock types.</param> /// <param name="isDedicatedLockDatabase">Set to True, if the database to be used for storing locks is dedicated to this use only or if it /// is shared with other uses (caching values, etc). There is a slight performance boost if using dedicated. </param> public RedisLocker(IRedisCacheClient redisCacheClient, byte redisDatabaseNumber = 0, bool isDedicatedLockDatabase = false) { _redisDB = redisCacheClient.GetDb(redisDatabaseNumber); _isDedicatedLockDatabase = isDedicatedLockDatabase; // Clear the Lock Prefix if this is a dedicated database - no need for it, since everything in the database is a lock entry. if (isDedicatedLockDatabase) { LockPrefix = ""; } }
public ConsumeTaskManager(string eventType, IEventHandler eventHandler, RabbitMQConnectionSetting connectionSetting, IRabbitMQConnectionManager connectionManager, IRedisDatabase redis, ILogger logger) { _redis = redis; _eventType = eventType.ThrowIfNull(nameof(eventType)); _handler = eventHandler.ThrowIfNull(nameof(eventHandler)); _logger = logger.ThrowIfNull(nameof(logger)); _connectionManager = connectionManager.ThrowIfNull(nameof(connectionManager)); _connectionSetting = connectionSetting.ThrowIfNull(nameof(connectionSetting)); Restart(); }
protected RabbitMQAndDistributedQueueDynamicTaskManager(RabbitMQConnectionSetting connectionSetting, IRabbitMQConnectionManager connectionManager, IRedisDatabase redis, ILogger logger) { _logger = logger; _connectionManager = connectionManager; _redis = redis; _connectionSetting = connectionSetting; int taskCount = EstimatedTaskNumber; AddTaskAndStart(taskCount > MaxWorkerThread() ? MaxWorkerThread() : taskCount); }
private async Task Setup() { var redisSetup = new Setup(this.logger); this.logger.Information("Initializing the services setup..."); services = new ServiceCollection().AddStackExchangeRedisExtensions <NewtonsoftSerializer>(redisSetup.RedisConfiguration); serviceProvider = services.BuildServiceProvider(); redis = serviceProvider.GetService <IRedisDatabase>(); reactionService = new ReactionService(this.logger, redis); infractionService = new InfractionService(this.logger, redis); logService = new LogService(this.logger, redis); services.AddSingleton(this.logger) .AddSingleton(reactionService) .AddSingleton(infractionService) .AddSingleton(logService); serviceProvider = services.BuildServiceProvider(); this.logger.Information("Successfully setup the services."); config = await redis.GetAsync <Config>(RedisKeyNaming.Config).ConfigureAwait(true); if (config == null || string.IsNullOrWhiteSpace(config.Token)) { this.logger.Information("Initializing the bot token setup..."); Console.Write("Your bot token: "); var token = Console.ReadLine(); if (config == null) { config = new Config() { Token = token }; await redis.AddAsync(RedisKeyNaming.Config, config).ConfigureAwait(true); } else if (string.IsNullOrWhiteSpace(config.Token)) { config.Token = token; await redis.ReplaceAsync(RedisKeyNaming.Config, config).ConfigureAwait(true); } this.logger.Information("Successfully set the bot token into the database."); } else { this.logger.Information("Discord token is already set and will be used from the database."); } OpenCL.IsEnabled = false; this.logger.Information("Disabled GPU acceleration."); await Task.CompletedTask.ConfigureAwait(true); }
private async Task <T> GetCacheField <T>(Nuid id, string field_name) { if (!await CacheExist(id)) { return(default(T)); } IRedisDatabase db = GetCache(id); string key = CacheUtils.BuildFields(id); return(await db.HashGetAsync <T>(key, field_name)); }
private async Task <NList> GetCacheRowValue(Nuid id, string table_name, long row) { if (!await CacheExist(id)) { return(NList.Empty); } IRedisDatabase db = GetCache(id); string key = CacheUtils.BuildTable(id, table_name); return(await db.HashGetAsync <NList>(key, row.ToString())); }
private async Task <NList> GetCacheTableKeyValue <TPrimaryKey>(Nuid id, string table_name, TPrimaryKey primary_key) { if (!await CacheExist(id)) { return(NList.Empty); } IRedisDatabase db = GetCache(id); string key = CacheUtils.BuildTable(id, table_name); return(await db.HashGetAsync <NList>(key, primary_key.ToString())); }
public StaticDataPublicationService(IRedisDatabase redisDatabase, ILogger <StaticDataPublicationService> logger, IOptions <StaticDataPublicationOptions> updateOptions) { _countriesStreamName = updateOptions.Value.CountriesStreamName; _localitiesStreamName = updateOptions.Value.LocalitiesStreamName; _localityZonesStreamName = updateOptions.Value.LocalityZonesStreamName; _accommodationsStreamName = updateOptions.Value.AccommodationsStreamName; _logger = logger; _database = redisDatabase.Database; InitStreams(); }
public ClientEventHandler(DiscordShardedClient client, ILogger logger, IRedisDatabase redis, IReactionService reactionService, ILogService logService) { this.client = client; this.logger = logger; this.reactionService = reactionService; this.logService = logService; this.redis = redis; this.logger.Information("Register events..."); this.client.Ready += Ready; this.client.Resumed += Resumed; this.client.GuildDownloadCompleted += GuildDownloadCompleted; this.client.GuildAvailable += GuildAvailable; this.client.GuildUnavailable += GuildUnavailable; this.client.GuildCreated += GuildCreated; this.client.GuildUpdated += GuildUpdated; this.client.GuildDeleted += GuildDeleted; this.client.GuildEmojisUpdated += GuildEmojisUpdated; this.client.GuildIntegrationsUpdated += GuildIntegrationsUpdated; this.client.ChannelCreated += ChannelCreated; this.client.ChannelDeleted += ChannelDeleted; this.client.ChannelUpdated += ChannelUpdated; this.client.GuildRoleCreated += GuildRoleCreated; this.client.GuildRoleUpdated += GuildRoleUpdated; this.client.GuildRoleDeleted += GuildRoleDeleted; this.client.GuildMemberAdded += GuildMemberAdded; this.client.GuildMemberRemoved += GuildMemberRemoved; this.client.GuildMemberUpdated += GuildMemberUpdated; this.client.GuildMembersChunked += GuildMemberChunked; this.client.GuildBanAdded += GuildBanAdded; this.client.GuildBanRemoved += GuildBanRemoved; this.client.InviteCreated += InviteCreated; this.client.InviteDeleted += InviteDeleted; this.client.DmChannelCreated += DmChannelCreated; this.client.DmChannelDeleted += DmChannelDeleted; this.client.MessageUpdated += MessageUpdated; this.client.MessageDeleted += MessageDeleted; this.client.MessagesBulkDeleted += MessagesBulkDeleted; this.client.MessageReactionAdded += MessageReactionAdded; this.client.MessageReactionRemoved += MessageReactionRemoved; this.client.MessageReactionRemovedEmoji += MessageReactionRemovedEmoji; this.client.MessageReactionsCleared += MessageReactionsCleared; this.client.VoiceServerUpdated += VoiceServerUpdated; this.client.VoiceStateUpdated += VoiceStateUpdated; this.client.SocketOpened += SocketOpened; this.client.SocketClosed += SocketClosed; this.client.SocketErrored += SocketErrored; this.client.WebhooksUpdated += WebhooksUpdated; this.client.ClientErrored += ClientErrored; this.client.UnknownEvent += UnknownEvent; this.logger.Information("Registered all events!"); }
public RedisInformationMiddleware( RequestDelegate next, RedisMiddlewareAccessOptions options, ILogger <RedisInformationMiddleware> logger, IRedisCacheConnectionPoolManager connectionPoolManager, IRedisDatabase redisDatabase) { this.next = next; this.options = options; this.logger = logger; this.connectionPoolManager = connectionPoolManager; this.redisDatabase = redisDatabase; }
public static RedisCacheProvider CreateInstance(IConfiguration configuration, ISerializer sz = null) { if (_cachedb == null) { _cachedb = RedisConnectManger.Instance(configuration, sz); } if (sz == null) { sz = new MsgPackObjectSerializer(); } _serializer = sz; return(new RedisCacheProvider()); }
public static async Task <Guild> InitGuild(this IRedisDatabase redis, ulong guildId) { var guild = await redis.GetAsync <Guild>(RedisKeyNaming.Guild(guildId)); if (guild != null) { return(guild); } guild = new Guild { Id = guildId, Prefix = ApplicationInformation.DefaultPrefix, ModeratorRoleIds = new List <ulong>(), AllowWarnModerators = false, AllowMuteModerators = false, Logs = new List <Log>(), Settings = new List <Setting>(), ReactionCategories = new List <ReactionCategory>(), ReactionMessages = new List <ReactionMessage>() }; await redis.AddAsync <Guild>(RedisKeyNaming.Guild(guildId), guild); if (string.IsNullOrWhiteSpace(guild.Prefix)) { guild.Prefix = ApplicationInformation.DefaultPrefix; } guild.ModeratorRoleIds ??= new List <ulong>(); if (guild.AllowWarnModerators != true && guild.AllowWarnModerators != false) { guild.AllowWarnModerators = false; } if (guild.AllowMuteModerators != true && guild.AllowMuteModerators != false) { guild.AllowMuteModerators = false; } guild.Logs ??= new List <Log>(); guild.Settings ??= new List <Setting>(); guild.ReactionCategories ??= new List <ReactionCategory>(); guild.ReactionMessages ??= new List <ReactionMessage>(); await redis.ReplaceAsync <Guild>(RedisKeyNaming.Guild(guildId), guild); return(guild); }
public RedisCache() { var configuration = ConfigurationManagerExtension.GetSection("redisCacheClient") as RedisCachingSectionHandler; if (configuration == null) { throw new ConfigurationErrorsException("Unable to locate <redisCacheClient> section into your configuration file. Take a look https://github.com/imperugo/StackExchange.Redis.Extensions"); } var redisConfiguration = RedisCachingSectionHandler.GetConfig(); var connectionPoolManager = new RedisCacheConnectionPoolManager(redisConfiguration); _redis = new RedisCacheClient(connectionPoolManager, new NewtonsoftSerializer(), redisConfiguration).GetDbFromConfiguration(); }
public static async Task <bool> IsModerator(this IRedisDatabase redis, ulong guildId, DiscordMember member) { var guild = await redis.GetAsync <Guild>(RedisKeyNaming.Guild(guildId)); var isModerator = false; foreach (var role in member.Roles) { isModerator = guild.ModeratorRoleIds.Any(x => x == role.Id); if (isModerator) { break; } } return(isModerator); }
public RedisClient(string hostOrIP, int port, int dbIndex = 0) { mDb = new RedisDatabase(hostOrIP, port, dbIndex); }
public RateRepository(IRedisDatabase redisDatabase) { _redisDatabase = redisDatabase; }