示例#1
0
 protected LocalCacheTable(LocalCache cache, string name, TableOptions options)
 {
    m_Name = name;
    m_Cache = cache;
    m_Options = options!=null ? options.Clone() : new TableOptions(name);
    m_Options.m_Name = name;
 } 
        public void LocalCache_will_not_store_to_cache_if_content_type_already_added_with_same_refresh_time()
        {
            // Arrange
            var contentRefreshDate = new DateTime(2014, 07, 07, 12, 30, 0);
            var storageHandler = new Mock<IStorageHandler<Content>>();

            storageHandler.Setup(x => x.WriteToStorage(It.IsAny<ContentTypes>(), It.IsAny<Content>()));
            var cache = new LocalCache<Content>(storageHandler.Object);
            var footerContent = new Content
            {
                Sections = new List<ContentSection>
                {
                    new ContentSection
                    {
                        Id = ContentTypes.Footer.ToString(),
                        Html = "<div id='footer' />"
                    }
                },
                RefreshDate = contentRefreshDate
            };

            // Act
            cache.WriteToCache(ContentTypes.Footer, footerContent, footerContent.RefreshDate);
            cache.WriteToCache(ContentTypes.Footer, footerContent, footerContent.RefreshDate);

            // Assert
            storageHandler.Verify(x => x.WriteToStorage(It.IsAny<ContentTypes>(), It.IsAny<Content>()), Times.Once);
        }
示例#3
0
        static Current()
        {
            var connection = new AsyncRedisConnection(null, "127.0.0.1:6379,connectTimeout=10", () => false, "", "ryujit-debug");
            var syncRoot = new object();
            var localCache = new LocalCache(syncRoot, 0);
            var redis = new AsyncDelayedRedisCache("connect", new Messaging(connection), connection, localCache, new NullCache(0), 0, "dev");
            SiteCache = new ImmediateCache(redis, 0);

            #pragma warning disable 618
            LocalCache.LogDuration += (string key, string caller, int? duration) =>
            #pragma warning restore 618
            {
                if (key.Contains("guid-test"))
                {
                    switch (caller)
                    {
                        case "LocalCache.Set":
                            CurrentTest.LocalCacheSetDuration = duration;
                            break;
                        case "LocalCache.SetWithPriority":
                            CurrentTest.LocalCacheSetWithPriorityDuration = duration;
                            break;
                        case "RawSet":
                            CurrentTest.RawSetDuration = duration;
                            break;
                    }
                }
            };
        }
        public void LocalCache_will_retrieve_content_from_cache_if_it_exists()
        {
            // Arrange
            var cachedContent = new Content
            {
                RefreshDate = new DateTime(2014, 01, 01),
                Sections = new List<ContentSection>
                {
                    new ContentSection
                    {
                        Id = ContentTypes.HeaderWithoutMegaNav.ToString(),
                        Html = "<div id='header' />"
                    }
                }
            };
            var storageHandler = new Mock<IStorageHandler<Content>>();
            storageHandler.Setup(x => x.ReadFromStorage(ContentTypes.HeaderWithoutMegaNav)).Returns(cachedContent);
            var cache = new LocalCache<Content>(storageHandler.Object);

            // Act
            var footerWithoutMegaNav = cache.ReadFromCache(ContentTypes.HeaderWithoutMegaNav);

            // Assert
            Assert.That(footerWithoutMegaNav, Is.Not.Null);
        }
        public void LocalCache_will_return_null_if_content_does_not_exist_in_cache()
        {
            // Arrange
            var storageHandler = new Mock<IStorageHandler<Content>>();
            storageHandler.Setup(x => x.ReadFromStorage(ContentTypes.HeaderWithoutMegaNav)).Returns((Content)null);
            var cache = new LocalCache<Content>(storageHandler.Object);

            // Act
            var footerWithoutMegaNav = cache.ReadFromCache(ContentTypes.HeaderWithoutMegaNav);

            // Assert
            Assert.That(footerWithoutMegaNav, Is.Null);
        }
示例#6
0
      public void T050_PileNonOwnershipErrorStart()
      {
        var pile = new DefaultPile();

        try
        {
          var cache = new LocalCache();
          cache.Pile = pile;
          cache.Configure(null);
          cache.Start();  //can not start cache that uses inactive pile which is not managed by this cache
        }
        finally
        {
          pile.Dispose();
        }
                
      }
