Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Help with Caching file to save on bandwidth

Featured Replies

Hi,

 

This is the page in question that I hope to apply caching too: http://fifasoundtrac...track/?s=fifa10

 

Let me explain what is happening, based on the users location I pull the song and artist names from my MySQL Database and then using these names I query the iTunes and YouTube API's to fetch download links and a video URL from YouTube which I use to provide a streaming link from inside my site.

 

You can see all this happening when you go to the page I've mentioned above and usually it takes around 20seconds to 'load' around 40 songs.

 

I've been working on this for ages as a personal project on my localhost server but I've just recently uploaded it online.

 

I've got about 5 people to test the page and noticed the it used around 2.9mb of bandwidth just for these 5 people. I don't really know if that is a lot of bandwidth to be honest but I'd like to cache the pages anyway to imrpove on the loading times.

 

I have been looking at this tutorial on php caching here: http://www.jonasjohn...php/caching.htm and I'm not sure it's exactly what I'm looking for.

 

The part of the process that takes the most time is querying YouTube and iTunes, from what I understand the method above would just create a static copy of the page elsewhere on the server which it would fetch based on the expire limit that has been set.

 

What I was thinking might be better would be to store the values of the iTunes download links and YouTube Video URL's as cookies on the users computer after they have run the page the first time and when they visit again just retrieve these values.

 

Obviously the whole point of making this page dynamic is that thing like YouTube Videos available and iTunes songs can change over time so I would want to set a limit on how long the cookie lasted.

 

So anyway if you've bothered to read all of that thank you and I hope I explained it well, just looking for people thoughts on whether this is possible/best method and maybe a few pointers in the right direction.

 

Thanks again, James :)

 

Edit:

Here is a section of my messy source code: http://pastie.org/pr...as0pze80ugvce7a

1. Create a directory in the folder this file in located.

 

2. Call it /cache

 

3. Give it 0777 permissions.

 

 

4. http://pastebin.com/d11d23c85 try that.

 

 

The code i've written very simply uses the md5 function to turn the url into a unique checksum. Once an request is made to Amazon, the output is stored on your server for $cache_time. Once that time expires (for that specific request) the next request will be fetched from amazon and the cycle will start again. This will save you from getting banned from Amazon's API and will also save you a tonne of bandwidth. Eventually you can integrate this into memcached.

 

 

You will notice the second time you "refresh" a page there will be a huge speed increase. So huge that you may wee a bit.

 

(this is the code i added.)


	// Start simple caching.
	$amazon_url	=	"http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch?term=".$cleanst."&limit=1";
	$md5url		=	md5($amazon_url);

	$cache_time	=	3600 * 24; // a day?

	if (file_exists('cache/'.$md5_url) && filemtime('cache/'.$md5_url)+$cache_time > time())
	{
		$json	=	file_get_contents('cache/'.$md5_url);
	}
	else
	{
		$json = file_get_contents("http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch?term=".$cleanst."&limit=1");

		$handle	=	fopen('cache/'.$md5_url.'','w');
		fwrite($handle,$json);
		fclose($handle);
	}

  • Author

Only just seen your post and have recently worked out an alternate solution using cookies.

 

It worked fine on my local testing server but when I uploaded it to my server I got this message:

 

Warning: Cannot modify header information - headers already sent by (output started at /home/james/public_html/soundtrack/soundtrack_gen.php:2) in /home/james/public_html/soundtrack/soundtrack_gen.phpon line 258

 

I read up on the issue and somewhere someone mentioned that:

 

Headers must be parsed before any HTML, and for some reason (Unknown by me, but I'm sure others can answer why) certain hosts don't like it when you place any HTML above header tags.

 

I tried to change my file to adhere to these rules but I still get the same error message..

 

Here is all my source code: http://pastie.org/pr...khdtngvrjtodctq

 

My cookies are being set on lines: 257, 258, 309

 

Edit:

Thanks for writing that up, but my pages are unique to each user based on their location they get different results so I think using cookies is more appropriate. Correct me if I'm wrong of course.

No, your site would benefit from both methods, relying on cookies is fairly dodgey too.

 

 

Your site is also vunerable to DOS attacks even more so, as if i request 50 files every second your server will very quickly be on it's knees and you'll be banned from Amazon.

 

 

You're also exceeding your server timelimit - You shouldn't have to go past that for a simple search function.

 

Do what i said and use the script i sent you. (it will do exactly the same as what you're doing, but will be more secure, less error prone and will save your server from crashing, it will also save requests across users, not just for ONE.)

 

 

 

Scenario:

 

Your idea with cookies:

 

- User visits your website. They're from England.

- Your script finds arists/songs from the database

- For every song found (could be say, 50) you're making a request to amazons website. Thats a lot of requests for 1 page.

 

- User is served a slow loading page. They don't stick around.

- A cookie is then set so the next time they visit the page they're displayed a cached version which is probabaly out of date/their cookie is probabaly deleted.

 

My idea with the server:

 

- User visits your website. They're from England.

- Your script finds artists/songs from the database

- The script checks if theres an existing cache for this specific song and country/location on your server. If there is it serves the file. <1ms loading time. IF THERE IS NO FILE PROCEED ONTO NEXT POINT.

 

- Script gets data for that artist from Amazon website for that location/artist.

- We cache that data locally for that specific location/artist. So that the next time ANYONE who searches for that artist in that specific loation gets CACHED results, thus cutting out 50! external requests.

 

 

Also the other problem you'll hit if you use cookies is the size restriction. A cookie according top rfc can only be 4K in size max.

  • Author

Ok thanks for the advice, I'll give your method a try tomorrow.

 

I'll try and adapt it for my YouTube requests as well as they are the ones which seem to take the longest to load.

 

Will post back results tomorrow.

Ok thanks for the advice, I'll give your method a try tomorrow.

 

I'll try and adapt it for my YouTube requests as well as they are the ones which seem to take the longest to load.

 

Will post back results tomorrow.

 

If you need help i'll make a video tutorial. Or you can just PM me.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.