コード例 #1
0
        /// <summary>
        /// Find is getting data from remote (redis) storage. After this
        /// it writes updated version of data into locaL (litedb) storage
        /// </summary>
        /// <param name="key"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private T Find <T>(string key)
        {
            var liteDbService = new LiteDbService();
            var dynamicConfig = new DynamicConfig
            {
                ApplicationName = ApplicationName,
                Name            = key,
                IsActive        = 1
            };

            var configurationReaderFactory = new ConfigurationServiceFactory();
            var reader = configurationReaderFactory.ProduceReader("Redis", ConnectionString);

            var redisResult = reader.Read <DynamicConfig>(dynamicConfig);

            // If redis result fits with requested data type
            if (redisResult?.Value is T)
            {
                liteDbService.RemoveConfig(key);
                liteDbService.AddConfig(redisResult);
                return((T)Convert.ChangeType(redisResult.Value, typeof(T)));
            }

            return(default(T));
        }
コード例 #2
0
        /// <summary>
        /// ConfigJob is the definition of scheduled job of worker.
        /// It's comparing local (litedb) data with remote (redis) storage data. Also it's
        /// making neccessary updates on local.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="refreshTimerIntervalInMs"></param>
        /// <returns></returns>
        private async Task <bool> ConfigJob(string key, TimeSpan refreshTimerIntervalInMs)
        {
            var isCancelled = _cancellationToken.IsCancellationRequested;

            // if task not cancelled
            if (!isCancelled)
            {
                await Task.Delay(refreshTimerIntervalInMs, _cancellationToken);

                var liteDbService = new LiteDbService();
                var dynamicConfig = new DynamicConfig
                {
                    ApplicationName = ApplicationName,
                    Name            = key,
                    IsActive        = 1
                };

                var configurationReaderFactory = new ConfigurationServiceFactory();
                var reader = configurationReaderFactory.ProduceReader("Redis", ConnectionString);

                var redisResult  = reader.Read <DynamicConfig>(dynamicConfig);
                var liteDbResult = liteDbService.GetConfig(key);

                if (redisResult != null && liteDbResult != null)
                {
                    // Comparing data changes
                    if (redisResult.Value != liteDbResult.Value)
                    {
                        liteDbService.RemoveConfig(key);
                        liteDbService.AddConfig(redisResult);
                        return(true);
                    }
                }
            }

            return(false);
        }