示例#7
0
 private static LocalCache makeCache()
 {
     var cache = new LocalCache();
        cache.Pile = new DefaultPile(cache);
        cache.Configure(null);
        cache.Start();
        return cache;
 }
示例#8
0
      public void T040_PileOwnership()
      {
        var cache = new LocalCache();
        cache.Pile = new DefaultPile(cache);
        cache.Configure(null);
        cache.Start();
                
        
        var tA = cache.GetOrCreateTable<string>("A");
        tA.Put("aaa", "avalue");
        Assert.AreEqual("avalue", tA.Get("aaa"));

        cache.WaitForCompleteStop();
        Assert.AreEqual(NFX.ServiceModel.ControlStatus.Inactive, cache.Status);
        Assert.AreEqual(NFX.ServiceModel.ControlStatus.Inactive, cache.Pile.Status);
      }
示例#9
0
        public void DeleteSeveral_TwoTables_ByteArray(bool speed, int durationSec, int putMin, int putMax, int delFactor, int payloadSizeMin, int payloadSizeMax, bool isParallel)
        {
            using (var cache = new LocalCache())
              using (var pile = new DefaultPile(cache))
              {
            cache.Pile = pile;
            cache.PileAllocMode = speed ? AllocationMode.FavorSpeed : AllocationMode.ReuseSpace;
            cache.Start();

            var startTime = DateTime.UtcNow;
            var tasks = new List<Task>();
            for (var t = 0; t < (isParallel ? (System.Environment.ProcessorCount - 1) : 1); t++)
              tasks.Add(Task.Factory.StartNew(() =>
            {
              var list = new List<Tuple<int, GDID, int, byte, byte>>();
              var tA = cache.GetOrCreateTable<GDID>("A");
              var tB = cache.GetOrCreateTable<GDID>("B");
              ulong k = 0;
              var wlc = 0;

              while (true)
              {
                if ((DateTime.UtcNow - startTime).TotalSeconds >= durationSec) break;

                var putCount = NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(putMin, putMax);
                for (int i = 0; i < putCount; i++)
                {
                  var payloadSize = NFX.ExternalRandomGenerator
                                      .Instance.NextScaledRandomInteger(payloadSizeMin, payloadSizeMax);
                  var val = new byte[payloadSize];
                  val[0] = (byte)NFX.ExternalRandomGenerator.Instance.NextRandomInteger;
                  val[payloadSize - 1] = (byte)NFX.ExternalRandomGenerator.Instance.NextRandomInteger;

                  var tableId = NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, 1);
                  var table = tableId == 0 ? tA : tB;
                  var key = new GDID((uint)Thread.CurrentThread.ManagedThreadId, k);

                  table.Put(key, val);

                  list.Add(new Tuple<int, GDID, int, byte, byte>(tableId, key, payloadSize - 1, val[0], val[payloadSize - 1]));
                  k++;
                }

                int delCount = putCount / delFactor;
                for (int i = 0; i < delCount; i++)
                {
                  while (true && list.Count > 0)
                  {
                    var idx = NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, list.Count - 1);
                    var element = list[idx];
                    var table = element.Item1 == 0 ? tA : tB;
                    var key = element.Item2;

                    var removed = table.Remove(key);
                    list.RemoveAt(idx);
                    if (removed)
                      break;
                  }
                }

                // get several random elements
                if (list.Count > 64 && NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, 100) > 98)
                {
                  var toRead = NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(8, 64);
                  wlc++;
                  if (wlc % 125 == 0)
                    Console.WriteLine("Thread {0} is reading {1} elements"
                      .Args(Thread.CurrentThread.ManagedThreadId, toRead));
                  for (var j = 0; j < toRead && list.Count > 0; j++)
                  {
                    var idx = NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, list.Count - 1);
                    var element = list[idx];
                    var table = element.Item1 == 0 ? tA : tB;
                    var key = element.Item2;

                    var buf = table.Get(key) as byte[];
                    if (buf == null)
                    {
                      list.RemoveAt(idx);
                      continue;
                    }
                    Assert.AreEqual(element.Item4, buf[0]);
                    Assert.AreEqual(element.Item5, buf[element.Item3]);
                  }
                }

                if (list.Count == Int32.MaxValue)
                  list = new List<Tuple<int, GDID, int, byte, byte>>();
              }

              // total check
              Console.WriteLine("Thread {0} is doing final read of {1} elements".Args(Thread.CurrentThread.ManagedThreadId, list.Count));
              foreach (var element in list)
              {
                var table = element.Item1 == 0 ? tA : tB;
                var val = table.Get(element.Item2) as byte[];
                if (val == null)
                  continue;
                Assert.AreEqual(element.Item4, val[0]);
                Assert.AreEqual(element.Item5, val[element.Item3]);
              }
              return;
            }, TaskCreationOptions.LongRunning));
            Task.WaitAll(tasks.ToArray());
              }
        }
