Yahweh Posted April 26, 2006 Share Posted April 26, 2006 I have a website that stores cached copies of webpages on my server, so that I can serve the cached copies of the pages without having to process server-side code again. Normally, I just keep the copies in a folder called /cache like this /cache 0000001.html 0000002.html 0000003.html... However, I was wondering if it makes a difference if I stored the files in subfolders, where each subfolder contains 100 caches: /cache /000-100 0000001.html 0000002.html 0000003.html ... /101-200 0000101.html 0000102.html 0000103.html ... /301-400 0000301.html 0000302.html 0000303.html ... ... I figure, if I had a 1000 (or a million) caches, I could serve them up faster by putting them in folders of about 100 each.The logic behind this idea works like this: if I had 1000 caches in a single folder, then the server has to search through 1000 filenames (at least 1000 operations). If I had them in groups of 100, the server has to pick 1 of the 10 folders (at least 10 operations) then search through 100 filenames (100 more operations). I cut the server load by almost 90%, which is noticeable for extremely popular websites.Is my reasoning correct, or do I completely misunderstand the way servers operate? Link to comment Share on other sites More sharing options...
boen_robot Posted April 26, 2006 Share Posted April 26, 2006 I think you've got the speeding correctly, but what about the cache itself? I can't figure out how is the server going to know when to use the server side code and when not, and isn't this kind of check slow by itself?If there is an explainable and proven answer to this (in other words: if my question is stupid), please tell me at least in PM . Link to comment Share on other sites More sharing options...
Yahweh Posted April 30, 2006 Author Share Posted April 30, 2006 I think you've got the speeding correctly, but what about the cache itself? I can't figure out how is the server going to know when to use the server side code and when not, and isn't this kind of check slow by itself?If there is an explainable and proven answer to this (in other words: if my question is stupid), please tell me at least in PM .<{POST_SNAPBACK}> Oops, sorry I didn't get to this sooner :)The check for cacheing isn't that slow, as long as you do it right. I write everything in ASP, which has a FileScriptingObject that lets you check when a file was created. I generally use this code:Option ExplicitDim CacheTimeCacheTime = 3 'hoursPrivate Sub showPage(ByRef strCache) dim objFSO, objFile, DoCache Dim writeNewCache writeNewCache = True set objFSO = Server.CreateObject("Scripting.FileSystemObject") if objFSO.FileExists(Server.MapPath(strCache)) then if DateDiff("h", objFSO.GetFile(Server.MapPath(strCache)).DateLastModified, Now()) < CacheTime then writeNewCache = False end if end if DoCache = cbool(lcase(request("mode")) = lcase("recalc")) if writeNewCache = True or DoCache = True then server.transfer("buildfile.asp") 'I build the file, then write it to disk end if 'set objFSO = nothing Server.Transfer(strCache)End SubCall ShowPage("somepage.html") Building the file and writing it to disk takes about as long as executing it on every page load, but with the extra 2 or 3 milliseconds of time it takes to build and write the file is acceptable (it has the same affect as setting Response.Buffer = True), because you only have to build it just once per whatever time interval you set, as opposed to building it on each page load. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now