Building a Simple Little “Cache”

I think the most fun part about programming are these little eureka moments when you build something useful, whether that’s a stunningly powerful algorithm or the simplest little hack. Here's a quick story.

I built a basic little site called What Was On TV Last Night?

https://whatwasontvlastnight.com/

The only purpose of this site is to grab and send last night’s TV episodes and email the list out to the subscribers. I built this for myself so I didn’t have to spend any time remembering when certain shows were on, when they come back to air after hiatus and thus never miss anything I like to watch.

For quite some time, I could not get this to work at all (since I’m a novice coder at best) but I persisted in an attempt to improve.

I had to hack MailChimp a bit, and struggle with ChatGPT (posts on these topics are in the works).

After the newsletter was finally working, I found a very useful API (TVMaze) which provides the art for each show. However, the API has a limitation that made this VERY slow. To load all the art for any day would take 3-5 minutes. This is in fact too slow to use in MailChimp at all. It would time out in the campaign creation process and not let me send any emails fueled with this content.

So, I built a simple little “cache” hack to get around this limitation.

At 4am, the newsletter content gets built by a cron job pinging a file and the content is dumped into a static HTML file created on the fly. The server only has to ping one time every day and update the static file.

From there, the site can show a sample and the newsletter can send since it now loads nearly instantly.

Sample code is below the post if you want to use any of it!  It’s very useful if you have a reason to replicate the same content over and over but don’t want to ping the data sources every time. Fundamentally, it’s the same concept as why you would use a caching system.  

My favorite part about programming is solving a problem – first, I wanted to make it easier to simply know what shows I was missing. Next, I needed to get an email to automatically send.  Finally optimizing and making it faster led to this simple little hack to reduce the load time and minimize the server and API usage. 

This is what makes coding fun.

Here’s the code you can use:

This code can be put into a PHP file to generate the static HTML file.
				
					<?php

// The URL you want to fetch
$url = 'REPLACE WITH YOUR URL';

// Use file_get_contents to fetch the URL content
$content = file_get_contents($url);

// Check if the content was fetched successfully
if ($content === FALSE) {
    // Handle the error
    echo "Failed to fetch the URL";
} else {
    // Define the name of the output file
    $outputFilename = 'REPLACE WITH YOUR FILENAME i.e OUTPUT.HTML';

    // Attempt to write the content to the file
    if (file_put_contents($outputFilename, $content) === FALSE) {
        // Handle the error
        echo "Failed to write the content to the file";
    } else {
        echo "The file was written successfully";
    }
}
?>
				
			
You can use this little snippet to display your generated HTML file.
				
						<?php
	include('REPLACE WITH THE PATH TO YOUR STATIC HTML FILE');
	?>
				
			

Leave a Reply

Your email address will not be published. Required fields are marked *

Hello

Contact

Newsletter #1

Here are a few cool links!

🎵 Check out this great song and absurdly well-done music video, JoJi – Glimpse of Us.

🍿 Everything, Everywhere, All at Once is a fantastic movie; 10/10.

✨ Amazing video and explanation of Hubble Telescope images.

🎶 Heardle is wordle for music. Pretty fun.

🕸️ Most popular websites since 1993 visualization.

📺 We Own This City is a cool new show. Baltimore po-leece. Lots of ex-Wire cast.