示例#10
0
        public void Chessboard_ByteArray(bool speed, int durationSec, int payloadSizeMin, int payloadSizeMax, bool isParallel)
        {
            using (var cache = new LocalCache())
              using (var pile = new DefaultPile(cache))
              {
            cache.Pile = pile;
            cache.PileAllocMode = speed ? AllocationMode.FavorSpeed : AllocationMode.ReuseSpace;
            cache.Start();

            var startTime = DateTime.UtcNow;
            var tasks = new List<Task>();
            for (var t = 0; t < (isParallel ? (System.Environment.ProcessorCount - 1) : 1); t++)
              tasks.Add(Task.Factory.StartNew(() =>
            {
              var list = new List<CheckByteArray>();
              var i = 0;
              var tA = cache.GetOrCreateTable<GDID>("A");
              var wlc = 0;
              while (true)
              {
                if ((DateTime.UtcNow - startTime).TotalSeconds >= durationSec) break;

                var payloadSize = NFX.ExternalRandomGenerator
                                    .Instance.NextScaledRandomInteger(payloadSizeMin, payloadSizeMax);
                var val = new byte[payloadSize];
                val[0] = (byte)NFX.ExternalRandomGenerator.Instance.NextRandomInteger;
                val[payloadSize - 1] = (byte)NFX.ExternalRandomGenerator.Instance.NextRandomInteger;

                var key = new GDID((uint)Thread.CurrentThread.ManagedThreadId, (ulong)i);
                tA.Put(key, val);

                var element = new CheckByteArray(key, payloadSize - 1, val[0], val[payloadSize - 1]);
                list.Add(element);

                // delete previous element
                if (list.Count > 1 && i % 2 == 0)
                {
                  key = list[list.Count - 2].Key;
                  tA.Remove(key);
                  list.RemoveAt(list.Count - 2);
                }

                // get several random elements
                if (list.Count > 64 && NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, 100) > 98)
                {
                  var toRead = NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(8, 64);
                  wlc++;
                  if (wlc % 125 == 0)
                    Console.WriteLine("Thread {0} is reading {1} elements, total {2}"
                      .Args(Thread.CurrentThread.ManagedThreadId, toRead, list.Count));
                  for (var k = 0; k < toRead && list.Count > 0; k++)
                  {
                    var idx = NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, list.Count - 1);
                    element = list[idx];
                    var buf = tA.Get(element.Key) as byte[];
                    if (buf == null)
                    {
                      list.RemoveAt(idx);
                      continue;
                    }
                    Assert.AreEqual(element.FirstByte, buf[0]);
                    Assert.AreEqual(element.LastByte, buf[element.IdxLast]);
                  }
                }

                if (i == Int32.MaxValue)
                  i = 0;
                else
                  i++;

                if (list.Count == Int32.MaxValue)
                  list = new List<CheckByteArray>();
              }

              // total check
              Console.WriteLine("Thread {0} is doing final read of {1} elements, tableCount {2}"
                .Args(Thread.CurrentThread.ManagedThreadId, list.Count, tA.Count));
              foreach (var element in list)
              {
                var buf = tA.Get(element.Key) as byte[];
                if (buf == null)
                  continue;
                Assert.AreEqual(element.FirstByte, buf[0]);
                Assert.AreEqual(element.LastByte, buf[element.IdxLast]);
              }
              return;
            }, TaskCreationOptions.LongRunning));
            Task.WaitAll(tasks.ToArray());
              }
        }
