PHP File Cache Usage instance analysis

Source: Internet
Author: User

PHP File Cache Usage instance analysis

This article mainly introduces the usage of PHP File Cache class, and analyzes in detail the definition, functions, and specific usage skills of PHP File Cache class in the form of instances, which is of great practical value, for more information, see

This example describes the usage of the PHP File Cache class. Share it with you for your reference. The details are 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

<? Php

/**

* Simple File Cache class

*

*/

Class XZCache {

// Default cache time one hour

Var $ cache_time = 3600;

// Default cache dir

Var $ cache_dir = './cache ';

Public function _ construct ($ cache_dir = null, $ cache_time = null ){

$ This-> cache_dir = isset ($ cache_dir )? $ Cache_dir: $ this-> cache_dir;

$ This-> cache_time = isset ($ cache_time )? $ Cache_time: $ this-> cache_time;

}

Public function saveCache ($ key, $ value ){

If (is_dir ($ this-> cache_dir )){

$ Cache_file = $ this-> cache_dir. '/xzcache _'. md5 ($ key );

$ Timedif = @ (time ()-filemtime ($ cache_file ));

If ($ timedif >=$ this-> cache_time ){

// Cached file is too old, create new

$ Serialized = serialize ($ value );

If ($ f = @ fopen ($ cache_file, 'w ')){

Fwrite ($ f, $ serialized, strlen ($ serialized ));

Fclose ($ f );

}

}

$ Result = 1;

} Else {

Echo "Error: dir is not exist .";

$ Result = 0;

}

Return $ result;

}

/**

* @ Return array

* 0 no cache

* 1 cached

* 2 overdue

*/

Public function getCache ($ key ){

$ Cache_file = $ this-> cache_dir. '/xzcache _'. md5 ($ key );

If (is_dir ($ this-> cache_dir) & is_file ($ cache_file )){

$ Timedif = @ (time ()-filemtime ($ cache_file ));

If ($ timedif >=$ this-> cache_time ){

$ Result ['cached'] = 2;

} Else {

// Cached file is fresh enough, return cached array

$ Result ['value'] = unserialize (file_get_contents ($ cache_file ));

$ Result ['cached'] = 1;

}

} Else {

Echo "Error: no cache ";

$ Result ['cached'] = 0;

}

Return $ result;

}

} // End of class

Example:

?

1

2

3

4

5

6

$ Cache = new XZCache ();

$ Key = 'global ';

$ Value = $ GLOBALS;

$ Cache-> saveCache ($ key, $ value );

$ Result = $ cache-> getCache ($ key );

Var_dump ($ result );

I hope this article will help you with php programming.

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.