Home > Backend Development > PHP Tutorial > PHP file cache class usage example analysis_PHP tutorial

PHP file cache class usage example analysis_PHP tutorial

WBOY
Release: 2016-07-13 09:56:29
Original
757 people have browsed it

Analysis of examples of usage of php file cache class

This article mainly introduces the usage of php file cache class, and analyzes the definition and function of php file cache class in detail in the form of examples. and specific usage techniques, which are of great practical value. Friends who need them can refer to them

The example in this article describes the usage of php file cache class. Share it with everyone 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

/**

* 简单的文件缓存类

*

*/

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

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
<🎜>/**<🎜> <🎜>* Simple file caching 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

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);

1

2 3

45 6
$cache = new XZCache(); $key = 'global'; $value = $GLOBALS; $cache->saveCache($key, $value); $result = $cache->getCache($key);
var_dump($result);
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/987716.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/987716.htmlTechArticlePHP file cache class usage example analysis This article mainly introduces the php file cache class usage, which is more detailed in the form of examples. Analyzed the definition, functions and specific usage techniques of the PHP file cache class...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template