示例#11
0
        public void LocalCache_will_store_to_cache_if_content_type_exists_with_older_refresh_time()
        {
            // Arrange
            var storageHandler = new Mock<IStorageHandler<Content>>();

            storageHandler.Setup(x => x.WriteToStorage(It.IsAny<ContentTypes>(), It.IsAny<Content>()));
            var cache = new LocalCache<Content>(storageHandler.Object);

            var oldFooterContent = new Content
            {
                Sections = new List<ContentSection>
                {
                    new ContentSection
                    {
                        Id = ContentTypes.Footer.ToString(),
                        Html = "<div id='footer' />"
                    }
                },
                RefreshDate = new DateTime(2014, 07, 07)
            };

            var newFooterContent = new Content
            {
                Sections = new List<ContentSection>
                {
                    new ContentSection
                    {
                        Id = ContentTypes.Footer.ToString(),
                        Html = "<div id='footer'>footer</div>"
                    }
                },
                RefreshDate = new DateTime(2014, 07, 08)
            };

            // Act
            cache.WriteToCache(ContentTypes.Footer, oldFooterContent, oldFooterContent.RefreshDate);
            cache.WriteToCache(ContentTypes.Footer, newFooterContent, newFooterContent.RefreshDate);

            // Assert
            storageHandler.Verify(x => x.WriteToStorage(It.IsAny<ContentTypes>(), It.IsAny<Content>()), Times.Exactly(2));
        }
示例#12
0
 public MemoryNetworkCache(long max_cache_size)
 {
     heap_cache = new LocalCache(Cache.ClosestPrime(4096), max_cache_size);
     s2block_cache = new Dictionary<long, BlockCacheElement>(1024);
 }
示例#13
0
 public InMemoryNetworkCache(long maxCacheSize)
 {
     heapCache = new LocalCache(Cache.ClosestPrime(12000), maxCacheSize);
     s2BlockCache = new Dictionary<BlockId, BlockCacheElement>(1023);
     pathInfoMap = new Dictionary<string, PathInfo>(255);
 }
示例#14
0
      public void T060_PileNonOwnership()
      {
        var pile = new DefaultPile();
        pile.Start();
        try
        {
              var cache = new LocalCache();
              cache.Pile = pile;
              cache.Configure(null);
              cache.Start();
                
        
              var tA = cache.GetOrCreateTable<string>("A");
              tA.Put("aaa", "avalue");
              tA.Put("bbb", "bvalue");
              Assert.AreEqual("avalue", tA.Get("aaa"));

              Assert.AreEqual(2, cache.Count);
              Assert.AreEqual(2, pile.ObjectCount);

              cache.WaitForCompleteStop();
              Assert.AreEqual(NFX.ServiceModel.ControlStatus.Inactive, cache.Status);
        
              Assert.AreEqual(NFX.ServiceModel.ControlStatus.Active, pile.Status);
              Assert.AreEqual(0, pile.ObjectCount);
       
              cache = new LocalCache();
              cache.Pile = pile;
              cache.Configure(null);
              cache.Start();

              var tAbc = cache.GetOrCreateTable<string>("Abc");
              tAbc.Put("aaa", "avalue");
              tAbc.Put("bbb", "bvalue");
              tAbc.Put("ccc", "cvalue");
              tAbc.Put("ddd", "cvalue");

              Assert.AreEqual(4, pile.ObjectCount);

              var cache2 = new LocalCache();
              cache2.Pile = pile;
              cache2.Configure(null);
              cache2.Start();
                
        
              var t2 = cache2.GetOrCreateTable<string>("A");
              t2.Put("aaa", "avalue");
              t2.Put("bbb", "bvalue");
        
              Assert.AreEqual(2, cache2.Count);
              Assert.AreEqual(6, pile.ObjectCount);

              cache.WaitForCompleteStop();
              Assert.AreEqual(NFX.ServiceModel.ControlStatus.Active, pile.Status);
              Assert.AreEqual(2, pile.ObjectCount);

              cache2.WaitForCompleteStop();
              Assert.AreEqual(NFX.ServiceModel.ControlStatus.Active, pile.Status);
              Assert.AreEqual(0, pile.ObjectCount);

              pile.WaitForCompleteStop();
              Assert.AreEqual(NFX.ServiceModel.ControlStatus.Inactive, pile.Status);
        }
        finally
        {
          pile.Dispose();
        }
      }
