private SecondLevelCache<Guid, TestElement> CreateCache()
 {
     var cache = new SecondLevelCache<Guid, TestElement>(Comparer<Guid>.Default,
                                                         k => k.Frequency,
                                                         a =>
                                                             {
                                                                 a();
                                                                 _wasRebalancing = true;
                                                             });
     return cache;
 }
Example #2
0
        private SecondLevelCache <Guid, TestElement> CreateCache()
        {
            var cache = new SecondLevelCache <Guid, TestElement>(Comparer <Guid> .Default,
                                                                 k => k.Frequency,
                                                                 a =>
            {
                a();
                _wasRebalancing = true;
            });

            return(cache);
        }
Example #3
0
        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);
        }
Example #4
0
        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);
        }
Example #5
0
 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);
 }
Example #6
0
        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();
  }
Example #8
0
        /// <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);
        }
Example #9
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));
        }
Example #10
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);
                }
            }
        }
Example #11
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);
                }
            }
        }
Example #12
0
 /// <inheritdoc/>
 public Task <IDisposable> AcquireLockAsync(string key, TimeSpan lockExpiry, TimeSpan timeout)
 {
     GuardDisposed();
     return(SecondLevelCache.AcquireLockAsync(key, lockExpiry, timeout));
 }
Example #13
0
 /// <inheritdoc/>
 public Task RemoveAllAsync()
 {
     GuardDisposed();
     FirstLevelCache.RemoveAll();
     return(SecondLevelCache.RemoveAllAsync());
 }
Example #14
0
 /// <inheritdoc/>
 public void RemoveAll()
 {
     GuardDisposed();
     FirstLevelCache.RemoveAll();
     SecondLevelCache.RemoveAll();
 }
Example #15
0
 /// <inheritdoc/>
 public Task RemoveAsync(string key)
 {
     GuardDisposed();
     FirstLevelCache.Remove(key);
     return(SecondLevelCache.RemoveAsync(key));
 }
Example #16
0
 /// <inheritdoc/>
 public void Remove(string key)
 {
     GuardDisposed();
     FirstLevelCache.Remove(key);
     SecondLevelCache.Remove(key);
 }