Simple Cache

This fairly simple and easy to use PHP class can be used for web page caching purposes. Requirements:
  1. You should have writing permissions/ privileges for the caching folder (eg. chmod 666 or 777)!
  2. You should add some attached value (for example a cookie or a session value) for the logged in users!
  3. The caching object should be added right after the initialization of the session cookie!

Cache types table:


Usage:

1

2

3

4

5

6

<?

 

$folder=$_SERVER["DOCUMENT_ROOT"]."/cache_";

$cache=new CACHE($folder);

$cache->startCache();

?>

1

2

3

4

5

6

<?

 

$folder=$_SERVER["DOCUMENT_ROOT"]."/cache_";

$cache=new CACHE($folder);

$cache->startCache();

?>

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

<?

 

// b) Server-side caching

 

$folder=$_SERVER["DOCUMENT_ROOT"]."/cache_";

$cache=new CACHE($folder);

$cache->setCacheType(1); // Server side caching

$cache->startCache();

 

// c) Client-side (browser) caching

 

$folder=$_SERVER["DOCUMENT_ROOT"]."/cache_";

$cache=new CACHE($folder);

$cache->setCacheType(2); // Client side caching

$cache->startCache();

 

// d) Both (server and client browser) sides caching

 

$folder=$_SERVER["DOCUMENT_ROOT"]."/cache_";

$cache=new CACHE($folder);

$cache->setCacheType(3); // Both side caching

$cache->startCache();

 

// e) Simple client-side cache

 

$folder=$_SERVER["DOCUMENT_ROOT"]."/cache_";

$cache=new CACHE($folder,1);

$cache->startCache();

 

// f) Logged in user, both sides cache

 

$folder=$_SERVER["DOCUMENT_ROOT"]."/cache_";

$cache=new CACHE($folder,2, $_COOKIE["user"]); // $_COOKIE["user"] is just an example

$cache->startCache();

 

// g) Changing the default settings (in the cache.php file):

 

private $root=""; // Path to the local folder, YOU NEED WRITING PERMISSIONS (chmod 666 or 777)

private $server__cache_time=300; // How long a file will remain cached on the server (seconds)

private $client__cache_time=150; // How long a file will remain cached within the browser (seconds)

private $cache_type=2; // 0 = No cache, 1 = Server-side cache, 2 = Client-side (browser) cache, 3 = Both sides caching

private $attached_value=""; // Useful for some COOKIE or SESSION values (may be used for logged in users)

private $page;

public $file_name; // The cache file name on server

public $full_file_name; // Full file path and name on the server

public $need_update=false; // If true, then the content will be saved in the path (full_file_name)

public $file_extension="html"; // Useful for faster checking of the cache contents

 

// i) Extra basic usage

$cache=new CACHE($_SERVER["DOCUMENT_ROOT"]."/cache_",2);

$cache->startCache();

// Within these lines of code all caching is performed, no matter the page HTML content is loaded afterwards.

 

// j) All methods

 

$folder=$_SERVER["DOCUMENT_ROOT"]."/cache_";

$cache=new CACHE();

$cache->setRoot($folder); // Path where the cached files will be saved

$cache->setCacheType(3); // Both side caching

$cache->setClientCacheTime(100); // The page will be kept in user's browser for a maximum of 100 s, the page will be same for this amount

$cache->setServerCacheTime(300); // The page will be kept on the server for ALL users for a maximum of 300 s, and will not be generated dynamically within that interval

$cache->setCacheTime(225); // Both caching intervals will be set to 225 seconds, in this case the two previous calls will get overwritten (setClientCacheTime, setServerCacheTime)

$cache->setFileExtension("html"); // This will save cached files as HTML/XHTML and it will make it easier to check the cached contents

$cache->startCache(); // Start caching

?>

The cache class:

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

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

<?

// Created by Raul Molnar (raul@webstorm.ro, for support)

// You have full rights to change, use, sell or donate some amount (to the Paypal account rm@webstorm.ro) .

// Have fun!

 

