PHP File Cache Class Usage Example Analysis _php tutorial

Source: Internet
Author: User

Example Analysis of PHP file cache class Usage


This article mainly introduces the PHP file cache class usage, in the case of the form of a more detailed analysis of the PHP file cache class definition, function and specific use of skills, very practical value, the need for friends can refer to the next

The examples in this article describe the PHP file cache class usage. Share to everyone for your reference. Specific 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

/**

* 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

The usage examples are as follows:

?

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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/987716.html www.bkjia.com true http://www.bkjia.com/PHPjc/987716.html techarticle Example Analysis of PHP file cache class Usage This article mainly introduces the PHP file cache class usage, in the case of the detailed analysis of the PHP file cache class definition, function and specific use technology ...

  • 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.