private SecondLevelCache<Guid, TestElement> CreateCache() { var cache = new SecondLevelCache<Guid, TestElement>(Comparer<Guid>.Default, k => k.Frequency, a => { a(); _wasRebalancing = true; }); return cache; }
private SecondLevelCache <Guid, TestElement> CreateCache() { var cache = new SecondLevelCache <Guid, TestElement>(Comparer <Guid> .Default, k => k.Frequency, a => { a(); _wasRebalancing = true; }); return(cache); }
public virtual void Delete(TEntity entity) { object id = GetId(entity); logger.DebugFormat("Deleting #{0}", id); FileSystem.Delete <TEntity>(id); Cache.Remove(GetId(entity)); Cache.Clear(entityCache: false, queryCache: true); SecondLevelCache.Clear(entityCache: false, queryCache: true); }
public virtual void SaveOrUpdate(TEntity entity) { var id = GetOrAssignId(entity); string xml = Serialize(entity); logger.DebugFormat("Writing #{0} with xml {1}", id, string.IsNullOrEmpty(xml) ? "(empty)" : xml.Length.ToString()); FileSystem.Write <TEntity>(id, xml); Cache.Set(id, entity); Cache.Clear(entityCache: false, queryCache: true); SecondLevelCache.Clear(entityCache: false, queryCache: true); }
public CacheController(SecondLevelCache <TKey, TValue> controlledCache, bool fixAllNodes) { _controlledCache = controlledCache; _changeBranchLength = !fixAllNodes; Settings = new CacheAdaptationSettings { CheckThreshold = 50, GoneIntensityForBranchIncrease = 0.205f, GoneIntensityForBranchDecrease = 0.2f, MinBranchLengthToRebalance = 6, RebalancingMode = RebalancingMode.Hybrid }; _controlledCache.NodeGoneEvent += (o, e) => Interlocked.Increment(ref _nodeBeenGoneSinceLastBranchIncreasing); }
public void SimpleCacheTest() { var cache = new SecondLevelCache <int, TestElement>(Comparer <int> .Default, k => k.Frequency, null, true); bool testFailed = false; cache.NodeGoneEvent += (o, e) => testFailed = true; for (int i = 0; i < 10000; i++) { cache.Add(i, new TestElement()); } GC.Collect(2, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); GC.Collect(2, GCCollectionMode.Forced); Assert.IsTrue(!testFailed); cache.Dispose(); }
public void SimpleCacheTest() { var cache = new SecondLevelCache<int,TestElement>(Comparer<int>.Default,k=>k.Frequency,null,true); bool testFailed = false; cache.NodeGoneEvent += (o, e) => testFailed = true; for(int i=0;i<10000;i++) { cache.Add(i,new TestElement()); } GC.Collect(2,GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); GC.Collect(2,GCCollectionMode.Forced); Assert.IsTrue(!testFailed); cache.Dispose(); }
/// <inheritdoc/> public void Set <T>(Dictionary <CacheItemDefinition, T> objects) where T : class { GuardDisposed(); var valuesToStore = SaveToFirstLevelCacheAndGetSerializedValuesForSecondLevelCache(objects); if (valuesToStore.InNonSerializableCache.Count > 0) { var keys = valuesToStore.InNonSerializableCache.Select(x => x.Key.Key).ToArray(); var ttlResults = SecondLevelCache.GetTimeToLives(keys); RemoveTtlResultFromValuesToStore(ttlResults, valuesToStore); } SecondLevelCache.Set(valuesToStore.InSecondLevelCache); StoreValuesInFirstLevelCacheAndNonSerializableCache(valuesToStore); }
/// <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)); }
/// <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); } } }
/// <inheritdoc/> public Task <IDisposable> AcquireLockAsync(string key, TimeSpan lockExpiry, TimeSpan timeout) { GuardDisposed(); return(SecondLevelCache.AcquireLockAsync(key, lockExpiry, timeout)); }
/// <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); }