public async Task CreateStorage_SaveOption_ReadCache()
        {
            AutoCacheOptions.SaveAll = true;
            _autoCache = new CrudAutoCache <string, Guid>(_storage, ToGuid, Cache, null, AutoCacheOptions);
            var id = await _autoCache.CreateAsync("A"); // Will update cache thanks to SaveAll

            await PrepareStorageAsync(id, "B");
            await VerifyAsync(id, "B", "A");
        }
        public async Task UpdateStorage_GetOption_ReadCache()
        {
            AutoCacheOptions.DoGetToUpdate = true;
            _autoCache = new CrudAutoCache <string, Guid>(_storage, ToGuid, Cache, null, AutoCacheOptions);
            var id = Guid.NewGuid();

            await PrepareStorageAndCacheAsync(id, "A", "A");

            await _autoCache.UpdateAsync(id, "B"); // Will update cache thanks to DoGetToUpdate

            await PrepareStorageAsync(id, "C");
            await VerifyAsync(id, "C", "B");
        }
 public void Initialize()
 {
     FulcrumApplicationHelper.UnitTestSetup(typeof(TestAutoCacheCrud).FullName);
     _storage = new CrudMemory <string, string, Guid>();
     Cache    = new MemoryDistributedCache();
     DistributedCacheOptions = new DistributedCacheEntryOptions
     {
         AbsoluteExpirationRelativeToNow = TimeSpan.FromMilliseconds(1000)
     };
     AutoCacheOptions = new AutoCacheOptions
     {
         AbsoluteExpirationRelativeToNow = DistributedCacheOptions.AbsoluteExpirationRelativeToNow
     };
     _autoCache = new CrudAutoCache <string, Guid>(_storage, ToGuid, Cache, null, AutoCacheOptions);
 }
        public async Task ReadAllUpdatesIndividualItems()
        {
            AutoCacheOptions.SaveCollections = true;
            AutoCacheOptions.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(10);
            _autoCache = new CrudAutoCache <string, Guid>(_storage, item => ToGuid(item, 1), Cache, null, AutoCacheOptions);
            var id1 = ToGuid("A1", 1);

            await PrepareStorageAndCacheAsync(id1, "A1", null);

            var id2 = ToGuid("B1", 1);

            await PrepareStorageAndCacheAsync(id2, "B1", null);

            var result = await _autoCache.ReadAllAsync();

            Assert.IsNotNull(result);
            await _autoCache.DelayUntilNoOperationActiveAsync();

            await VerifyAsync(id1, "A1");
            await VerifyAsync(id2, "B1");
        }
        public async Task DeleteAllRemovesCache()
        {
            AutoCacheOptions.SaveCollections = true;
            AutoCacheOptions.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(10);
            _autoCache = new CrudAutoCache <string, Guid>(_storage, item => ToGuid(item, 1), Cache, null, AutoCacheOptions);
            var id1 = ToGuid("A1", 1);

            await PrepareStorageAndCacheAsync(id1, "A1", null);

            var id2 = ToGuid("B1", 1);

            await PrepareStorageAndCacheAsync(id2, "B1", null);

            await _autoCache.ReadAllAsync();

            await _autoCache.DeleteAllAsync();

            var result = await _autoCache.ReadAllAsync();

            Assert.IsNotNull(result);
            var enumerable = result as string[] ?? result.ToArray();

            Assert.AreEqual(0, enumerable.Length);
        }
        public async Task ReadAll()
        {
            AutoCacheOptions.SaveCollections = true;
            AutoCacheOptions.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(10);
            _autoCache = new CrudAutoCache <string, Guid>(_storage, item => ToGuid(item, 1), Cache, null, AutoCacheOptions);
            var id1 = ToGuid("A1", 1);

            await PrepareStorageAndCacheAsync(id1, "A1", null);

            var id2 = ToGuid("B1", 1);

            await PrepareStorageAndCacheAsync(id2, "B1", null);

            var result = await _autoCache.ReadAllAsync();

            Assert.IsNotNull(result);
            var enumerable = result as string[] ?? result.ToArray();

            Assert.AreEqual(2, enumerable.Length);
            Assert.IsTrue(enumerable.Contains("A1"));
            Assert.IsTrue(enumerable.Contains("B1"));
            await _autoCache.DelayUntilNoOperationActiveAsync();

            await _storage.UpdateAsync(id1, "A2");

            await _storage.UpdateAsync(id2, "B2");

            // Even though the items have been updated, the result will be fetched from the cache.
            result = await _autoCache.ReadAllAsync();

            Assert.IsNotNull(result);
            enumerable = result as string[] ?? result.ToArray();
            Assert.AreEqual(2, enumerable.Length);
            Assert.IsTrue(enumerable.Contains("A1"), "Missing A1 in " + string.Join(", ", enumerable));
            Assert.IsTrue(enumerable.Contains("B1"), "Missing B1 in " + string.Join(", ", enumerable));
        }