示例#1
0
        public async Task Should_Set_Get_And_Remove_Cache_Items_With_Object_Type_CacheKey()
        {
            var personCache = GetRequiredService <IDistributedCache <PersonCacheItem, ComplexObjectAsCacheKey> >();

            var cacheKey = new ComplexObjectAsCacheKey {
                Name = "DummyData", Age = 42
            };
            const string personName = "john nash";

            //Get (not exists yet)
            var cacheItem = await personCache.GetAsync(cacheKey).ConfigureAwait(false);

            cacheItem.ShouldBeNull();

            //Set
            cacheItem = new PersonCacheItem(personName);
            await personCache.SetAsync(cacheKey, cacheItem).ConfigureAwait(false);

            //Get (it should be available now
            cacheItem = await personCache.GetAsync(cacheKey).ConfigureAwait(false);

            cacheItem.ShouldNotBeNull();
            cacheItem.Name.ShouldBe(personName);

            //Remove
            await personCache.RemoveAsync(cacheKey).ConfigureAwait(false);

            //Get (not exists since removed)
            cacheItem = await personCache.GetAsync(cacheKey).ConfigureAwait(false);

            cacheItem.ShouldBeNull();
        }
示例#2
0
        public async Task Should_Set_Get_And_Remove_Cache_Items_For_Same_Object_Type_With_Different_CacheKeys()
        {
            var personCache = GetRequiredService <IDistributedCache <PersonCacheItem, ComplexObjectAsCacheKey> >();

            var cache1Key = new ComplexObjectAsCacheKey {
                Name = "John", Age = 42
            };
            var cache2Key = new ComplexObjectAsCacheKey {
                Name = "Jenny", Age = 24
            };
            const string personName = "john nash";

            //Get (not exists yet)
            var cacheItem1 = await personCache.GetAsync(cache1Key).ConfigureAwait(false);

            var cacheItem2 = await personCache.GetAsync(cache2Key).ConfigureAwait(false);

            cacheItem1.ShouldBeNull();
            cacheItem2.ShouldBeNull();

            //Set
            cacheItem1 = new PersonCacheItem(personName);
            cacheItem2 = new PersonCacheItem(personName);
            await personCache.SetAsync(cache1Key, cacheItem1).ConfigureAwait(false);

            await personCache.SetAsync(cache2Key, cacheItem2).ConfigureAwait(false);

            //Get (it should be available now
            cacheItem1 = await personCache.GetAsync(cache1Key).ConfigureAwait(false);

            cacheItem1.ShouldNotBeNull();
            cacheItem1.Name.ShouldBe(personName);

            cacheItem2 = await personCache.GetAsync(cache2Key).ConfigureAwait(false);

            cacheItem2.ShouldNotBeNull();
            cacheItem2.Name.ShouldBe(personName);

            //Remove
            await personCache.RemoveAsync(cache1Key).ConfigureAwait(false);

            await personCache.RemoveAsync(cache2Key).ConfigureAwait(false);

            //Get (not exists since removed)
            cacheItem1 = await personCache.GetAsync(cache1Key).ConfigureAwait(false);

            cacheItem1.ShouldBeNull();
            cacheItem2 = await personCache.GetAsync(cache2Key).ConfigureAwait(false);

            cacheItem2.ShouldBeNull();
        }