2012-03-08 3 views
1

내가 CakePHP는 2.1의 새로운 HTTP 캐시 기능을 활용하여 내 사이트를 가속화하기 위해 노력하고있어 :CakePHP는 2.1 HTTP 캐시

class ArticlesController extends AppController { 
    public function view($id) { 
     $article = $this->Article->find(
      'first', 
      array('conditions' => array('Article.id' => $id)) 
     ); 

     $this->response->modified($article['Article']['modified']); 
     $this->set(compact('article')); 
    } 
} 

캐싱 잘 작동하지만 다른 사용자를 구분하지 않습니다 (예 경우 사용자가 로그인하여 이미 캐시 된 페이지를 방문하고 이전에 캐시 된 페이지가 표시되며 사용자 별 콘텐츠는 표시되지 않음).

  • 캐시는 사용자가 로그인되어있는 경우
  • 캐싱이 비활성화되어 다른 사용자와 매장 사이에 각 사용자에 대해 별도의 캐시를 차별 (사용자 로그인 만 사용됩니다 : 나는 일이 다음 중 하나를하고 싶습니다 관리 목적)

나는

if (AuthComponent::user('id')) { 
    $this->disableCache(); 
} 

를 추가하려고했습니다하지만이 문제를 해결하지 않는 것

,536,

누구나이 작업을 수행하는 방법을 알고 있습니까? 아니면 근본적으로 잘못된 작업을하고 있습니까?

답변

1

etag 캐싱 방법을 시도하고 아티클 ID 및 사용자 ID를 기반으로 해시를 생성 할 수 있습니다.

는 Etag입니다 헤더 (라는 엔터티 태그)가 유일하게 요청 된 리소스를 식별하는 문자열입니다 http://book.cakephp.org/2.0/en/controllers/request-response.html#the-etag-header

참조하십시오. 이것은 파일의 체크섬과 매우 흡사합니다. 캐싱은 체크섬을 비교하여 일치하는지 여부를 알려줍니다.

은 실제로 당신이 수동으로 CakeResponse :: checkNotModified을 (전화를해야 헤더) 방법을 사용의 이점을 가져 오거나 RequestHandlerComponent이 컨트롤러에 포함해야합니다 : (내가 솔루션을 게시 할 거라고 생각

<?php 
public function index() { 
    $articles = $this->Article->find('all'); 
    $this->response->etag($this->Article->generateHash($articles)); 
    if ($this->response->checkNotModified($this->request)) { 
     return $this->response; 
    } 
    ... 
} 
+0

내가 시도 $ this-> response-> etag ($ this-> Article-> generateHash ($ article))를 사용하여; 'Array to string conversion'오류가 발생하여이를 수행하지 않았습니다. 내가 generateHash에 대한 설명서를 찾을 수 없어, 그래서 그것을 디버깅 할 생각이 없어. – Tomba

+0

게다가, 나는 절대적으로 필요하지 않는 한 Etags를 사용하고 싶지 않다. – Tomba

+1

당신의 특정 요구 사항에 맞게 generateHash() 메소드를 구현해야한다. 당신은 메소드를 구현할 필요가 없지만 어떻게 든 해시를 생성해야합니다. 귀하의 경우에는 md5 ($ userId. '-'. $ articleId)와 같은 것이 필요합니다. etags를 사용하고 싶지 않다면 어쨌든 해시 키를 생성하고 캐시하는 다른 방법을 찾아야합니다. 또한 페이지의 캐싱 된 요소와 캐싱되지 않은 요소를 뷰의 사용자와 관련된 페이지 부분에 사용할 수 있습니다. – burzum

0

s) 나는 누군가를 돕는 경우에 대비하여 결국 사용했다.

class ArticlesController extends AppController { 
    public function view($id) { 
     $article = $this->Article->find(
      'first', 
      array('conditions' => array('Article.id' => $id)) 
     ); 

     if (!AuthComponent::user('id')) { 
      $this->response->etag($this->Article->generateHash($article)); 
     } 

     $this->set(compact('article')); 
    } 
} 

각 사용자에 대해 별도의 캐시를 위해 (그리고 경우에 아무도 로그인하지 않은 경우) :

완전히 로그인 한 사용자에 대한 캐싱을 사용하지 않으려면

class Article extends AppModel { 
    public function generateHash($article) { 
     if (AuthComponent::user('id')) { 
      return md5(AuthComponent::user('id') . '-' . $article['Article']['modified']); 
     } else { 
      return md5($article['Article']['modified']); 
     } 
    } 
} 

class ArticlesController extends AppController { 
    public function view($id) { 
     $article = $this->Article->find(
      'first', 
      array('conditions' => array('Article.id' => $id)) 
     ); 

     $this->response->etag($this->Article->generateHash($article)); 

     $this->set(compact('article')); 
    } 
}