public async Task<ActionResult> PopulateCache() {
            HttpResponseMessage result = await new HttpClient().GetAsync("http://apress.com");
            long? data = result.Content.Headers.ContentLength;
            //DateTime expiryTime = DateTime.Now.AddSeconds(30);
            //HttpContext.Cache.Insert("pageLength", data, null, expiryTime, Cache.NoSlidingExpiration);
           // TimeSpan idleDuration = new TimeSpan(0, 0, 30);
            //HttpContext.Cache.Insert("pageLength", data, null, Cache.NoAbsoluteExpiration, idleDuration);
            


      
            SelfExpiringData<long?> seData = new SelfExpiringData<long?>(data, 3);


            //CacheDependency fileDep = new CacheDependency(Request.MapPath("~/data.txt"));
            //AggregateCacheDependency aggDep = new AggregateCacheDependency();
            //aggDep.Add(seData, fileDep);

          //  HttpContext.Cache.Insert("pageLength", seData, seData, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
            //    CacheItemPriority.Normal, HandleRemovalNotification);

            HttpContext.Cache.Insert("pageLength", seData, seData,
                Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, HandleUpdateNotification);
                


            //DateTime timestamp = DateTime.Now;
            //CacheDependency timestampDependency = new CacheDependency(null, new[] { "pageLength" });
            //HttpContext.Cache.Insert("pageTimestamp", timestamp, timestampDependency);



            return RedirectToAction("Index");
        }
        public ActionResult Index()
        {
            SelfExpiringData <long?> seData =
                (SelfExpiringData <long?>)HttpContext.Cache["pageLength"];

            return(View(seData == null ? null : seData.Value));
        }
Esempio n. 3
0
        public async Task <ActionResult> PopulateCache()
        {
            HttpResponseMessage result = await new HttpClient().GetAsync("http://apress.com");
            long?data = result.Content.Headers.ContentLength;
            SelfExpiringData <long?> seData = new SelfExpiringData <long?>(data, 3);

            HttpContext.Cache.Insert("pageLength", seData, seData);
            return(RedirectToAction("Index"));
        }
        private void HandleUpdateNotification(string key, CacheItemUpdateReason reason, out object expensiveObject, out CacheDependency dependency, 
            out DateTime absoluteExpiration, out TimeSpan slidingExpiration) {
            System.Diagnostics.Debug.WriteLine("Item {0} removed ({1})", key, Enum.GetName(typeof(CacheItemUpdateReason), reason));
            expensiveObject = dependency = new SelfExpiringData<long?>(GetData(false).Result, 3);

            absoluteExpiration = Cache.NoAbsoluteExpiration;
            slidingExpiration = Cache.NoSlidingExpiration;
            
        }
        public async Task<ActionResult> PopulateCache() {

            HttpResponseMessage result
                = await new HttpClient().GetAsync("http://apress.com");
            long? data = result.Content.Headers.ContentLength;

            SelfExpiringData<long?> seData = new SelfExpiringData<long?>(data, 3);
            HttpContext.Cache.Insert("pageLength", seData, seData,
                Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
                    HandleNotification);
            return RedirectToAction("Index");
        }
        public async Task <ActionResult> PopulateCache()
        {
            HttpResponseMessage result = await new HttpClient().GetAsync("http://apress.com");
            long?data = result.Content.Headers.ContentLength;

            SelfExpiringData <long?> seData = new SelfExpiringData <long?>(data, 3);

            HttpContext.Cache.Insert("pageLength", seData, seData, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
                                     CacheItemPriority.Normal, HandleNotification);

            return(RedirectToAction("Index"));
        }
        public async Task <ActionResult> PopulateCache()
        {
            HttpResponseMessage result = await new HttpClient().GetAsync("http://apress.com");
            long?data = result.Content.Headers.ContentLength;

            SelfExpiringData <long?> seData  = new SelfExpiringData <long?>(data, 3);
            CacheDependency          fileDep = new CacheDependency(Request.MapPath("~/data.txt"));
            AggregateCacheDependency aggDep  = new AggregateCacheDependency();

            aggDep.Add(seData, fileDep);
            HttpContext.Cache.Insert("pageLength", seData, aggDep);

            return(RedirectToAction("Index"));
        }
Esempio n. 8
0
        private void HandleNotification(string key,
                                        CacheItemUpdateReason reason,
                                        out object data,
                                        out CacheDependency dependency,
                                        out DateTime absoluteExpiry,
                                        out TimeSpan slidingExpiry)
        {
            System.Diagnostics.Debug.WriteLine("Item {0} removed. ({1})", key, Enum.GetName(typeof(CacheItemRemovedReason), reason));

            data = dependency =
                new SelfExpiringData <long?>(GetData(false).Result, 3);

            slidingExpiry  = Cache.NoSlidingExpiration;
            absoluteExpiry = Cache.NoAbsoluteExpiration;
        }