示例#15
0
        public void NoGrowth_ByteArray(bool speed, int durationSec, int payloadSizeMin, int payloadSizeMax, int countMin, int countMax)
        {
            using (var cache = new LocalCache())
              using (var pile = new DefaultPile(cache))
              {
            cache.Pile = pile;
            cache.PileAllocMode = speed ? AllocationMode.FavorSpeed : AllocationMode.ReuseSpace;
            cache.Start();

            var startTime = DateTime.UtcNow;
            var tasks = new List<Task>();
            for (var t = 0; t < (System.Environment.ProcessorCount - 1); t++)
              tasks.Add(Task.Factory.StartNew(() =>
            {
              var tA = cache.GetOrCreateTable<GDID>("A");
              var list = new List<CheckByteArray>();
              bool put = true;

              while (true)
              {
                if ((DateTime.UtcNow - startTime).TotalSeconds >= durationSec) return;

                if (put)
                {
                  var cnt = NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(countMin, countMax);
                  for (int i = 0; i < cnt; i++)
                  {
                    var payloadSize = NFX.ExternalRandomGenerator
                                        .Instance.NextScaledRandomInteger(payloadSizeMin, payloadSizeMax);
                    var val = new byte[payloadSize];
                    val[0] = (byte)NFX.ExternalRandomGenerator.Instance.NextRandomInteger;
                    val[payloadSize - 1] = (byte)NFX.ExternalRandomGenerator.Instance.NextRandomInteger;
                    var key = new GDID((uint)Thread.CurrentThread.ManagedThreadId, (ulong)i);

                    tA.Put(key, val);

                    var element = new CheckByteArray(key, payloadSize - 1, val[0], val[payloadSize - 1]);
                    list.Add(element);
                  }
                  Console.WriteLine("Thread {0} put {1} objects".Args(Thread.CurrentThread.ManagedThreadId, list.Count));
                  put = false;
                }
                else
                {
                  var i = 0;
                  for (var j = 0; j < list.Count; j++)
                  {
                    var element = list[j];
                    var buf = tA.Get(element.Key) as byte[];
                    if (buf != null)
                    {
                      Assert.AreEqual(element.FirstByte, buf[0]);
                      Assert.AreEqual(element.LastByte, buf[element.IdxLast]);
                      tA.Remove(element.Key);
                      i++;
                    }
                  }
                  Console.WriteLine("Thread {0} deleted {1} objects".Args(Thread.CurrentThread.ManagedThreadId, i));
                  list.Clear();
                  put = true;
                }
              }
            }, TaskCreationOptions.LongRunning));
            Task.WaitAll(tasks.ToArray());
              }
        }
示例#16
0
        public void LocalCache_will_store_to_cache_if_content_type_not_already_added()
        {
            // Arrange
            var storageHandler = new Mock<IStorageHandler<Content>>();
            storageHandler.Setup(x => x.WriteToStorage(It.IsAny<ContentTypes>(), It.IsAny<Content>()));
            var cache = new LocalCache<Content>(storageHandler.Object);
            var footerContent = new Content
            {
                Sections = new List<ContentSection>
                {
                    new ContentSection
                    {
                        Id = ContentTypes.Footer.ToString(),
                        Html = "<div id='footer' />"
                    }
                }
            };

            // Act
            cache.WriteToCache(ContentTypes.Footer, footerContent, DateTime.Now);

            // Assert
            storageHandler.Verify(x => x.WriteToStorage(It.IsAny<ContentTypes>(), It.IsAny<Content>()), Times.Once);
        }
示例#17
0
 /// <summary>
 /// Instances a new <see cref="RouteMappingDoubleBufferQueue"/>.
 /// </summary>
 public RouteMappingDoubleBufferQueue()
 {
     m_onStatusMessage = x => { };
     m_onProcessException = x => { };
     m_globalCache = new GlobalCache(new Dictionary<IAdapter, Consumer>(), 0);
     m_injectMeasurementsLocalCache = new LocalCache(this, null);
 }
示例#18
0
 private LocalCache makeCache(IConfigSectionNode conf = null)
 {
    var cache = new LocalCache();
    cache.Pile = new DefaultPile(cache);
    cache.Configure( conf );
    cache.Start();
    return cache;
 }
示例#19
0
 /// <summary>
 /// Gets a handler for measurements that routes measurements to the appropriate consumers.
 /// </summary>
 /// <returns>The measurement handler used for routing.</returns>
 public virtual EventHandler<EventArgs<ICollection<IMeasurement>>> GetRoutedMeasurementsHandler()
 {
     LocalCache localCache = new LocalCache(this);
     return (sender, args) => localCache.Route(args.Argument);
 }