Basic memcache operations in php _ PHP Tutorial

Source: Internet
Author: User
Basic memcache operation instance in php. Basic memcache operation examples in php this article describes the basic operations and usage of Memcache in PHP. Share it with you for your reference. If you need it, study it. Basic memcache operation instance in me php in php

This article describes the basic operations and usage of Memcache in PHP. Share it with you for your reference. If you need it, study it.

Basic memcache operation instance in php

?

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

Memcache demo

$ Server_ip = '2017. 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 );

$ Array1 = array ('name1' => 'jiajiam1 ',

'Ag1' => 12,

'Country' => 'China ');

$ Memcache-> add ("other", $ array1, 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 take a look at more specific instances.

?

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

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

If (! Isset ($ city) |! Is_array ($ city )){

Exit;

}

// Print_r ($ city); exit;

$ Mem = new Memcache ();

$ Mem-> connect ('localhost', '123 ');

$ 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 )){

// Value from mysql

Echo "get key from mysql :"."
";

$ Query = "select * from pm25 ";

$ Result = mysql_query ($ query );

While ($ row = mysql_fetch_assoc ($ result )){

$ Rows [] = $ row;

}

// Save the obtained value array to memcache

For ($ I = 0; $ I

$ 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

$ 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 ['region'];

$ Pm2_5 [$ key] = $ row ['pm2 _ 5'];

}

Array_multisort ($ area, SORT_ASC, $ pm2_5, SORT_ASC, $ getJson );

For ($ I = 0; $ I

If ($ getJson [$ I] ['pm2 _ 5'] = 0)

$ Count = 0;

Else

$ Count = 1;

$ Sum = $ getJson [$ I] ['pm2 _ 5'];

For ($ j = $ I + 1; $ j

If (strcmp ($ getJson [$ j] ['area '], $ getJson [$ I] ['area']) = 0 ){

If ($ getJson [$ j] ['pm2 _ 5'] = 0 ){

Continue;

}

Else {

$ Count ++;

$ Sum + = $ getJson [$ j] ['pm2 _ 5'];

$ Pm2_5 = $ sum/$ count;

}

}

Else {

// Insert into mysql

$ Result ['city'] = $ getJson [$ I] ['region'];

$ Result ['pm25'] = intval ($ pm2_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 is all the content of this article. I hope you will like it.

The following example describes the basic operations and usage of Memcache in PHP. Share it with you for your reference. If you need it, 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.