I have a webpage that uses AJAX, MySQL, and a simple PHP file to grab content for the body of my site and place it in the body of the page. Basically, the entire site is one, dynamic page that utilizes jQuery and the history plugin to keep all the links bookmarkable and Back/Fo开发者_Python百科rward capable.
I want to optimize my site to use the least amount of resources possible (server-side). Right now, anytime someone clicks a link to another "page" on my site, the PHP page is called and a database connection is created, the content is grabbed from the database, and then placed on the page with JavaScript.
Would it be better to instead have the PHP file grab a cached file that contains the content and then send that to the browser?
I still want my pages to be as up to date as possible, though, so I was thinking instead to have a column in the table with my content that says its modification date, and, if the cached file is older, load the data in the table and replace the cached file. However, that would make the PHP script both create a database connection AND check the file modification time of the cached file.
What's the best solution?
When you update the data in the database also remove any cached version of the corresponding data.
This way you can have the php file check if a cached version of the file exists. If there isn't one then connect to the db and cache that data and return it else just return the cached version. This way you only establish a database connection if no cached version exists.
精彩评论