How to cache PHP variables as a file _php tutorial

Source: Internet
Author: User

How to cache PHP variables as files


The example in this article describes how to cache php variables in file form. Share to everyone for your reference. The implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

/*

$cache _set = Array (

Cache path, last add "/"

' CacheRoot ' = './cache/',

Cache time

' CacheTime ' =>20,

Cache type

' CacheType ' =>1,

Extended Name

' Cacheexe ' = '. php '

);

$cache = new Cache ($cache _set);

$a =array (' 1 ', ' 2 ');

$a = "AAA";

$b = ";

if ($cache->cache_is ("D")) {

$c = $cache->cache_read ("D");

echo "C";

Print_r ($c);

}else {

$b = $cache->cache_data (' d ', $a);

}

Print_r ($b);

$cache->clear ("a");

echo $cache->cache_read ("./cache/d.php");

Echo $d;

*/

/**

* Data Cache Class v1.0

* @author Shooke

* 2009-11-13 16:02:26

* Used to cache data, such as variables, but cannot cache pages

*/

Class cache{

Configuration

Public $config = Array (

Cache path

' CacheRoot ' = './cache/',

Cache time

' CacheTime ' =>1,

Cache type 1 Serialization data 2 variable

' CacheType ' =>2,

Extended Name

' Cacheexe ' = '. php '

Converting intermediate variables

);

Public $return _name=array ();

function __construct ($cache _set = Array ())

{

if (!empty ($cache _set)) $this->config=array_merge ($this->config, $cache _set);

$this->config[' ClassName '] = __class__;

}

Public Function Clear ($filename = ") {

if (File_exists ($this->cache_file ($filename))) {

@unlink ($this->cache_file ($filename));

}elseif (Empty ($filename)) {

$this->clear_dir ($this->config[' cacheroot ');

}else{

$this->clear_dir ($this->config[' CacheRoot '). $filename);

echo $this->config[' CacheRoot '). $filename;

}

}

Loop Delete Path

Private Function Clear_dir ($dir, $to = False)

{

if ($list = Glob ($dir. '/* '))

{

foreach ($list as $file)

{

Is_dir ($file)? $this->clear_dir ($file): unlink ($file);

}

}

if ($to = = = False) rmdir ($DIR);

}

Write Cache

Private Function Cache_write ($filename, $writetext, $openmod = ' W ') {

if (!file_exists ($filename)) {

@ $this->makedir (dirname ($filename));

}

if (@ $fp = fopen ($filename, $openmod)) {

Flock ($FP, 2);

Fwrite ($fp, $writetext);

Fclose ($FP);

return true;

} else {

echo "File: $filename write error.";

return false;

}

}

Cache validity Valid Returns True

Public Function cache_is ($fileName) {

$fileName = $this->cache_file ($fileName);

if (file_exists ($fileName)) {

Never expire if the cache time is negative

if ($this->config[' cacheTime ') < 0) {

return true;

}

Expires if the cache time is 0

if ($this->config[' cacheTime ') = = 0) {

return false;

}

Get the cache file settling time

$ctime = Intval (Filemtime ($fileName));

Whether the comparison is greater than the cache time, is expired or is not valid

if (Time ()-$ctime > $this->config[' cacheTime ') {

return false;

}else {

return true;

}

File does not exist as expired expiration

}else {

return false;

}

}

Public Function Cache_data ($name, $data) {

$varname = $name;

$name = $this->cache_file ($name);

config[' CacheTime ']==0 that is, do not enable caching is to return data directly

if ($this->config[' cacheTime ') <> 0) {

if ($this->config[' CacheType ']==1) {

$write _data = " ". Serialize ($data);

return $data;

}else {

$write _data = "

$write _data. = Var_export ($data, true);

$write _data. = ";\\r\\n?>";

}

$this->cache_write ($name, $write _data);

}

return $data;

}

Cache file Name

Private Function Cache_file ($filename) {

return $this->config[' CacheRoot '). $filename. $this->config[' Cacheexe ';

}

Read file

Public Function Cache_read ($file) {

$file = $this->cache_file ($file);

if (!file_exists ($file)) {

Return ';

}

if ($this->config[' CacheType ']==1) {

if (function_exists (' file_get_contents ')) {

$cache _content= file_get_contents ($file);

}else{

$fopen = fopen ($file, ' R ');

$cache _content = ";

do {

$data = Fread ($fopen, FileSize ($file));

if (strlen ($data) ===0) break;

$cache _content. = $data;

}while (1);

Fclose ($fopen);

}

$cache _content = substr ($cache _content,13);/* Remove */

$cache _content = unserialize ($cache _content);

return $cache _content;

}else{

Include_once ($file);

return $var;

}

}

Looping through the creation of catalogs

Private Function MakeDir ($dir, $mode = 0777) {

if (! $dir) return 0;

$dir = Str_replace ("\\\\", "/", $dir);

$mdir = "";

foreach (Explode ("/", $dir) as $val) {

$mdir. = $val. " /";

if ($val = = ":" | | $val = = "." | | Trim ($val) = = "") continue;

if (! file_exists ($mdir)) {

if (! @mkdir ($mdir, $mode)) {

return false;

}

}

}

return true;

}

}

?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1022063.html www.bkjia.com true http://www.bkjia.com/PHPjc/1022063.html techarticle how to cache PHP variables as a file The example in this article describes how to cache PHP variables in a file format. Share to everyone for your reference. The specific implementation method is as follows:? 1 2 3 4 5 6 7 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.