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); } }
/// <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); } } }