PHP File-System Cache

[ad]

I’m just sharing a couple of functions I created a while back to cache some resource-intensive processed dada for quick and easy access. This is pretty ideal for large amounts of data and it’s very simple to set up. The beauty of this is that you can store just about any data type – it doesn’t have to be a string.

Just setup the cache directory to have proper read/write permissions (preferably in a non-accessible from web directory). Then use the two functions.

Example usage:

// The 2nd argument, $hours is how long to retain data before getting new
$contents = get_cache('TEST_KEY', 24);
if ($contents === false) {
	// This is where you'd get data from an API, DB or whatever
	$contents = 'Just some example contents';
	set_cache('TEST_KEY', $contents);
}
echo $contents;
}

Code:

define('CACHE_DIR', 'cache/'); // Include trailing slash

function get_cache($key, $hours) {
	$file = CACHE_DIR . md5($key) . '.cache';
	if (!file_exists($file) || filemtime($file) < time() - $hours * 3600)
		return false;
	return unserialize(file_get_contents($file));
}

function set_cache($key, $value) {
	$file = CACHE_DIR . md5($key) . '.cache';
	file_put_contents($file, serialize($value));
}

HTML Optimizer & Compressor

Quite often I create an HTML file, possibly generated dynamically, that sees a ton of exposure. To save loading time and bandwidth, I optimize/compress the static files (HTML, CSS, and Javascript). I’ve found very good tools for packing Javascript and CSS, Dean Edwards’ packer and CSS Drive’s compressor respectively, but haven’t found a great tool for optimizing HTML. The following tool is something I whipped up in VB.net which simply removes whitespace and comments from HTML and tells you how much disk space you are saving. Careful, it could possibly mess up your javascript or anything else whitespace sensitive (pre tags, etc), but should do the job for most tasks. Admittedly this might be more convenient as a web application, and it wouldn’t take much, but I created it personally for my uses so I can use it offline as well.

Download: HTMLOptimizer.zip (7.92KB)

[ad]

ASP File_Get_Contents and File_Put_Contents

As I've become more of a PHP developer than an ASP developer, one thing I've noticed is that it is 10x easier to read/write text files with PHP. Simply use file_get_contents() and be done. ASP isn't so nice. But it can be done, so I'm creating those for simplicity here:

<%
Function File_Get_Contents(strFile)
	' Remote File
	If Left(strFile, 7) = "http://" Or Left(strFile, 8) = "https://" Then
		Set objXML = Server.CreateObject("Microsoft.XMLHTTP")
		' Use this line if above errors
		'Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
		objXML.Open "GET", strFile, False
		objXML.Send()
		File_Get_Contents = objXML.ResponseText()
		Set objXML = Nothing
	' Local File
	Else
		Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
		Set objFile = objFSO.OpenTextFile(strFile, 1)
		File_Get_Contents = objFile.ReadAll()
		Set objFile = Nothing
		Set objFSO = Nothing
	End If
End Function

Function File_Put_Contents(strFile, strContents, blnAppend)
	If blnAppend Then
		intMode = 8
	Else
		intMode = 2
	End If
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile(strFile, intMode, True)
	objFile.Write(strContents)
	Set objFile = Nothing
	Set objFSO = Nothing
End Function
%>

Of course this file_get_contents doesn't support remote URLs, but it can easily be added.

EDIT: I just added remote URL support.

[ad]

How to access your cell phone voicemail from another phone


This is certainly no new news, but I’m fairly sure many aren’t aware of this simple feature.

Say you left your phone at home and you were expecting a call, but your friend remembered his phone — tough luck, right? ┬áNope (obviously, or I wouldn’t have made this post).

Here’s what you can do…just pick up any phone and do the following:

  1. Dial your 10-digit wireless number.
  2. Press the * key when you hear your personal greeting.
  3. Enter your personal passcode.
  4. Follow the voice prompts.

Free Web FLV Flash Player

I’ve used this product multiple times myself, so I thought it would be worthwhile posting it for anyone else looking for a web flash player. The great thing about it is it is very versatile, customizable, and FREE. The JW FLV Media player is suitable to handle FLV, MP4, MP3, AAC, JPG, PNG and GIF formats.

The player, which can play both audio and video, was written by Jeroen Wijering quite a while ago and became very successful. He has since ran with the success and co-founded Longtail Video.

A useful thing to do with this player is to download a copy onto your computer. You can then load FLV’s you’ve downloaded from Youtube, etc. into it and play them offline. This eliminates the need to install a bloated FLV player onto your PC (which likely isn’t a problem for most, but meh.).

To see the video player in action, check out these funny videos.