Exemple #1
0
        private void StoreValuesInFirstLevelCacheAndNonSerializableCache <T>(ValuesToStore <T> valuesToStore)
        {
            foreach (var kvp in valuesToStore.InFirstLevelCache)
            {
                FirstLevelCache.Set(kvp.Key.Key, kvp.Value, ttl: kvp.Key.TimeToLive);
            }

            foreach (var kvp in valuesToStore.InNonSerializableCache)
            {
                NonSerializableCache.Set(kvp.Key.Key, kvp.Value, ttl: kvp.Key.TimeToLive);
            }
        }
Exemple #2
0
        /// <inheritdoc/>
        public async Task <T> GetAsync <T>(string key)
            where T : class
        {
            GuardDisposed();

            var nonSerializableResult = NonSerializableCache.Get <T>(key);

            if (nonSerializableResult.IsSuccessful)
            {
                return(nonSerializableResult.Value);
            }

            var firstLevelResult = FirstLevelCache.Get <T>(key);

            if (firstLevelResult.IsSuccessful)
            {
                return(firstLevelResult.Value);
            }

            var bytes = await SecondLevelCache.GetAsync(key).ConfigureAwait(false);

            if (bytes != null)
            {
                var deserializedValue = SerializationProvider.Deserialize(bytes);

                if (deserializedValue is NonSerializableObjectPlaceHolder)
                {
                    return(null);
                }

                var ttlResult = await SecondLevelCache.GetTimeToLiveAsync(key).ConfigureAwait(false);

                if (ttlResult.KeyExists)
                {
                    FirstLevelCache.Set(key, bytes, ttl: ttlResult.TimeToLive);
                }

                return(deserializedValue as T);
            }

            return(default(T));
        }
Exemple #3
0
        /// <inheritdoc/>
        public async Task SetAsync <T>(string key,
                                       T value,
                                       TimeSpan?ttl)
            where T : class
        {
            GuardDisposed();

            if (value == null)
            {
                return;
            }

            await Task.Yield();

            if (SerializationProvider.CanSerialize(value.GetType()))
            {
                var bytes = SerializationProvider.Serialize(value);

                await SecondLevelCache.SetAsync(key, bytes, ttl : ttl).ConfigureAwait(false);

                FirstLevelCache.Set(key, bytes, ttl: ttl);
            }
            else
            {
                var ph        = new NonSerializableObjectPlaceHolder();
                var ttlResult = SecondLevelCache.GetTimeToLive(key);

                if (ttlResult.KeyExists)
                {
                    NonSerializableCache.Set(key, value, ttl: ttlResult.TimeToLive);
                }
                else
                {
                    byte[] bytes = SerializationProvider.Serialize(ph);
                    await SecondLevelCache.SetAsync(key, bytes, ttl : ttl).ConfigureAwait(false);

                    NonSerializableCache.Set(key, value, ttl: ttl);
                }
            }
        }
Exemple #4
0
        /// <inheritdoc/>
        public void Set <T>(string key,
                            T value,
                            TimeSpan?ttl)
            where T : class
        {
            GuardDisposed();

            if (value == null)
            {
                return;
            }

            if (SerializationProvider.CanSerialize(value.GetType()))
            {
                byte[] bytes = SerializationProvider.Serialize(value);

                SecondLevelCache.Set(key, bytes, ttl: ttl);
                FirstLevelCache.Set(key, bytes, ttl: ttl);
            }
            else
            {
                var ph        = new NonSerializableObjectPlaceHolder();
                var ttlResult = SecondLevelCache.GetTimeToLive(key);

                if (ttlResult.KeyExists)
                {
                    NonSerializableCache.Set(key, value, ttl: ttlResult.TimeToLive);
                }
                else
                {
                    byte[] bytes = SerializationProvider.Serialize(ph);
                    SecondLevelCache.Set(key, bytes, ttl: ttl);
                    NonSerializableCache.Set(key, value, ttl: ttl);
                }
            }
        }
Exemple #5
0
 private void OnRemoveAllItemsFromSecondLevelCache(object sender, RemoveAllItemsEventArgs e)
 {
     FirstLevelCache.RemoveAll();
     NonSerializableCache.RemoveAll();
 }