How php operates redis Cache

Source: Internet
Author: User
Tags php redis delete cache

How php operates redis Cache

In addition to memcache, a commonly used php operation class library, we may also be very familiar with memory cache, Which is redis. We will share this php technical article with you, this is about how to use php to operate the redis memory cache tool library.

Php redis cache operations

?

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

129

130

<? Php

/**

* Redis cache operations

* @ Author hxm

* @ Version 1.0

* @ Since 2015.05.04

*/

Class RCache extends Object implements CacheFace

{

Private $ redis = null; // redis object

 

Private $ sId = 1; // servier service ID

 

Private $ con = null; // link resource

 

/**

* Initialize Redis

*

* @ Return Object

*/

Public function _ construct ()

{

If (! Class_exists ('redis '))

{

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

}

$ This-> redis = new Redis ();

}

 

/**

* 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-> redis-> 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-> redis-> getset ($ key, $ value );

} Else {

Return $ this-> redis-> set ($ key, $ value );

}

}

 

/**

* 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-> redis-> get ($ key );

}

 

/**

* Clean (delete) all elements that have been stored

*

* @ Access private

* @ Return array

*/

Public function flush ()

{

$ This-> connect ();

Return $ this-> redis-> flushall ();

}

/**

* 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-> redis-> del ($ key );

}

 

/**

* Destructor

* Disable memcache.

*/

Public function _ destruct ()

{

If ($ this-> redis)

{

$ This-> redis-> 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.