Example #1
0
        public Func<IEnumerable<CacheElement>> PrepareToExecute(IEnumerable<string> toSelect)
        {
            var selectList = toSelect.ToList();
            if (selectList.Count == 0) return () => new List<CacheElement>();

            var selectOp = selectOps[selectList.Count - 1];
            var now = sched.Now;

            return (() => 
            {
                var result = new List<CacheElement>();
                try 
                {
                    for (int i = 0; i < selectList.Count; i++) 
                    {
                        this.Checked(raw.sqlite3_bind_text(selectOp, i+1, selectList[i]));
                    }

                    while (this.Checked(raw.sqlite3_step(selectOp)) == SQLite3.Result.Row) 
                    {
                        var ce = new CacheElement() {
                            Key = raw.sqlite3_column_text(selectOp, 0), 
                            TypeName = raw.sqlite3_column_text(selectOp, 1), 
                            Value = raw.sqlite3_column_blob(selectOp, 2),
                            Expiration = new DateTime(raw.sqlite3_column_int64(selectOp, 3)),
                            CreatedAt = new DateTime(raw.sqlite3_column_int64(selectOp, 4)),
                        };

                        if (now.UtcTicks <= ce.Expiration.Ticks) result.Add(ce);
                    }
                } 
                finally 
                {
                    this.Checked(raw.sqlite3_reset(selectOp));
                }

                return result;
            });
        }
Example #2
0
        /// <summary>
        /// Takes the hash identifying the urls of the files that make up a combined file.
        /// Returns the compressed content of the combined files, and the version ID
        /// of the combined files. The version ID is based on the last modified time of the last
        /// modified file file that goes into the combined file.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="urlsHash"></param>
        /// <param name="totalFileNames">
        /// The file names of the files read by this method get added to this list.
        /// If this is null, nothing is done with this parameter.
        /// </param>
        /// <param name="combinedFileContent">
        /// Content to be sent back to the browser. 
        /// Will be null if the content could not be retrieved, because the hash was not found in
        /// the Application object. This means that the file tag that caused the browser to 
        /// request this file was generated in JavaScript or appeared outside the head tag
        /// on the page. This will also happen in debug mode. 
        /// In this case, the name of the requested file matches an actual
        /// file on the server.
        /// </param>
        /// <param name="versionId"></param>
        private static void GetContentVersion(
            HttpContext context, 
            string urlsHash,
            UrlProcessor urlProcessor,
            List<string> totalFileNames,
            bool minifyCSS, bool minifyJavaScript,
            out string combinedFileContent,
            out string versionId, 
            out FileTypeUtilities.FileType fileType)
        {
            combinedFileContent = null;
            versionId = null;

            List<Uri> fileUrls;
            RetrieveFileUrls(context, urlsHash, out fileUrls, out fileType);
            if (fileUrls == null)
            {
                return;
            }

            CacheElement cacheElement = (CacheElement)context.Cache[urlsHash];
            if (cacheElement == null)
            {
                StringBuilder combinedContentSb = new StringBuilder();
                DateTime mostRecentModifiedTime = DateTime.MinValue;
                List<string> fileNames = new List<string>();
                bool fileMissing = false;

                foreach (Uri fileUrl in fileUrls)
                {
                    string filePath = MapPath(fileUrl.AbsolutePath, urlProcessor.ThrowExceptionOnMissingFile);
                    string fileContent = null;

                    if (filePath != null)
                    {
                        fileContent = File.ReadAllText(filePath);
                        if (fileType == FileTypeUtilities.FileType.CSS)
                        {
                            FixUrlProperties(ref fileContent, fileUrl, urlProcessor);
                        }

                        DateTime lastModifiedTime = File.GetLastWriteTime(filePath);

                        mostRecentModifiedTime =
                            (mostRecentModifiedTime > lastModifiedTime) ?
                                mostRecentModifiedTime : lastModifiedTime;

                        fileNames.Add(filePath);
                        if (totalFileNames != null) { totalFileNames.Add(filePath); }
                    }
                    else
                    {
                        // A comment starting with /*! doesn't get removed by the minifier
                        fileContent = string.Format("\n/*!\n** Does not exist: {0}\n*/\n", fileUrl);

                        fileMissing = true;
                    }

                    combinedContentSb.Append(fileContent);
                }

                string combinedContent = combinedContentSb.ToString();
                if (!string.IsNullOrEmpty(combinedContent))
                {
                    cacheElement = new CacheElement();

                    cacheElement.CombinedFileContent = combinedContent;
                    if (fileType == FileTypeUtilities.FileType.JavaScript)
                    {
                        if (minifyJavaScript)
                        {
                            cacheElement.CombinedFileContent = JavaScriptCompressor.Compress(combinedContent);
                        }
                    }
                    else
                    {
                        if (minifyCSS)
                        {
                            cacheElement.CombinedFileContent = CssCompressor.Compress(combinedContent);
                        }
                    }

                    cacheElement.VersionId = VersionId(mostRecentModifiedTime);

                    // Cache the newly created cacheElement
                    // 
                    // Do not cache the cacheElement if one of the files couldn't be found.
                    // That way, the package will keep checking the missing file, and pick it up
                    // when someone puts the file there.
                    if (!fileMissing)
                    {
                        CacheDependency cd = new CacheDependency(fileNames.ToArray());
                        context.Cache.Insert(urlsHash, cacheElement, cd);
                    }
                }
            }

            if (cacheElement == null)
            {
                if (context.IsDebuggingEnabled) { throw new Exception("cacheElement == null"); }

                combinedFileContent = "";
                versionId = "";
            }
            else
            {
                combinedFileContent = cacheElement.CombinedFileContent;
                versionId = cacheElement.VersionId;
            }
        }