-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathclass.Cache.php
80 lines (64 loc) · 1.48 KB
/
class.Cache.php
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
<?php
/**
* @fileoverview Cache Manager
* @author Vincent Thibault (alias KeyWorld - Twitter: @robrowser)
* @version 1.5.1
*/
final class Cache
{
static public $path = "";
static public $time = 0;
static private $directory = "";
static private $filename = "";
/**
* Find a file in cache
*/
static public function get(&$content)
{
$path = self::$path . DIRECTORY_SEPARATOR . self::$directory . DIRECTORY_SEPARATOR . self::$filename;
if( file_exists($path) && is_readable($path) ) {
if( filemtime($path) + self::$time > time() ) {
$content = file_get_contents($path);
return true;
}
unlink($path);
}
return false;
}
/**
* Set a directory where to save files
*/
static public function setNamespace($name)
{
self::$directory = $name;
}
/**
* Set a filename
*/
static public function setFilename($name)
{
self::$filename = $name;
}
/**
* Store a file in cache
*/
static public function save()
{
// Cache not disable
if( self::$time > 0 ) {
$path = self::$directory . DIRECTORY_SEPARATOR . self::$filename;
$current_path = self::$path;
$directories = explode('/', $path);
array_pop($directories); // remove filename
// Creating directories
foreach( $directories as $dir ) {
$current_path .= $dir . DIRECTORY_SEPARATOR;
if( !file_exists($current_path) ) {
mkdir( $current_path );
}
}
// Saving content
file_put_contents( self::$path . $path, ob_get_contents() );
}
}
}