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

ssdb的Yii cache扩展

Jun 06, 2016 pm 08:12 PM
cachegoogleLevelyii확장하다

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类:

    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으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

WebStorm Mac 버전

WebStorm Mac 버전

유용한 JavaScript 개발 도구

SublimeText3 중국어 버전

SublimeText3 중국어 버전

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

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

mPDF

mPDF

mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기