Jump to content

Cache Question


JohnOP

Recommended Posts

I have a question about caching files, the way i have seen people do it on tutorials is they save the file to a cache/ folder if non exists,  then check if it exists when the page loads so if it doesn't then load the file normally or if it does then include the cache/file but what is the difference? if the browser has to load the file every time someone goes to it so you cache it for better performance isn't including the cache/file just the exact same thing? the server still has to download the included cached file.

Link to comment
Share on other sites

Do you have a specific example? How you cache something depends on what it is and how 'expensive' it is to produce/reproduce.

 

Reading a remote file would involve saving the whole actual file in the cache so that you don't need to reread the remote file. Caching a web page that uses an 'expensive' database query would involve saving the resultant HTML page so that you can just output the resultant HTML without executing the query again. Caching a parsed/tokenized version of a template would save the time needed to parse/tokenize it, but would still require reeding it into the template class so that the resultant code can be executed at runtime.

Link to comment
Share on other sites

but what is the difference?

 

There are different reasons for caching.  One reason is to preserve bandwidth by letting browser store local copies of resources so they do not have to re-request them.  This is something the browser manages automatically.

 

Another reason is to cache the results of an expensive operation.  For example, resizing a large image into a thumbnail is an expensive operation and can put a lot of stress on a server if it is constantly resizing an image.  As such, it is beneficial to do the resize once and save the result in a file.  That way for subsequent requests can simple use the cached results rather than having to repeat the resize.

 

You can code your resize script so that it checks for the cached results file and if so, just outputs it.  If it does not exist yet, then it can create it.

 

Link to comment
Share on other sites

Now that we know what you are doing and what the general problem is you are trying to solve, how big are these image files, because the time taken to send them from the web server to the browser could be many times more than the extra overhead due to storying them in a database, and caching the image on the server as a file won't help if the problem is due to the transmission time.

 

Also, if the overhead of retrieving the image from the database and dynamically outputting it using php code is where the problem is at, and you want to cache the image as an actual file in the file system to solve the problem, doesn't that suggest that images should be stored as files in the first place and should not be stored in a database?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.