Emil CHERICHES

Emil CHERICHES

Why caching is good

July 9, 2013 Emil C
No Comments

What is caching?

Basically is a way of saving computational power when a server that hosts an app is serving the same content several times, to several users. Our app would normally save the output of some internal work either on the server’s hard drive as files either in memory in order to be able to serve the same contents all over again much faster. Of course, the concept of thees savings include an expiring time, after which the saved content is generated again.

In web development caching is typically a good thing. For instance, on a heavily visited news site, where thousands of visitors see the same page probably in the same second even an one minute cache could save a tremendous amount of computing power.

So, this article is just a proof of concept. For this I have set a web host, I am using Nginx,PHP,MariaDB. I installed the Smarty templates engine, which has it’s own disk caching engine.

I created a config.php file which contains basic app configuration information like sql credentials and default caching time.

My functions.php file contains a Template() class that extends the Smarty() class, in fact it defines working folders for Smarty and enables caching. The other class SQLi() extends the mysqli() class in order to facilitate work with MySQL queries.

capture_005_09072013_122245

I also imported the Geonames database just to have some heavy contents in my database. So, all my template does is to show on the screen a huge array extracted form this database.

capture_007_09072013_122322

My index.php is very simple. First of all we count the execution time (I used this example to do that), Then I initialize my classes and I check to see if the template isn’t already cached.

capture_006_09072013_122312

If it is, I would just send it to the browser, if not I would query the database for my Informations and then send it to the browser. On the very bottom I would also show the execution time.

And some results:

capture_009_09072013_122833

As you could see on the screenshot, on the first execution it would take the server about 3 seconds to query the database, generate the page and then send it to the browser. On the second and third execution however the actual time to “generate” the page on the server was about 0.1 seconds, which is by far better. As you could see this page actually sends 17 MB of information to the browser.

In real world situations it is probably impossible to just send the whole page as a cached one, you have probably some bits of information that might change on every load of your web page, but in fact it is good to know about and even use some internal caching, at least for some areas of your web pages.

Web development cachephpsmarty
Previous Post

Basic OS RAM Memory consumption

Next Post

TIP: Make Postfix use less SQL connections

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts
  • Debian on BTRFS with subvolumes
  • NixOS TIP: OTP in Gnome login screen
  • Arch to Manjaro – the dirty way
  • The anatomy of a smart bulb
  • OpenVPN on 443
Categories
  • from the web
  • just blog
  • linux
    • Debian
    • NixOS
  • phones
  • Phones & Tablets
  • programing
  • security
  • Smart Home
  • Tips & Tricks
  • Uncategorized
  • Web development
  • Windows
Blogroll
  • cheriches.fr
Subscribe by Email
Recent Posts
  • Debian on BTRFS with subvolumes
  • NixOS TIP: OTP in Gnome login screen
  • Arch to Manjaro – the dirty way
  • The anatomy of a smart bulb
  • OpenVPN on 443
Categories
  • from the web (3)
  • just blog (1)
  • linux (20)
    • Debian (1)
    • NixOS (1)
  • phones (1)
  • Phones & Tablets (2)
  • programing (1)
  • security (2)
  • Smart Home (1)
  • Tips & Tricks (16)
  • Uncategorized (1)
  • Web development (1)
  • Windows (1)
Blogroll
  • cheriches.fr
Tags cloud
adb ADS aircrack-ng Android Apache apt-get Arch BTRFS CentOS Chrome Cluster CSS debian Docker Firefox firmware flashing GNOME Google Authenticator High Availability HTTPS javascript KVM linux Manjaro MySQL OpenBeken OpenBK7231T OpenVPN OTP php piwik Postfix Proxy_ARP release RHEL Samba ssh Tuya ubuntu UEFI VPN VRRP windows Youtube
Recent Comments
  • Greg M on The anatomy of a smart bulb
  • The anatomy of a smart bulb #LED @EmilsBits « Adafruit Industries – Makers, hackers, artists, designers and engineers! on The anatomy of a smart bulb
  • Emil C on The anatomy of a smart bulb
  • David Brower on The anatomy of a smart bulb
  • 智能灯泡的解剖 - 偏执的码农 on The anatomy of a smart bulb
Proudly powered by WordPress | Theme: Fmi by Forrss.
Manage Cookie Consent
We use cookies to optimize our website and our service.
Functional cookies Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}