Exemple #1
0
        public IEnumerable <FeaturedMovies> GetFeaturedMovies(out IExpirationTrigger expirationTrigger)
        {
            _featuredMoviesTokenSource = new CancellationTokenSource();

            expirationTrigger = new CancellationTokenTrigger(_featuredMoviesTokenSource.Token);
            return(GetMovies().OrderBy(m => m.Rank).Take(2));
        }
Exemple #2
0
 /// <summary>
 /// Expire the cache entry if the given event occurs.
 /// </summary>
 /// <param name="options"></param>
 /// <param name="trigger"></param>
 public static MemoryCacheEntryOptions AddExpirationTrigger(
     this MemoryCacheEntryOptions options,
     [NotNull] IExpirationTrigger trigger)
 {
     options.Triggers.Add(trigger);
     return(options);
 }
        public IEnumerable<FeaturedMovies> GetFeaturedMovies(out IExpirationTrigger expirationTrigger)
        {
            _featuredMoviesTokenSource = new CancellationTokenSource();

            expirationTrigger = new CancellationTokenTrigger(_featuredMoviesTokenSource.Token);
            return GetMovies().OrderBy(m => m.Rank).Take(2);
        }
 public void AddExpirationTrigger(IExpirationTrigger trigger)
 {
     if (Triggers == null)
     {
         Triggers = new List<IExpirationTrigger>(1);
     }
     Triggers.Add(trigger);
 }
 public void AddExpirationTrigger(IExpirationTrigger trigger)
 {
     if (Triggers == null)
     {
         Triggers = new List <IExpirationTrigger>(1);
     }
     Triggers.Add(trigger);
 }
 public string GetProducts(string category, out IExpirationTrigger trigger)
 {
     var token = _tokenSource.IsCancellationRequested ? CancellationToken.None :
                                                        _tokenSource.Token;
     trigger = new CancellationTokenTrigger(token);
     if (category == "Books")
     {
         return "Book1, Book2";
     }
     else
     {
         return "Laptops";
     }
 }
Exemple #7
0
        public string GetProducts(string category, out IExpirationTrigger trigger)
        {
            var token = _tokenSource.IsCancellationRequested ? CancellationToken.None :
                        _tokenSource.Token;

            trigger = new CancellationTokenTrigger(token);
            if (category == "Books")
            {
                return("Book1, Book2");
            }
            else
            {
                return("Laptops");
            }
        }
        public string GetCriticsQuote(out IExpirationTrigger trigger)
        {
            _quotesTokenSource = new CancellationTokenSource();

            var quotes = new[]
            {
                "A must see for iguana lovers everywhere",
                "Slightly better than watching paint dry",
                "Never felt more relieved seeing the credits roll",
                "Bravo!"
            };

            trigger = new CancellationTokenTrigger(_quotesTokenSource.Token);
            return quotes[_random.Next(0, quotes.Length)];
        }
