note 48000 added to function.serialize

From: Date: Wed, 08 Dec 2004 18:47:14 +0000
Subject: note 48000 added to function.serialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-81540@lists.php.net to get a copy of this message
If this helps anybody, I was looking for a few quick calls to cache stuff to files. I settled on the below functions. By the way, if I am doing something stupid here, I'd appreciate some comments on that also. Anyway, on with the code: First, the usage: //Use: $var = tuncache('MyCacheName'); if (is_null($var)) { $var = 'a really expensive process'; $check = tcache('MyCacheName', $var, '+25 minutes'); if (is_null($check)) { die('I need to reset my permissions or get a bigger hard drive'); } } //use $var now Now, the functions: //uses strtotime to decide how long to cache $cacheVar based on $lifespan //so anything but a timestamp works. +1 day works, 09/14/2010 works, etc function tcache($cacheName, $cacheVar, $lifespan=null) { if (!is_null($lifespan)) { //a lifespan was provided if (strpos($lifespan, '+') === 0 || strpos($lifespan, '-') === 0) { //Have to do this to counter for bug in php5 to //properly evaluate now to do the date math $endTime = strtotime(date('m/d/Y H:i:s') . ' ' .$lifespan); } else { //we can safely use anything else I think $endTime = strtotime($lifespan); } } else { //cache forever (forever being ten years) until manually deleted $endTime = strtotime(date('m/d/Y H:i:s') . ' +10 years'); } $cVal = serialize($cacheVar); $file = tFindcache($cacheName); $result = true; $f = fopen($file, 'w'); if ($f === false) { $result = null; //barf if we can't open the file } else { //write the cache fwrite($f, $endTime . "\n"); fwrite($f, $cVal); fclose($f); } return $result; } function tuncache($cacheName) { $file = tFindCache($cacheName); if (is_readable($file)) { //open file $f = fopen($file, 'r'); $time = time(); $expTime = trim(fgets($f)); //check time if ($time > $expTime) { //It expired $result = null; } else { //Read it and unserialize it $data = fread($f, filesize($file)); $result = unserialize($data); } } else { //barf $result = null; } return $result; } //just to set a few options for setting where to save stuff function tFindCache($cacheName) { if (isset($_ENV['PHP_CACHE_PATH'])) { $cachePath = $_ENV['PHP_CACHE_PATH']; } else if (isset($_SERVER['PHP_CACHE_PATH'])) { $cachePath = $_SERVER['PHP_CACHE_PATH']; } else { $cachePath = './'; //Potential danger here } $cachePath .= 'PHP_CACHE_' . $cacheName . '.cache'; return $cachePath; } ---- Manual Page -- http://www.php.net/manual/en/function.serialize.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+48000 Delete -- http://master.php.net/manage/user-notes.php?action=delete+48000&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48000&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48000&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48000&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48000&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48000&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+48000&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#81540) next »