Quick Cache code implementation in PHP for SEO

Making website quick and efficient to load helps in SEO. Good SEO means better ranking and more visitors. Google SEO believes in faster loading of websites as a plus point. How cache (caching pages or some part of a page) could help in making page load fast?

When a webpage is requested, it executes codes on server (takes time to process queries, SQL) and returns result. All this time from request to complete loading constitutes total time for a webpage to load. When there are lots of SQL queries to be executed First Byte sent is quite delayed. Cache helps making First Byte faster as less of SQL queries are being executed.

Let us take an example to explain. Say there is a designs page where webmaster/owner has shared multiple products design with details and images. A product would have a name, category, theme, tag, features, attributes, properties, size, price etc. There are say 100 products on website with 10 products displayed at a time on a page. With pagination other products can be checked. When first page is loaded (which shows 10 products) lots of SQL queries would be used to fetch all required data. When the contents of this page doesn’t change very often (like in every minute or so) then for representing same data all SQL queries are executed time and again. With cache the page would be stored as HTML (generally) and contents downloaded to browser without running much SQL queries. Generally caching is done for static content like HTML pages, images, stylesheet, javascript. Here, we are doing it for PHP pages.

Following can be done with cache execution:

  • Complete page cache or a page block can be cached
  • Time based cache (when not to do caching)
  • Pagination cache
  • Duration based cache (for how long to show cached page)
  • Clear cache page
  • No caching during search queries (if this design page has search implemented based on category, price then caching should not work)

Good thing about this simple cache would be that a block can also be cached form PHP page. Along with 10 products on each page there is say list of all categories (say 50) showed on right of page. Each time any paginated page loads from designs then the list of categories are fetched and displayed. Considering this list as a block, it can be cached separately from products list.

In this blog post, I would be sharing some PHP code to implement simple cache system on a website.

Leave a Comment