JohnOP Posted November 27, 2011 Share Posted November 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251877-cache-question/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 27, 2011 Share Posted November 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251877-cache-question/#findComment-1291519 Share on other sites More sharing options...
JohnOP Posted November 27, 2011 Author Share Posted November 27, 2011 Well mostly i want to cache images that never change. Quote Link to comment https://forums.phpfreaks.com/topic/251877-cache-question/#findComment-1291525 Share on other sites More sharing options...
newb Posted November 27, 2011 Share Posted November 27, 2011 afaik, image caching is done by most browsers automatically anyway so u dont have to worry about that. if ur talking about image preloading (for rollover effects or something), check out javascript/jquery Quote Link to comment https://forums.phpfreaks.com/topic/251877-cache-question/#findComment-1291526 Share on other sites More sharing options...
kicken Posted November 27, 2011 Share Posted November 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251877-cache-question/#findComment-1291528 Share on other sites More sharing options...
JohnOP Posted November 27, 2011 Author Share Posted November 27, 2011 The images i have are resized in css and are pulled from the database for each user but when vieweing the page the image slowly loads in like its lagging in. Quote Link to comment https://forums.phpfreaks.com/topic/251877-cache-question/#findComment-1291535 Share on other sites More sharing options...
PFMaBiSmAd Posted November 27, 2011 Share Posted November 27, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/251877-cache-question/#findComment-1291556 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.