public void ClearTest()
        {
            GenericCache <int, int> cache = new GenericCache <int, int>();

            cache.Add(1, 1001);
            cache.Add(2, 1002);
            Assert.AreEqual <int>(2, cache.Count);
            cache.Clear();
            Assert.AreEqual <int>(0, cache.Count);
        }
Ejemplo n.º 2
0
        public void GenericCacheTest()
        {
            var cacheHelper = new GenericCache <string, string>();

            cacheHelper.Add("hello", "world");

            Assert.AreEqual <int>(1, cacheHelper.Count);
            Assert.AreEqual <bool>(true, cacheHelper.ContainsKey("hello"));
            Assert.AreEqual <bool>(false, cacheHelper.ContainsKey("hi"));

            var val = "";

            cacheHelper.TryGetValue("hello", out val);
            Assert.AreEqual <string>("world", val);

            cacheHelper.Clear();
            Assert.AreEqual <int>(0, cacheHelper.Count);
        }
Ejemplo n.º 3
0
        private static void PerformanceProfiling()
        {
            // Create cache
            var sqliteConfig = new SQLiteCacheHandleAdditionalConfiguration
            {
                DatabaseFilePath = "MyDatabase2.sqlite"
            };

            using ICacheManager <int> cacheManager = CacheFactory.Build <int>(
                      settings =>
            {
                settings
                .WithProtoBufSerializer(
                    new RecyclableMemoryStreamManager())         // TODO: Even the memory stream objects is causing perf problems, since it's in the tight loop', is there a way to pool those too?
                .WithSQLiteCacheHandle(sqliteConfig)
                .EnableStatistics()
                .Build();
            });
            using CancellationTokenSource cts = new CancellationTokenSource();
            Task.Run(
                async() =>
            {
                while (!cts.Token.IsCancellationRequested)
                {
                    await Task.Delay(100);

                    var stats = cacheManager.CacheHandles.First().Stats;
                    Console.WriteLine(
                        string.Format(
                            "Items: {0}, Hits: {1}, Miss: {2}, Remove: {3}, ClearRegion: {4}, Clear: {5}, Adds: {6}, Puts: {7}, Gets: {8}",
                            stats.GetStatistic(CacheStatsCounterType.Items),
                            stats.GetStatistic(CacheStatsCounterType.Hits),
                            stats.GetStatistic(CacheStatsCounterType.Misses),
                            stats.GetStatistic(CacheStatsCounterType.RemoveCalls),
                            stats.GetStatistic(CacheStatsCounterType.ClearRegionCalls),
                            stats.GetStatistic(CacheStatsCounterType.ClearCalls),
                            stats.GetStatistic(CacheStatsCounterType.AddCalls),
                            stats.GetStatistic(CacheStatsCounterType.PutCalls),
                            stats.GetStatistic(CacheStatsCounterType.GetCalls)
                            ));
                }
            });
            using GenericCache <Guid, int> toStringCache = new GenericCache <Guid, int>(cacheManager);

            toStringCache.Clear();

            // Performance profiling
            var st = Stopwatch.StartNew();

            try
            {
                // Use a transaction to avoid going to disk for EACH operation
                // TODO: Better/more ephemeral/auto ways to do this?
                using (var tr = sqliteConfig.GetBeginTransactionMethod()())
                {
                    for (int i = 0; i < 28000; i++)
                    {
                        Guid newGuid = Guid.NewGuid();
                        toStringCache[newGuid] = i;

                        var cacheItemIfExists = toStringCache.GetCacheItem(newGuid);
                        cacheItemIfExists.Should().NotBeNull("should exist");
                        cacheItemIfExists.Value.value.Should().Be(i);
                    }

                    tr.Commit();
                    tr.Dispose();
                }
            }
            finally
            {
                Console.WriteLine(st.Elapsed);
            }
        }