2017-11-01 9 views
2

최근에 CakePHP 3.2 프로젝트를 웹 호스팅 (CentOS, php 7.0/5.6 시도 모두 시도, OpCache)으로 설정하여 쿼리 결과 캐싱이 작동하지 않는다는 것을 알게되었습니다.CakePHP 쿼리 결과 캐싱

탐색 메뉴에 기본 파일 캐시 엔진을 사용합니다.

$mt = TableRegistry::get('MenuItems'); 
$menu = $mt->find('active') 
      ->select([ 
         "MenuItems.id",'Nodes.id','title', 'header', 'route', 'link', 
         'parent_id' => 'Nodes.parent_id' ]) 
      ->orderAsc('Nodes.num') 
      ->cache("sitemenu_MenuItems", 'long') 
      ->hydrate(false) 
      ->toArray(); 

나는 지역 OpenServer, IIS7IIS8 서버 및 기타 Linux 서버에이 코드없는 문제가 있었다.

그래서 무슨 일이 일어나고 : 내가 처음 스크립트를 실행하면

는 케이크 DB에서 데이터를 가져 와서 /src/tmp/cache/long/cake_sitemenu__menu_items에 캐시를 기록합니다. 파일이 평소와 같이 내용을 직렬화했습니다.
하지만 페이지를 새로 고침 할 때 빈 결과가 나타납니다.

오류 또는 디버그 로그에 아무 것도 없습니다.

캐시 키를 소문자로했지만 행운이 없었습니다.

아이디어가 있으십니까?


내가 기본 캐싱 자체를 확인하는 테스트 코드를 추가했습니다 :

public function qwe(){ 
      $data = ['key' => 'value']; 
      //Cache::write('test', $data, 'long'); 
      $result = Cache::read('test', 'long'); 
      $result[] = 'xxx'; 
      $this->setJsonResponse($result); 
    } 

를이 코드는 올바른 결과를 반환합니다.


지금까지 내가 Cake\Cache\Egine\FileEngine 클래스의 일부 문제 read() 방법이 있음을 발견했다. 223 :

$data = unserialize((string)$data); 

$data은 호스팅 및 로컬 시스템에서 호출하기 전에 동일한 값을 갖습니다. 그리고 unserialize은 빈 items 필드 개체를 반환합니다; 당신이 볼 수 있듯이 PHP 7.0.15 and PHP 7.1.0 ResultSet Caching #10111

+0

IIS8.5 및 PHP 7.1.12와 동일한 – teran

답변

0

, dubugging 동안 나는 그 문제는 SplFixedArrayResultSet 사용과 unserialize()에 의해 발생했다 발견 :


은 좋은 SplFixedArrayunserialize

HAH 뭔가가있을 것 같다.

그 후 나는 this topic: PHP 7.0.15 and PHP 7.1.0 ResultSet Caching #10111입니다.

제 경우에는 PHP 7.0.24, 7.1.5, 5.6.305.5.38을 사용하려고했습니다. 그리고 5.5는 잘 작동했습니다.

이 문제는 해당 항목 토론에서 수정되었으므로 에서 ResultSet::serializeunserialize 메서드를 복사했습니다. 트릭은 보통 배열로 데이터를 serialize하는 것이지 SplFixedArray이 아니라 serialize하는 것이 었습니다.

\Cake\ORM\ResultSet

313:  public function serialize() 
314:  { 
315:   if (!$this->_useBuffering) { 
316:    $msg = 'You cannot serialize an un-buffered ResultSet. Use Query::bufferResults() to get a buffered ResultSet.'; 
317:    throw new Exception($msg); 
318:   } 
319: 
320:   while ($this->valid()) { 
321:    $this->next(); 
322:   } 
323: 
324:   if ($this->_results instanceof SplFixedArray) { 
325:    return serialize($this->_results->toArray()); 
326:   } 
327: 
328:   return serialize($this->_results); 
329:  } 
330: 

339:  public function unserialize($serialized) 
340:  { 
341:   $results = (array)(unserialize($serialized) ?: []); 
342:   $this->_results = SplFixedArray::fromArray($results); 
343:   $this->_useBuffering = true; 
344:   $this->_count = $this->_results->count(); 
345:  }