ページングデータの取得

2278 ワード

phpバージョン-テストなしで参考のみ
  • が使用する場合、これを継承し、対応する方法
      score   :   cursor->score 【  】 length   
       *
       *      :
       *          ,      ,      
       *
       * User: xjyin
       * Date: 15/11/5
       * Time:   7:45
       */
    
      class Cursor {
    
          private $member;
    
          private $score;
      }
    
      abstract class CursorTimelineGetter {
    
          private $cursor;
    
          private $length;
    
          function _construct($cursor, $length) {
              $this->cursor = $cursor;
              $this->length = $length;
          }
    
          //      cursor array,        $length
          abstract protected function getFromCache($cursor, $length);
    
          //      cursor array,        $length
          abstract protected function getFromDB($cursor, $length);
    
          //                 , dbResults  cursor  array
          abstract protected function saveToCache($dbResults);
    
          //            ,          
          //         zAdd(key, map)   ,        member  zAdd
          abstract protected function patchToCache($cursor);
    
          public function exec() {
              $cacheResults = $this->getFromCache($this->cursor, $this->length);
              if(!$cacheResults) {
                  $cacheResults = array();
              }
              //                ,     
              if(count($cacheResults) >= $this->length) {
                  return $cacheResults;
              }
              //         ,          
              $lengthFromDB = $this->length - count($cacheResults);
              $cursorFromDb = null;
              //              ,   cursor      db  cache ,      
              //              ,       ,     patchToCache
              if (count($cacheResults) <= 0) {
                  $this->patchToCache($this->cursor);
                  $cursorFromDb = $this->cursor;
              }
              else {
                  $cursorFromDb = $cacheResults[count($cacheResults) - 1];
              }
              $dbResults = $this->getFromDB($cursorFromDb, $lengthFromDB);
              if(!$dbResults or count($dbResults) == 0) {
                  return $cacheResults;
              }
              $this->saveToCache($dbResults);
              return array_merge($cacheResults, $dbResults);
          }
      }
  • を実現する.