Ejemplo n.º 1
0
        public void CanCreateWithCacheNameAndInstanceName()
        {
            var cache = new ObjectCacheWrapper("SomeCache.Instance1");

            Assert.That(cache.Id.Name, Is.EqualTo("SomeCache"), "CacheName");
            Assert.That(cache.Id.InstanceName, Is.EqualTo("Instance1"), "InstanceName");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Author: Kunuk Nykjaer
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            const string key = "key";
            const string filepath = @"c:\temp\file.json";

            IObjectCache cache1 = new FileCache(filepath);
            IObjectCache cache2 = new ObjectCacheWrapper(MemoryCache.Default);

            var dataBeforeLoaded1 = cache1.Get<IDictionary<string, List<string>>>(key);
            var dataBeforeLoaded2 = cache2.Get<IDictionary<string, List<string>>>(key);

            if (dataBeforeLoaded1 != null) Console.WriteLine("dataBeforeLoaded1 was loaded count: {0}", dataBeforeLoaded1.Count);
            if (dataBeforeLoaded2 != null) Console.WriteLine("dataBeforeLoaded2 was loaded count: {0}", dataBeforeLoaded2.Count);

            IDictionary<string, List<string>> dataSaved = new Dictionary<string, List<string>>
            {
                {"a", new List<string>{"a1","a2"}},
                {"b", new List<string>{"b1","b2"}}
            };

            cache1.Set(key, dataSaved, DateTimeOffset.MaxValue);
            cache2.Set(key, dataSaved, DateTimeOffset.MaxValue);

            var dataAfter1 = cache1.Get<IDictionary<string, List<string>>>(key);
            var dataAfter2 = cache2.Get<IDictionary<string, List<string>>>(key);

            if (dataAfter1 != null) Console.WriteLine("dataAfter1 was loaded count: {0}", dataAfter1.Count);
            if (dataAfter2 != null) Console.WriteLine("dataAfter2 was loaded count: {0}", dataAfter2.Count);

            Console.WriteLine("press a key...");
            Console.ReadKey();
        }
Ejemplo n.º 3
0
        public void ItemsAddedToCacheShouldUseCachePolicySupplied()
        {
            // given
            var policy = new CacheItemPolicy
            {
                AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(1)
            };
            ICache cache = new ObjectCacheWrapper(cacheItemPolicySelector: (key, value) => policy);

            // when, then
            cache.GetOrAdd("key1", _ => new object());
            cache.AddOrUpdate("key2", new object());

            Assert.That(cache.Contains("key1"), Is.True, "checking assumptions for GetOrAdd");
            Assert.That(cache.Contains("key2"), Is.True, "checking assumptions for Add");

            Thread.Sleep(2000); // wait until item has expired and should be removed

            Assert.That(cache.Contains("key1"), Is.False, "GetOrAdd did not use expiry policy");
            Assert.That(cache.Contains("key2"), Is.False, "Add did not use expiry policy");
        }