/// <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)); }
/// <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); } } }
/// <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); } } }
private void OnItemRemovedFromSecondLevelCache(object sender, ItemEvictedEventArgs e) { FirstLevelCache.Remove(e.Key); }
private void OnRemoveAllItemsFromSecondLevelCache(object sender, RemoveAllItemsEventArgs e) { FirstLevelCache.RemoveAll(); NonSerializableCache.RemoveAll(); }
/// <inheritdoc/> public Task RemoveAllAsync() { GuardDisposed(); FirstLevelCache.RemoveAll(); return(SecondLevelCache.RemoveAllAsync()); }
/// <inheritdoc/> public void RemoveAll() { GuardDisposed(); FirstLevelCache.RemoveAll(); SecondLevelCache.RemoveAll(); }
/// <inheritdoc/> public Task RemoveAsync(string key) { GuardDisposed(); FirstLevelCache.Remove(key); return(SecondLevelCache.RemoveAsync(key)); }
/// <inheritdoc/> public void Remove(string key) { GuardDisposed(); FirstLevelCache.Remove(key); SecondLevelCache.Remove(key); }