Esempio n. 1
0
        /// <inheritdoc/>
        public T Get <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 = SecondLevelCache.Get(key);

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

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

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

                return(deserializedValue as T);
            }

            return(default(T));
        }
Esempio n. 2
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);
                }
            }
        }
Esempio n. 3
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);
                }
            }
        }
Esempio n. 4
0
 private void OnItemRemovedFromSecondLevelCache(object sender, ItemEvictedEventArgs e)
 {
     FirstLevelCache.Remove(e.Key);
 }
Esempio n. 5
0
 private void OnRemoveAllItemsFromSecondLevelCache(object sender, RemoveAllItemsEventArgs e)
 {
     FirstLevelCache.RemoveAll();
     NonSerializableCache.RemoveAll();
 }
Esempio n. 6
0
 /// <inheritdoc/>
 public Task RemoveAllAsync()
 {
     GuardDisposed();
     FirstLevelCache.RemoveAll();
     return(SecondLevelCache.RemoveAllAsync());
 }
Esempio n. 7
0
 /// <inheritdoc/>
 public void RemoveAll()
 {
     GuardDisposed();
     FirstLevelCache.RemoveAll();
     SecondLevelCache.RemoveAll();
 }
Esempio n. 8
0
 /// <inheritdoc/>
 public Task RemoveAsync(string key)
 {
     GuardDisposed();
     FirstLevelCache.Remove(key);
     return(SecondLevelCache.RemoveAsync(key));
 }
Esempio n. 9
0
 /// <inheritdoc/>
 public void Remove(string key)
 {
     GuardDisposed();
     FirstLevelCache.Remove(key);
     SecondLevelCache.Remove(key);
 }