//TODO: need to get settings by user
        public SettingDto GetSettingDto()
        {
            var settingDto = _applicationCasheService.GetValue(ApplicationCasheNames.SettingDto);

            if (settingDto == null)
            {
                var settings = _settingProvider.GetSettings();

                var altRowsColor = settings.ContainsKey(SettingNames.AltRowsColor)
                    ? _serializationService.DeserializeObject <Color>(settings[SettingNames.AltRowsColor])
                    : ColorDefault;

                var dateFormateId = settings.ContainsKey(SettingNames.DateTimeFormat)
                    ? _serializationService.DeserializeObject <int>(settings[SettingNames.DateTimeFormat])
                    : DateTimeFormatDefault;

                var pageSize = settings.ContainsKey(SettingNames.PageSize)
                    ? _serializationService.DeserializeObject <int>(settings[SettingNames.PageSize])
                    : PageSizeDefault;

                var dateFormate = _settingProvider.GetDateFormat(dateFormateId);

                settingDto = new SettingDto
                {
                    DateFormateId    = dateFormateId,
                    AltRowsColor     = altRowsColor,
                    AltRowsColorName = altRowsColor.ToString(),
                    DateTimeFormat   = dateFormate.DateTimeFormat,
                    DateFormat       = dateFormate.ShortDateFormat.ToUpperInvariant(),
                    PageSize         = pageSize
                };

                _applicationCasheService.AddValue(ApplicationCasheNames.SettingDto, settingDto);
            }

            return((SettingDto)settingDto);
        }
Beispiel #2
0
        /// <summary>
        /// Считывание данных из файла
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public async Task <List <T> > ReadFile <T>(string fileName) where T : BaseModel
        {
            try
            {
                //Получение JSON`а из файла
                var json = File.ReadAllText(GetPath(fileName));
                //Преобразование JSON в тип T
                var data = _serializationService.DeserializeObject <List <T> >(json);

                return(data != null ? data : new List <T>());
            }
            catch (Exception ex)
            {
                return(new List <T>());
            }
        }
        /// <summary>
        /// Converts a string found in cache to an object
        /// </summary>
        /// <typeparam name="T">Object type to convert to</typeparam>
        /// <param name="stringToConvert">String to convert to an object</param>
        /// <param name="key">The key that was used to get the value</param>
        /// <param name="serializationSettings">The datacontract resolver to use for serialization
        /// (polymorphic dtos)</param>
        /// <returns>Object from string</returns>
        protected internal virtual T ConvertString <T>(string stringToConvert, RedisId key,
                                                       ISerializationSettings serializationSettings = null)
        {
            T retVal = default(T);

            if (string.IsNullOrWhiteSpace(stringToConvert))
            {
                return(retVal);
            }

            if (typeof(T) == typeof(string))
            {
                retVal = (T)Convert.ChangeType(stringToConvert, typeof(T));
            }
            else
            {
                ISerializationService serializationService = GetSerializationService <T>();
                retVal = serializationService.DeserializeObject <T>(stringToConvert, _cacheSettings.SerializationSettings);
            }

            return(retVal);
        }
        public IEnumerable <RedisCachedObject <T> > GetValues <T>(IEnumerable <RedisId> keys)
        {
            if (!(keys?.Any() ?? false))
            {
                return(new RedisCachedObject <T> [0]);
            }

            RedisId[] keysToUse = keys.ToArray();
            for (int i = 0; i < keysToUse.Count(); i++)
            {
                if (keysToUse[i].ObjectIdentifier.EndsWith("*"))
                {
                    continue;
                }
                keysToUse[i].ObjectIdentifier = keysToUse[i].ObjectIdentifier + "*";
            }

            IEnumerable <RedisId> redisKeys = GetKeys <T>(keys);

            if (!(redisKeys?.Any() ?? false))
            {
                return(new RedisCachedObject <T> [0]);
            }

            RedisValue[] results = ServiceGetValues <T>(redisKeys).ToArray();
            List <RedisCachedObject <T> > values = new List <RedisCachedObject <T> >(results.Length);

            foreach (RedisValue result in results)
            {
                RedisCachedObject <T>      current;
                RedisCachedObject <string> stringValue = RedisService.CacheProvider.GetCachedValue <string>(result, this);
                if (typeof(T) == typeof(object) || typeof(T) == typeof(string))
                {
                    current = RedisService.CacheProvider.ChangeType <T, string>(stringValue);
                }
                else if (typeof(T).IsPrimitive)
                {
                    current = RedisService.CacheProvider.ChangeType <T, string>(stringValue);
                }
                else
                {
                    ISerializationService sz =
                        RedisService.GetSerializationService <T>();
                    current = new RedisCachedObject <T>
                    {
                        RetrievedSuccesfully = stringValue.RetrievedSuccesfully,
                        Value      = sz.DeserializeObject <T>(stringValue.Value, RedisService.CacheSettings.SerializationSettings),
                        CachedTime = stringValue.CachedTime,
                        Status     = stringValue.Status,
                        Metadata   = stringValue.Metadata,
                        Id         = stringValue.Id,
                        ExpireTime = stringValue.ExpireTime,
                    };
                }
                if (current != null)
                {
                    values.Add(current);
                }
            }

            return(ServiceValuePostProcess(values));
        }
        protected virtual RedisCachedObject <T> GetCachedValue <T>(RedisValue redisValue, IRedisService service, bool first)
        {
            string decompressed = null;

            if (default(RedisValue) == redisValue)
            {
                return(CreateCachedValue <T>());
            }
            if (CacheSettings.ServiceSettings.CompressValues)
            {
                decompressed = DecompressValue(redisValue);
            }
            if (default(RedisValue) == redisValue)
            {
                return(CreateCachedValue <T>());
            }
            decompressed = decompressed ?? redisValue;
            RedisCachedObject <T> value;

            if (!first)
            {
                ISerializationService sz = CacheSettings.SerializationService;
                try
                {
                    return(sz.DeserializeObject <RedisCachedObject <T> >(decompressed, CacheSettings.SerializationSettings));
                }
                catch (SerializationException)
                {
                    return(CreateCachedValue <T>());
                }
            }

            RedisCachedObject <string> stringValue = GetCachedValue <string>(redisValue, service, false);

            if (typeof(T) == typeof(object) || typeof(T) == typeof(string))
            {
                value = ChangeType <T, string>(stringValue);
            }
            else if (typeof(T).IsPrimitive)
            {
                value = ChangeType <T, string>(stringValue);
            }
            else
            {
                ISerializationService sz = CacheSettings.SerializationService;
                value = new RedisCachedObject <T>
                {
                    RetrievedSuccesfully = stringValue.RetrievedSuccesfully,
                    Value      = sz.DeserializeObject <T>(stringValue.Value, CacheSettings.SerializationSettings),
                    CachedTime = stringValue.CachedTime,
                    Status     = stringValue.Status,
                    Metadata   = stringValue.Metadata,
                    Id         = stringValue.Id,
                    ExpireTime = stringValue.ExpireTime,
                };
            }

            if (value != null && value.ExpireTime < DateTime.UtcNow)
            {
                if (value.Id != null)
                {
                    service.DeleteValue(value.Id);
                }
                return(CreateCachedValue <T>());
            }

            return(value);
        }