php教程 php手册 ssdb的Yii cache扩展

ssdb的Yii cache扩展

Jun 06, 2016 pm 08:12 PM
cache google Level yii 확장하다

google的leveldb越来越被很多人接受。国内的ideawu基于leveldb还写了一个ssdb的前置扩展用来实现了很多功能,比如标准的getset和hget,hset还有zset,zget,也实现了队列。当然pub/sub就没有办法实现了。毕竟它和redis还是有点区别。 基于标准的ssdb的类,写了

google的leveldb越来越被很多人接受。国内的ideawu基于leveldb还写了一个ssdb的前置扩展用来实现了很多功能,比如标准的getset和hget,hset还有zset,zget,也实现了队列。当然pub/sub就没有办法实现了。毕竟它和redis还是有点区别。

基于标准的ssdb的类,写了个小扩展,扩展了Yii的Cache类:

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

class?CSsdbCache?extends?CCache??

{??

????/**?

?????*?@var?string?hostname?to?use?for?connecting?to?the?redis?server.?Defaults?to?'127.0.0.1'.?

?????*/??

????public?$hostname?=?'127.0.0.1';??

????/**?

?????*?@var?int?the?port?to?use?for?connecting?to?the?ssdb?server.?Default?port?is?8888.?

?????*/??

????public?$port?=?8888;??

????/**?

?????*?@var?float?

?????*/??

????public?$timeout?=?2000;??

????public?$serializer?=?false;??

????public?$_cache;??

????protected?$_cachekeys?=?'ssdb_cachekey';??

??????

????public?function?init()?{??

????????parent::init();??

????}??

????/**?

?????*?@return?SSDB?

?????*/??

????public?function?getSsdbCache()?{??

????????if?($this->_cache?!==?null)??

????????????return?$this->_cache;??

????????else?{??

????????????return?$this->_cache?=?new?SimpleSSDB($this->hostname,?$this->port,?$this->timeout);??

????????}??

????}??

????public?function?getkeys()?{??

????????return?$this->getSsdbCache()->hkeys($this->_cachekeys,?"",?"",?$this->getSsdbCache()->hsize($this->_cachekeys));??

????}??

????/**?

?????*?Retrieves?a?value?from?cache?with?a?specified?key.?

?????*?This?is?the?implementation?of?the?method?declared?in?the?parent?class.?

?????*?@param?string?$key?a?unique?key?identifying?the?cached?value?

?????*?@return?string|boolean?the?value?stored?in?cache,?false?if?the?value?is?not?in?the?cache?or?expired.?

?????*/??

????protected?function?getValue($key)?{??

????????return?unserialize($this->getSsdbCache()->get($key));??

????}??

??

????/**?

?????*?Stores?a?value?identified?by?a?key?in?cache.?

?????*?This?is?the?implementation?of?the?method?declared?in?the?parent?class.?

?????*?@param?string??$key????the?key?identifying?the?value?to?be?cached?

?????*?@param?string??$value??the?value?to?be?cached?

?????*?@param?integer?$expire?the?number?of?seconds?in?which?the?cached?value?will?expire.?0?means?never?expire.?

?????*?@return?boolean?true?if?the?value?is?successfully?stored?into?cache,?false?otherwise?

?????*/??

????protected?function?setValue($key,?$value,?$expire)?{??

????????$this->getSsdbCache()->hset($this->_cachekeys,?$key,?1);??

????????if?($expire?>?0)?{??

????????????//$expire?+=?time();??

????????????return?$this->getSsdbCache()->setx($key,?serialize($value),?(int)?$expire);??

????????}??

????????else?{??

????????????return?$this->getSsdbCache()->set($key,?serialize($value));??

????????}??

????}??

????/**?

?????*?Stores?a?value?identified?by?a?key?into?cache?if?the?cache?does?not?contain?this?key.?

?????*?This?is?the?implementation?of?the?method?declared?in?the?parent?class.?

?????*?@param?string??$key????the?key?identifying?the?value?to?be?cached?

?????*?@param?string??$value??the?value?to?be?cached?

?????*?@param?integer?$expire?the?number?of?seconds?in?which?the?cached?value?will?expire.?0?means?never?expire.?

?????*?@return?boolean?true?if?the?value?is?successfully?stored?into?cache,?false?otherwise?

?????*/??

????protected?function?addValue($key,?$value,?$expire)?{??

????????return?$this->setValue($key,?$value,?$expire);??

????}??

????/**?

?????*?Deletes?a?value?with?the?specified?key?from?cache?

?????*?This?is?the?implementation?of?the?method?declared?in?the?parent?class.?

?????*?@param?string?$key?the?key?of?the?value?to?be?deleted?

?????*?@return?boolean?if?no?error?happens?during?deletion?

?????*/??

????protected?function?deleteValue($key)?{??

????????$this->getSsdbCache()->hdel($this->_cachekeys,?$key);??

????????return?$this->getSsdbCache()->del($key);??

????}??

????/**?

?????*?@return?boolean?whether?the?flush?operation?was?successful.?

?????*/??

????protected?function?flushValues()?{??

????????$this->getSsdbCache()->multi_del($this->getkeys());??

????????return?$this->getSsdbCache()->hclear($this->_cachekeys);??

????}??

}??