Exemple #9
0
        public string GetCriticsQuote(out IExpirationTrigger trigger)
        {
            _quotesTokenSource = new CancellationTokenSource();

            var quotes = new[]
            {
                "A must see for iguana lovers everywhere",
                "Slightly better than watching paint dry",
                "Never felt more relieved seeing the credits roll",
                "Bravo!"
            };

            trigger = new CancellationTokenTrigger(_quotesTokenSource.Token);
            return(quotes[_random.Next(0, quotes.Length)]);
        }
        public async Task Createdtrigger_Same_For_A_File_And_Callsback_AllRegisteredTriggers_OnChange()
        {
            var fileName     = Guid.NewGuid().ToString();
            var fileLocation = Path.Combine(Path.GetTempPath(), fileName);

            File.WriteAllText(fileLocation, "Content");
            var provider = new PhysicalFileProvider(Path.GetTempPath());

            var count           = 10;
            var tasks           = new List <Task>(count);
            var triggers        = new IExpirationTrigger[count];
            var callbackResults = new bool[count];

            for (int i = 0; i < count; i++)
            {
                tasks.Add(new Task(index =>
                {
                    var expirationTrigger = provider.Watch(fileName);
                    triggers[(int)index]  = expirationTrigger;
                    Assert.NotNull(expirationTrigger);
                    Assert.False(expirationTrigger.IsExpired);
                    expirationTrigger.RegisterExpirationCallback(_ => { callbackResults[(int)index] = true; }, index);
                }, state: i));
            }

            // Simulating multiple concurrent requests to the same file.
            Parallel.ForEach(tasks, task => task.Start());
            await Task.WhenAll(tasks);

            File.AppendAllText(fileLocation, "UpdatedContent");

            // Some warm up time for the callbacks to be fired.
            await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);

            for (int index = 1; index < count; index++)
            {
                Assert.Equal(triggers[index - 1], triggers[index]);
            }

            Assert.True(callbackResults.All(c => c));

            File.Delete(fileLocation);
        }
        public async Task Createdtrigger_Same_For_A_File_And_Callsback_AllRegisteredTriggers_OnChange()
        {
            var fileName = Guid.NewGuid().ToString();
            var fileLocation = Path.Combine(Path.GetTempPath(), fileName);
            File.WriteAllText(fileLocation, "Content");
            var provider = new PhysicalFileProvider(Path.GetTempPath());

            var count = 10;
            var tasks = new List<Task>(count);
            var triggers = new IExpirationTrigger[count];
            var callbackResults = new bool[count];

            for (int i = 0; i < count; i++)
            {
                tasks.Add(new Task(index =>
                {
                    var expirationTrigger = provider.Watch(fileName);
                    triggers[(int)index] = expirationTrigger;
                    expirationTrigger.ShouldNotBe(null);
                    expirationTrigger.IsExpired.ShouldNotBe(true);
                    expirationTrigger.RegisterExpirationCallback(_ => { callbackResults[(int)index] = true; }, index);
                }, state: i));
            }

            // Simulating multiple concurrent requests to the same file.
            Parallel.ForEach(tasks, task => task.Start());
            await Task.WhenAll(tasks);
            File.AppendAllText(fileLocation, "UpdatedContent");

            // Some warm up time for the callbacks to be fired.
            await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);

            for (int index = 1; index < count; index++)
            {
                triggers[index].ShouldBe(triggers[index - 1]);
            }

            callbackResults.All(c => c == true).ShouldBe(true);

            File.Delete(fileLocation);
        }
        public async Task ModifyContent_And_Delete_File_Succeeds_And_Callsback_Registered_Triggers()
        {
            var fileName     = Guid.NewGuid().ToString();
            var fileLocation = Path.Combine(Path.GetTempPath(), fileName);

            File.WriteAllText(fileLocation, "OldContent");
            var provider = new PhysicalFileProvider(Path.GetTempPath());
            var fileInfo = provider.GetFileInfo(fileName);

            Assert.Equal(new FileInfo(fileInfo.PhysicalPath).Length, fileInfo.Length);
            Assert.True(fileInfo.Exists);

            IExpirationTrigger trigger3 = null, trigger4 = null;
            var trigger1 = provider.Watch(fileName);
            var trigger2 = provider.Watch(fileName);

            // Valid trigger1 created.
            Assert.NotNull(trigger1);
            Assert.False(trigger1.IsExpired);
            Assert.True(trigger1.ActiveExpirationCallbacks);

            // Valid trigger2 created.
            Assert.NotNull(trigger2);
            Assert.False(trigger2.IsExpired);
            Assert.True(trigger2.ActiveExpirationCallbacks);

            // Trigger is the same for a specific file.
            Assert.Equal(trigger2, trigger1);

            trigger1.RegisterExpirationCallback(state =>
            {
                var infoFromState = state as IFileInfo;
                trigger3          = provider.Watch(infoFromState.Name);
                Assert.NotNull(trigger3);
                trigger3.RegisterExpirationCallback(_ => { }, null);
                Assert.False(trigger3.IsExpired);
            }, state: fileInfo);

            trigger2.RegisterExpirationCallback(state =>
            {
                var infoFromState = state as IFileInfo;
                trigger4          = provider.Watch(infoFromState.Name);
                Assert.NotNull(trigger4);
                trigger4.RegisterExpirationCallback(_ => { }, null);
                Assert.False(trigger4.IsExpired);
            }, state: fileInfo);

            // Write new content.
            File.WriteAllText(fileLocation, "OldContent + NewContent");
            Assert.True(fileInfo.Exists);
            // Wait for callbacks to be fired.
            await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);

            Assert.True(trigger1.IsExpired);
            Assert.True(trigger2.IsExpired);

            // Trigger is the same for a specific file.
            Assert.Equal(trigger4, trigger3);
            // A new trigger is created.
            Assert.NotEqual(trigger1, trigger3);

            // Delete the file and verify file info is updated.
            File.Delete(fileLocation);
            fileInfo = provider.GetFileInfo(fileName);
            Assert.False(fileInfo.Exists);
            Assert.False(new FileInfo(fileLocation).Exists);

            // Wait for callbacks to be fired.
            await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE);

            Assert.True(trigger3.IsExpired);
            Assert.True(trigger4.IsExpired);
        }