Caching php variables as files _ PHP Tutorial

Source: Internet
Author: User
Cache php variables as files. How to cache php variables in file form this article describes how to cache php variables in file form. Share it with you for your reference. The specific implementation method is as follows :? 1234567 how to cache php variables as files

This example describes how to cache php variables as files. Share it with you for your reference. The specific 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

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

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, and add "/"

'Cacherot' => './cache /',

// Cache time

'Cachetime' => 20,

// Cache type

'Cachetype '=> 1,

// Extension

'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', $ );

}

Print_r ($ B );

// $ Cache-> clear ("");

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

// Echo $ d;

*/

/**

* Data cache class v1.0

* @ Author Shoke

* 16:02:26

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

*/

Class Cache {

// Configure

Public $ config = array (

// Cache path

'Cacherot' => './cache /',

// Cache time

'Cachetime' => 1,

// Cache type 1 stringized Data 2 variable

'Cachetype '=> 2,

// Extension

'Cacheexe '=>'. php'

// Convert the intermediate variable

);

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 ['cacherot']);

} Else {

$ This-> clear_dir ($ this-> config ['cacheroot']. $ filename );

Echo $ this-> config ['cacherot']. $ filename;

}

}

// Delete the path cyclically

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;

}

}

// Return true if the cache validity period is valid

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;

}

// If the cache time is 0, it will always expire

If ($ this-> config ['cachetime'] = 0 ){

Return false;

}

// Obtain the cache file creation time

$ Ctime = intval (filemtime ($ fileName ));

// Compare whether the cache time is greater than the cache time. if yes, it expires or it does not expire.

If (time ()-$ ctime> $ this-> config ['cachetime']) {

Return false;

} Else {

Return true;

}

// If the file does not exist, it is deemed as expired.

} Else {

Return false;

}

}

Public function cache_data ($ name, $ data ){

$ Varname = $ name;

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

// Config ['cachetime'] = 0. If cache is not enabled, data is directly returned.

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 ['cacherot']. $ filename. $ this-> config ['cacheexe '];

}

// Read the 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;

}

}

// Create a directory cyclically

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 will help you with php programming.

Examples in this article describes how to cache php variables in the form of files. Share it with you 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.