In any large database, it is important to cache your queries. Database calls can be quite expensive, even after careful tuning.
In the database that I set up for this, there are over two million rows, and queries can take seconds to complete (tens of seconds if no indexing is done).
While modern databases do include caching engines for popular queries, it is much better to simply open and read a file that contains cached information that translates to "There are 2673762 rows in this database" than to have the database actually count the rows.
We will create a very simple caching mechanism that takes the requested query, encodes the query to a string using MD5, then returns the cached query if it exists, and creates the cache if not. Remember, MD5 returns a pseudo-random string of characters that can be used to save a cache with a unique name.
A nice thing about cities and countries is that the data does not change very quickly. So, it would not be necessary to clear...