PHP memcache Basic Operating Example _php tutorial

Source: Internet
Author: User

Memcache Basic operating example in PHP


This paper describes the basic operation and usage of memcache in PHP. Share to everyone for your reference. A small partner in need to come and study it.

Memcache Basic operating example in PHP

?

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

Memcache Demo

$server _ip = ' 127.0.0.1 ';

$server _port = 11211;

$memcache = new Memcache ();

$memcache->connect ($server _ip, $server _port);

$memcache->add ("name1", "user_name1", memcache_compressed,0);

$memcache->add ("name2", "user_name2", memcache_compressed,0);

$array 1 = Array (' name1 ' = ' jiajiam1 ',

' Age1 ' =>12,

' Country ' = ' China ');

$memcache->add ("Other", $array 1,memcache_compressed,20);

$memcache->set ("Name3", "User_name3", memcache_compressed,0);

$memcache->replace ("name1", "User_name_relpace", memcache_compressed,0);

$memcache->replace ("123", "12345");

echo "name1:". $memcache->get ("name1"). "
";

$memcache->delete ("name1");

echo "name1:". $memcache->get ("name1"). "
";

$array _get = Array ("Name1", "name2", "Name3");

$result _get = $memcache->get ($array _get);

foreach ($result _get as $key = + $value) {

echo "$key:---> $value
";

}

foreach ($memcache->getstats () as $key = = $value) {

echo "$key:---> $value
";

};

Echo
";

foreach ($memcache->getextendedstats () as $key = = $value) {

echo "$key:---> $value
";

}

$memcache->close ();

?>

Let's look at a more specific example.

?

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

Include (' inc/common.inc.php ');

if (! isset ($city) | |! Is_array ($city)) {

Exit

}

Print_r ($city); exit;

$mem = new Memcache ();

$mem, connect (' localhost ', ' 11211 ');

$expires =15*60;

Check if cache exits

if ($value = $mem, Get ($city)) = = FALSE) {

echo "Get key from Memcache:". "
";

$return = $mem->get ($city);

echo Json_encode ($return);

}//if

else {

$resultJson =fetch_data ();

echo Count ($resultJson). "
";

if (count ($resultJson) ==1 | | empty ($resultJson)) {

To take a value from MySQL

echo "Get key from MySQL:". "
";

$query = "SELECT * from PM25";

$result =mysql_query ($query);

while ($row = Mysql_fetch_assoc ($result)) {

$rows []= $row;

}

The array of values to be obtained is stored in memcache

for ($i =0; $i<>< p=""> <>

$k [$i]= $rows [$i] [' City '];

$v [$i] [' City ']= $rows [$i] [' City '];

$v [$i] [' PM25 ']= $rows [$i] [' PM25 '];

$mem, set ($k [$i], $v [$i], False, $expires);

}

$return = $mem->get ($city);

echo Json_encode ($return);

}//if

else{

echo "Get key from new_writed MySQL:". "
";

write_db ($resultJson);

$query = "SELECT * from PM25";

$result =mysql_query ($query);

while ($row = Mysql_fetch_assoc ($result)) {

$rows []= $row;

}

Write Memcache

for ($i =0; $i<>< p=""> <>

$k [$i]= $rows [$i] [' City '];

$v [$i] [' City ']= $rows [$i] [' City '];

$v [$i] [' PM25 ']= $rows [$i] [' PM25 '];

$mem, set ($k [$i], $v [$i], False, $expires);

}

$return = $mem->get ($city);

echo Json_encode ($return);

}//else

}//else

foreach ($city as $k = = $v) {

$return [$k]= $mem->get ($v);

}

echo Json_encode ($return);

function Fetch_data () {

$url = "http://www.example.com";

$url = "";

$data = Http_get ($url);

$getJson = Json_decode ($data, true);

return $getJson;

}//func Fetch_data

function write_db ($getJson) {

$sql = "DELETE from PM25";

mysql_query ($sql);

Sort the Json.txt

foreach ($getJson as $key = = $row) {

$area [$key] = $row [' area '];

$PM 2_5[$key]= $row [' pm2_5 '];

}

Array_multisort ($area, SORT_ASC, $pm 2_5,sort_asc, $getJson);

for ($i =0; $i<>< p=""> <>

if ($getJson [$i] [' Pm2_5 ']==0)

$count = 0;

Else

$count = 1;

$sum = $getJson [$i] [' pm2_5 '];

for ($j = $i +1; $j<>< p=""> <>

if (strcmp ($getJson [$j] [' area '], $getJson [$i] ["area]] ==0) {

if ($getJson [$j] [' pm2_5 ']==0) {

Continue

}

else{

$count + +;

$sum + = $getJson [$j] [' pm2_5 '];

$PM 2_5= $sum/$count;

}

}

else{

INSERT INTO MySQL

$result [' City ']= $getJson [$i] [' area '];

$result [' Pm25 ']=intval ($pm 2_5);

$query = "INSERT into PM25 (CITY,PM25) VALUES ('". $result [' City ']. "',". $result [' PM25 ']. ")";

mysql_query ($query);

Break

}

}

}

return $getJson;

}//func write_db

$mem-Close ();

?>

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1000070.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000070.html techarticle Memcache Basic Operation example in PHP This article describes the basic operation and usage of memcache in PHP. Share to everyone for your reference. A small partner in need to come and study it. PHP me ...

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