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);
        }
        public IAsyncResult BeginRetrieve(CacheRequest request, AsyncCallback callback, object state)
        {
            CacheResponse response = null;

            CacheAsyncResult cacheAsyncResult = new CacheAsyncResult {
                FragmentUrl = request.CanonicalUri.ToString()
            };

            cacheAsyncResult.Complete(response, true);
            return(cacheAsyncResult);
        }
        public IAsyncResult BeginPersist(CacheRequest request, CacheResponse response, AsyncCallback callback, object state)
        {
            CacheAsyncResult cacheAsyncResult = new CacheAsyncResult();

            if (!this.cache.ContainsKey(request.CanonicalUri.ToString()) && !request.CanonicalUri.ToString().ToUpperInvariant().EndsWith(".ISML/MANIFEST") && !request.CanonicalUri.ToString().ToUpperInvariant().EndsWith(".ISM/MANIFEST"))
            {
                cacheAsyncResult.FragmentUrl = request.CanonicalUri.ToString();
                cacheAsyncResult.Complete(response, true);
                return(cacheAsyncResult);
            }

            cacheAsyncResult.Complete(null, true);
            return(cacheAsyncResult);
        }
        public CacheResponse EndRetrieve(IAsyncResult ar)
        {
            CacheResponse primaryResponse = null;

            if (this.primary != null)
            {
                primaryResponse = this.primary.EndRetrieve(ar);
            }

            if (this.secondary != null)
            {
                CacheResponse secondaryResponse = this.secondary.EndRetrieve(ar);

                if ((secondaryResponse == null || secondaryResponse.ContentLength == 0) && (primaryResponse != null && primaryResponse.ContentLength > 0))
                {
                    CacheAsyncResult cacheAsyncResult = ar as CacheAsyncResult;

                    if (cacheAsyncResult != null)
                    {
                        cacheAsyncResult.Result = primaryResponse;

                        this.secondary.EndPersist(cacheAsyncResult);
                    }
                }

                if ((primaryResponse == null || primaryResponse.ContentLength == 0) && (secondaryResponse != null && secondaryResponse.ContentLength > 0))
                {
                    CacheAsyncResult cacheAsyncResult = ar as CacheAsyncResult;

                    if (cacheAsyncResult != null && this.primary != null)
                    {
                        cacheAsyncResult.Result = secondaryResponse;

                        this.primary.EndPersist(cacheAsyncResult);
                    }

                    primaryResponse = secondaryResponse;
                }
            }

            return(primaryResponse);
        }
Beispiel #5
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);
        }
Beispiel #6
0
        public CacheResponse EndRetrieve(IAsyncResult asyncResult)
        {
            asyncResult.AsyncWaitHandle.WaitOne();

            CacheAsyncResult cacheAsyncResult = asyncResult as CacheAsyncResult;

            if (cacheAsyncResult != null && this.cache.ContainsKey(cacheAsyncResult.FragmentUrl) && !cacheAsyncResult.FragmentUrl.ToUpperInvariant().EndsWith(".ISML/MANIFEST") && !cacheAsyncResult.FragmentUrl.ToUpperInvariant().EndsWith(".ISM/MANIFEST"))
            {
                string filename = (string)this.cache[cacheAsyncResult.FragmentUrl].CachedValue;

                Stream stream = this.persitenceService.Retrieve(filename);
                if (this.cache[cacheAsyncResult.FragmentUrl].AvoidDeserialization)
                {
                    return(new CacheResponse(stream.Length, "text/xml", null, stream, HttpStatusCode.OK, "OK", DateTime.Now));
                }

                return(new CacheResponse(stream, true));
            }

            return(new CacheResponse(0, null, null, null, HttpStatusCode.NotFound, "Not Found", DateTime.Now));
        }
        public CacheResponse EndRetrieve(IAsyncResult asyncResult)
        {
            asyncResult.AsyncWaitHandle.WaitOne();

            CacheAsyncResult cacheAsyncResult = asyncResult as CacheAsyncResult;

            if (cacheAsyncResult != null && this.cache.ContainsKey(cacheAsyncResult.FragmentUrl) && !cacheAsyncResult.FragmentUrl.ToUpperInvariant().EndsWith(".ISML/MANIFEST") && !cacheAsyncResult.FragmentUrl.ToUpperInvariant().EndsWith(".ISM/MANIFEST"))
            {
                Stream stream = (Stream)this.cache[cacheAsyncResult.FragmentUrl].CachedValue;

                stream.Position = 0;

                if (this.cache[cacheAsyncResult.FragmentUrl].AvoidDeserialization)
                {
                    return(new CacheResponse(stream.Length, "text/xml", null, stream, HttpStatusCode.OK, "OK", DateTime.Now));
                }
                else
                {
                    return(new CacheResponse(stream, true));
                }
            }

            return(new CacheResponse(0, null, null, null, System.Net.HttpStatusCode.NotFound, "Not Found", DateTime.Now, false));
        }