로그인 후 복사

其实代码很简单,不过由于ssdb默认没有serialize功能,所以在存储之前,得先主动的serialize,然后get的时候unserialize。不然就没有办法存储数组了。

由于ssdb没有flush功能。所以利用hget/hset将所有的key存储下来。flush的时候把hget获取的key读出来删除。然后再清掉这个hget的key

最后还有expire。ssdb里的setx第三个参数。。。居然不是expire,而是ttl。开始的时候,一直都当成expire。结果浪费了很长时间

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Google Pixel 9 및 Pixel 9 Pro는 출시 시 Creative Assistant AI를 얻을 것이라는 소문이 있습니다. Google Pixel 9 및 Pixel 9 Pro는 출시 시 Creative Assistant AI를 얻을 것이라는 소문이 있습니다. Jun 22, 2024 am 10:50 AM

Google Pixel 9 및 Pixel 9 Pro는 출시 시 Creative Assistant AI를 얻을 것이라는 소문이 있습니다.

Pixel 9 Pro XL의 Google Tensor G4는 원신에서 Tensor G2보다 뒤떨어집니다. Pixel 9 Pro XL의 Google Tensor G4는 원신에서 Tensor G2보다 뒤떨어집니다. Aug 24, 2024 am 06:43 AM

Pixel 9 Pro XL의 Google Tensor G4는 원신에서 Tensor G2보다 뒤떨어집니다.

Google 앱 베타 APK 분해로 Gemini AI 비서에 추가되는 새로운 확장 기능 공개 Google 앱 베타 APK 분해로 Gemini AI 비서에 추가되는 새로운 확장 기능 공개 Jul 30, 2024 pm 01:06 PM

Google 앱 베타 APK 분해로 Gemini AI 비서에 추가되는 새로운 확장 기능 공개

Google Pixel 9 스마트폰은 7년 업데이트 약속에도 불구하고 Android 15와 함께 출시되지 않습니다. Google Pixel 9 스마트폰은 7년 업데이트 약속에도 불구하고 Android 15와 함께 출시되지 않습니다. Aug 01, 2024 pm 02:56 PM

Google Pixel 9 스마트폰은 7년 업데이트 약속에도 불구하고 Android 15와 함께 출시되지 않습니다.

Google AI, 개발자를 위한 Gemini 1.5 Pro 및 Gemma 2 발표 Google AI, 개발자를 위한 Gemini 1.5 Pro 및 Gemma 2 발표 Jul 01, 2024 am 07:22 AM

Google AI, 개발자를 위한 Gemini 1.5 Pro 및 Gemma 2 발표

새로운 Google Pixel 데스크탑 모드는 Motorola Ready For 및 Samsung DeX 대안으로 신선한 비디오로 선보였습니다. 새로운 Google Pixel 데스크탑 모드는 Motorola Ready For 및 Samsung DeX 대안으로 신선한 비디오로 선보였습니다. Aug 08, 2024 pm 03:05 PM

새로운 Google Pixel 데스크탑 모드는 Motorola Ready For 및 Samsung DeX 대안으로 신선한 비디오로 선보였습니다.

Google Pixel 9 Pro XL은 데스크톱 모드로 테스트되었습니다. Google Pixel 9 Pro XL은 데스크톱 모드로 테스트되었습니다. Aug 29, 2024 pm 01:09 PM

Google Pixel 9 Pro XL은 데스크톱 모드로 테스트되었습니다.

Google은 대부분의 사용자에게 AI Test Kitchen 및 Imagen 3를 공개합니다. Google은 대부분의 사용자에게 AI Test Kitchen 및 Imagen 3를 공개합니다. Sep 12, 2024 pm 12:17 PM

Google은 대부분의 사용자에게 AI Test Kitchen 및 Imagen 3를 공개합니다.

See all articles