How to operate memcache in php

Source: Internet
Author: User
Tags delete cache

How to operate memcache in php

In general, if the concurrency is not large, it does not affect the use of the cache technology. However, if the concurrency is high, it is very important to use the cache technology, it can effectively reduce the pressure on databases and servers. Of course, there are many technologies to solve high concurrency. Here, we only use the cache to describe the convenience and convenience of using memcache,

To use memcache, you must configure the memcahche environment on the server! After confirming that memcahce can be connected normally, it can be used in the program!

?

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

117

118

119

120

121

122

123

124

125

126

127

128

<? Php

/**

* Memcache cache operations

* @ Author hxm

* @ Version 1.0

* @ Since 2015.05.04

*/

Class MCache extends Object implements CacheFace

{

Private $ mem = null; // Mem object

 

Private $ sId = 1; // servier service ID

 

/**

* Initialize Memcache

*

* @ Return Object

*/

Public function _ construct ()

{

If (! Class_exists ('memcache '))

{

Throw new QException ('php extension does not exist: memcache ');

}

$ This-> mem = new Memcache ();

}

 

/**

* Link to the memcahce Service

*

* @ Access private

* @ Param string $ key keyword

* @ Param string $ value cached content

* @ Return array

*/

Private function connect ($ sid)

{

$ File = $ this-> CacheFile ();

Require $ file;

If (! Isset ($ cache ))

{

Throw new QException ('cache configuration file does not exist'. $ file );

}

$ Server = $ cache [$ this-> cacheId];

$ Sid = isset ($ sid) = 0? $ This-> sId: $ sid; // memcache Service Selection

If (! $ Server [$ sid])

{

Throw new QException ('the current cache server configuration file does not exist ');

}

$ Host = $ server [$ sid] ['host'];

$ Port = $ server [$ sid] ['Port'];

Try {

$ This-> mem-> connect ($ host, $ port );

} Catch (Exception $ e ){

Exit ('memecache connection failed, error message: '. $ e-> getMessage ());

}

}

 

/**

* Write Cache

*

* @ Access private

* @ Param string $ key keyword

* @ Param string $ value cached content

* @ Return array

*/

Public function set ($ key, $ value, $ sid, $ expire = 0)

{

$ Data = $ this-> get ($ key, $ sid); // if the key value already exists

If ($ data)

{

Return $ this-> mem-> set ($ key, $ value, MEMCACHE_COMPRESSED, $ expire );

} Else {

Return $ this-> mem-> add ($ key, $ value, MEMCACHE_COMPRESSED, $ expire );

}

}

 

/**

* Read Cache

*

* @ Access private

* @ Param string $ key keyword

* @ Param int $ sid select the number of memcache servers

* @ Return array

*/

Public function get ($ key, $ sid)

{

$ This-> connect ($ sid );

Return $ this-> mem-> get ($ key );

}

 

/**

* Clean (delete) all elements that have been stored

*

* @ Access private

* @ Return array

*/

Public function flush ()

{

$ This-> connect ();

Return $ this-> mem-> flush ();

}

/**

* Delete Cache

*

* @ Access private

* @ Param string $ key keyword

* @ Param int $ sid select the number of memcache servers

* @ Return array

*/

Public function remove ($ key, $ sid)

{

$ This-> connect ();

Return $ this-> mem-> delete ($ key );

}

 

/**

* Destructor

* Disable memcache.

*/

Public function _ destruct ()

{

/* If (! $ This-> mem)

{

$ This-> mem-> close ();

}*/

}

}

The above is all the content of this article. I hope you will like it.

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.