Example #1
0
        /// <summary>
        /// Поместить данные в хранилище.
        /// </summary>
        /// <typeparam name="T">Тип данных.</typeparam>
        /// <param name="key">Ключ.</param>
        /// <param name="data">Данные.</param>
        /// <returns>Признак успешности выполнения.</returns>
        public bool PutData <T>(CoreCachingKey key, T data)
        {
            var dataKey = Helper.ConvertToDataKey(key.Name);

            var isOk = PutDataToDatabase(dataKey, data);

            if (isOk)
            {
                var expiry = key.GetCacheExpiryTimeSpan(Helper.ExpiryInSeconds);

                if (expiry.HasValue)
                {
                    var db = Helper.GetDatabase();

                    isOk = db.KeyExpire(dataKey, expiry);
                }

                if (isOk)
                {
                    var tagKeys = Helper.ConvertToTagKeys(key.Tags);

                    BindDataKeyToTagKeys(tagKeys, dataKey);
                }
            }

            return(isOk);
        }
Example #2
0
        /// <summary>
        /// Асинхронно поместить данные в хранилище.
        /// </summary>
        /// <typeparam name="T">Тип данных.</typeparam>
        /// <param name="key">Ключ.</param>
        /// <param name="data">Данные.</param>
        /// <returns>Задача с признаком успешности выполнения.</returns>
        public async Task <bool> PutDataAsync <T>(CoreCachingKey key, T data)
        {
            var dataKey = Helper.ConvertToDataKey(key.Name);

            var isOk = await PutDataToDatabaseAsync(dataKey, data).CoreBaseExtTaskWithCurrentCulture(false);

            if (isOk)
            {
                var expiry = key.GetCacheExpiryTimeSpan(Helper.ExpiryInSeconds);

                if (expiry.HasValue)
                {
                    var db = Helper.GetDatabase();

                    isOk = await db.KeyExpireAsync(dataKey, expiry).CoreBaseExtTaskWithCurrentCulture(false);
                }

                if (isOk)
                {
                    var tagKeys = Helper.ConvertToTagKeys(key.Tags);

                    await BindDataKeyToTagKeysAsync(tagKeys, dataKey).CoreBaseExtTaskWithCurrentCulture(false);
                }
            }

            return(isOk);
        }
Example #3
0
        /// <summary>
        /// Поместить данные в хранилище.
        /// </summary>
        /// <typeparam name="T">Тип данных.</typeparam>
        /// <param name="key">Ключ.</param>
        /// <param name="data">Данные.</param>
        /// <returns>Признак успешности выполнения.</returns>
        public object PutData <T>(CoreCachingKey key, T data)
        {
            object value = null;

            var isOk = Cache == null;

            if (isOk)
            {
                value = data;
            }
            else
            {
                var dataKey = Helper.ConvertToDataKey(key.Name);

                var expiry = key.GetCacheExpiryTimeSpan(Helper.ExpiryInSeconds);

                var absoluteExpiration = expiry.HasValue ? DateTime.Now.Add(expiry.Value) : (DateTime?)null;

                var options = new MemoryCacheEntryOptions();

                if (absoluteExpiration.HasValue)
                {
                    options.AbsoluteExpiration = absoluteExpiration.Value;
                }

                if (typeof(T) == typeof(byte[]) || typeof(T) == typeof(string))
                {
                    value = data;
                }
                else
                {
                    value = data.CoreBaseExtProtoBufSerialize();
                }

                lock (DataKeysByTag)
                {
                    foreach (var tag in key.Tags)
                    {
                        HashSet <string> dataKeys;

                        if (!DataKeysByTag.TryGetValue(tag, out dataKeys))
                        {
                            dataKeys = new HashSet <string>();

                            DataKeysByTag[tag] = dataKeys;
                        }

                        dataKeys.Add(dataKey);
                    }

                    Cache.Set(dataKey, value, options);
                }

                isOk = true;
            }

            return(isOk ? value : null);
        }