1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?

/*
 * MySQL Cache Script
 * Easily Cache Information to a MySQL Table
 *
 *  Author :   JMan
 *  HomePage : www.bedpan.ca
 *  Email :    jman@bedpan.ca
 */

$server "localhost"
$db_user "root"
$db_pass ""
$database "db_name"

mysql_connect($server$db_user$db_pass); 
mysql_select_db($database); 

function 
slash($item1$clean=false){
    
//If you have problems withs slashes, play with slash($data, 2)
    
if ($clean == 1){
        return 
stripslashes($item1);
    }elseif (
$clean == 2){
        return 
str_replace("\\""\\\\"$item1);
    }else{
        return 
addslashes(str_replace("\\""\\\\"$item1));
    }
}

function 
cache($id$seconds=""$data=""){
    global 
$server$db_user$db_pass$database;
    
mysql_connect($server$db_user$db_pass);
    
mysql_select_db($database);
    
$utime time();
    
$exptime $utime $seconds;
    
mysql_query("DELETE FROM cache WHERE id = '$id' && timestamp <= UNIX_TIMESTAMP()") or die(mysql_error());
    
    
$result mysql_query("SELECT * FROM cache WHERE id = '$id'") or die(mysql_error());
    if (
mysql_num_rows($result)){
        
$db mysql_fetch_row($result);
        
$db[1] = slash($db[1], 1);
        
$stored unserialize($db[1]);
        if (
is_array($stored)){
            foreach (
$stored as $key => $store){
                
$stored[$key] = slash($stored[$key], 2);
            }
        }else{
            
$stored slash($stored2);
        }
        return 
$stored;
    }else{
        
$data serialize($data);
        
$data slash($data);
        
$id addslashes($id);
        if (
$datamysql_query("INSERT INTO cache (id, stored, timestamp) VALUES ('$id', '$data', '$exptime')") or die(mysql_error());
        return 
FALSE;
    }
}

mysql_close(); 

# Format: 
# cache($id, $seconds, $data) 
#    $id is a unique indentifying string 
#    $seconds is the number of seconds you want to cache the data 
#    $data is what you want cached 

$cached cache("animals"); 
if (
$cached){ //CHECK IF IT'S ALREADY CACHED 
    
$test $cached
    echo 
"\n<br>CACHED DATA\n<br>"
}else{ 
//AQUIRE THE DATA, THEN CACHE IT 
    
$test = array('turtle'"monkey"date("M j, Y"time()), "fish"); 
    
$cached cache("animals"60$test); //CACHES FOR 60 SECONDS
    
echo "\n<br>FRESH DATA\n<br>"
//DO WHATEVER TO THE DATA 
//print_r(slash($test, 2));
print_r($test);
?>

Return to Script