public bool EndPersist(IAsyncResult asyncResult)
        {
            asyncResult.AsyncWaitHandle.WaitOne();

            CacheAsyncResult cacheAsyncResult = asyncResult as CacheAsyncResult;

            if (cacheAsyncResult != null && cacheAsyncResult.Result != null)
            {
                if (!this.cache.ContainsKey(cacheAsyncResult.FragmentUrl))
                {
                    Stream        memoryStream  = new MemoryStream();
                    CacheResponse cacheResponse = cacheAsyncResult.Result as CacheResponse;

                    if (cacheResponse != null && cacheResponse.StatusCode == HttpStatusCode.OK)
                    {
                        cacheResponse.WriteTo(memoryStream);
                        memoryStream.Position = 0;

                        CacheItem cacheItem = new CacheItem
                        {
                            CachedValue = memoryStream,
                            Date        = DateTime.Now
                        };

                        this.cache.Add(cacheAsyncResult.FragmentUrl, cacheItem);
                        this.OnCacheUpdated(cacheAsyncResult.FragmentUrl);

                        return(true);
                    }
                }
            }

            return(false);
        }
        private void OnManifestResponse(IAsyncResult ar)
        {
            var wreq = ar.AsyncState as WebRequest;

            if (wreq != null)
            {
                var wresp = (HttpWebResponse)wreq.EndGetResponse(ar);

                if (wresp.StatusCode != HttpStatusCode.OK)
                {
                    throw new Exception(wresp.StatusDescription);
                }

                Stream respStream = wresp.GetResponseStream();

                if (!Directory.Exists(RootFolderPath))
                {
                    Directory.CreateDirectory(RootFolderPath);
                }
                //open a filestream
                using (var fs = new FileStream(ManifestFilePath, FileMode.Create, FileAccess.ReadWrite))
                {
                    //create a CacheResponse
                    var cacheResp = new CacheResponse(respStream.Length, wresp.ContentType, null,
                                                      respStream, wresp.StatusCode, wresp.StatusDescription,
                                                      DateTime.UtcNow);
                    //serialize to the file
                    cacheResp.WriteTo(fs);
                    fs.Flush();
                    fs.Close();
                }
            }
        }
Example #3
0
        public bool EndPersist(IAsyncResult asyncResult)
        {
            asyncResult.AsyncWaitHandle.WaitOne();

            CacheAsyncResult cacheAsyncResult = asyncResult as CacheAsyncResult;

            if (cacheAsyncResult != null && cacheAsyncResult.Result != null)
            {
                CacheResponse cacheResponse = cacheAsyncResult.Result as CacheResponse;

                if (cacheResponse != null && cacheResponse.Response != null && !this.cache.ContainsKey(cacheAsyncResult.FragmentUrl) && cacheResponse.StatusCode == HttpStatusCode.OK)
                {
                    string fileGuid = Guid.NewGuid().ToString();

                    using (MemoryStream stream = new MemoryStream())
                    {
                        cacheResponse.WriteTo(stream);
                        stream.Position = 0;

                        bool result = this.persitenceService.Persist(fileGuid, stream);

                        if (result)
                        {
                            CacheItem cacheItem = new CacheItem
                            {
                                CachedValue = fileGuid,
                                Date        = DateTime.Now
                            };

                            this.cache.Add(cacheAsyncResult.FragmentUrl, cacheItem);
                            this.OnCacheUpdated(cacheAsyncResult.FragmentUrl);

                            this.persitenceService.AddApplicationSettings(cacheAsyncResult.FragmentUrl, cacheItem);

                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
        private void OnManifestResponse(IAsyncResult ar)
        {
            var wreq = ar.AsyncState as WebRequest;
            if (wreq != null)
            {
                var wresp = (HttpWebResponse) wreq.EndGetResponse(ar);

                if (wresp.StatusCode != HttpStatusCode.OK)
                    throw new Exception(wresp.StatusDescription);

                Stream respStream = wresp.GetResponseStream();

                if (!Directory.Exists(RootFolderPath))
                    Directory.CreateDirectory(RootFolderPath);
                //open a filestream
                using (var fs = new FileStream(ManifestFilePath, FileMode.Create, FileAccess.ReadWrite))
                {
                    //create a CacheResponse
                    var cacheResp = new CacheResponse(respStream.Length, wresp.ContentType, null,
                        respStream, wresp.StatusCode, wresp.StatusDescription,
                        DateTime.UtcNow);
                    //serialize to the file
                    cacheResp.WriteTo(fs);
                    fs.Flush();
                    fs.Close();
                }
            }
        }