class CACHE {

 

 

private $root=""; // Path to the local folder, YOU NEED WRITE PERMISSIONS (chmod 666 or 777)

private $server__cache_time=300; // How long a file will be cached on the server, in seconds

private $client__cache_time=150; // How long will be cached a file on browser in seconds

private $cache_type=2; // 0 = No cache, 1 = Server side cache, 2 = Client side (browser) cache, 3 = Both sides cache

private $attached_value=""; // Useful for COOKIE or SESSION values. May be used for logged in users, for example

private $page;

public $file_name; // The cache file name on server

public $full_file_name; // Full file path and name on the server

public $need_update=false; // If true, the content will be saved locally in the path (full_file_name)

public $file_extension="html"; // Useful to check the cache content faster

 

 

public function __construct($root="",$cache_type=-1, $attached_value="") {

 

if ($root!="")$this->setRoot($root);

if ($attached_value!="")$this->attachValue($attached_value);

if ($cache_type!=-1)$this->setCacheType($cache_type);

 

}

 

function startCache() { // Start caching

if ($this->cache_type==1) {

$this->setServerSideCache();

$this->setClientCacheTime(0);

}

else if ($this->cache_type==2) {

$this->setClientSideCache();

$this->setServerCacheTime(0);

}

else if ($this->cache_type==3) {

$this->setClientSideCache();

$this->setServerSideCache();

}

else {

$this->setCacheTime(0);

$this->setClientSideCache();

}

 

}

 

function setRoot($root) { // Sets the root folder, without the ending slash

$this->root=$root;

}

function setCacheType($cache_type) { // Will set cacheType: 0 = No cache, 1 = Server side cache, 2 = Client side (browser) cache, 3 = Both sides caching

$this->cache_type=$cache_type;

}

 

function setFileExtension($file_extension) { ; // This will set the temporary cache file extension (HTML is recommended)

$this->file_extension=$file_extension;

}

 

function setClientCacheTime($time) { // Sets client cache time (in seconds)

$this->client__cache_time=$time;

}

 

function setServerCacheTime($time) { // Sets server cache time (in seconds)

$this->server__cache_time=$time;

}

 

function setCacheTime($time) { // Sets both, client and server cache time (in seconds)

$this->setClientCacheTime($time);

$this->setServerCacheTime($time);

}

 

function attachValue($value) { // Attaches a value related to the cache file, example : USER session or USER cookie, or a test value

$this->attached_value=$value;

}

 

function start_file_cache() { // Prepares the system for file caching (server side)

$this->need_update=true;

ob_start();

}

 

function setClientSideCache() { // Sets the necessary headers for client side caching

 

 

if ($this->client__cache_time>0) {

 

$time_format = gmdate("D, d M Y H:i:s", time() + $this->client__cache_time) . " GMT";

header("Expires: ".$time_format);

header("Pragma: cache");

header("Cache-Control: max-age=".$this->client__cache_time);}

else {header("Cache-Control: no-cache");

header("Pragma: no-cache");}

 

}

 

function setServerSideCache() { // Checking if the file is already there and not expired (in the interval of "server__cache_time" seconds you set

 

if ($this->server__cache_time&lt;=0) return;

 

$this->page=$_SERVER["REQUEST_URI"];

$sanitize=$result = preg_replace("/[^a-zA-Z0-9]+/", "",$_SERVER["PHP_SELF"]);

$this->file_name=$sanitize."-".substr(md5($this->page),10,16); // Generates a file name, you can put any rule here

 

if (strlen($this->attached_value)>0) { $this->file_name.="-".$this->attached_value; }

if (strlen($this->file_extension)>0) { $this->file_name.=".".$this->file_extension; }

 

$this->full_file_name=$this->root."/".$this->file_name;

 

if (file_exists($this->full_file_name)) {

$file_time=filemtime($this->full_file_name);

$now_time=time(false);

 

if (($file_time-$now_time)>=$this->server__cache_time) { $this->start_file_cache(); }

// If the cache file is expired, will get replaced by a more recent one

else {

@readfile($this->full_file_name);

die(""); // If the cache file is not expired, it will only display the content and the script will stop

}

}

else {

$this->start_cache(); // If the cache file is not created, we will simply create it

}

 

}

 

function callback($buffer) {

return $buffer.$this->save_file($buffer);

}

 

function save_file($content) { // This will save the cache file, fopen (or any method, even a database query) can be used for writing

 

if ($file = @fopen($this->full_file_name, "wb"))

{

@fwrite($file, $content);

@fclose($file);

}

return "";

}

 

function start_cache() {

$this->need_update=true;

ob_start(array(&amp;$this, "callback"));

}

}

?>

Newsletter

Sign up for inspiration, news and updates