2015-01-23 1 views
2

검색 기준이 하나 인 검색 페이지가 하나 있습니다. 해당 페이지에서 다른 페이지로 이동하여 브라우저의 뒤로 단추를 클릭하면 페이지가 새로 고쳐 졌음을 의미합니다. 사실 나는 페이지를 새로 고침하는 대신 새로 고침해서는 안되며 페이지를 새로 고치지 않고 결과가 표시되어야합니다.CakePHP의 전체 페이지 캐시

친절하게 제안 해주세요.

답변

1

Google에서와 같이 페이지를 새로 고치지 않고도 다른 페이지로 이동해야한다는 점을 질문드립니다.

하지만 캐시를 사용하지 않았습니다. 이 기능을 사용하려면 pushState()onpopstate()이라는 기능이 있습니다.

어떻게 작동합니까?

기능이 pushState() 인 경우 페이지를 새로 고치지 않고도 페이지의 URL을 변경할 수 있습니다. 이전 URL이 자동으로 기록에 추가됩니다. 따라서 url 기반의 ajax를 사용하여 페이지의 내용을로드하려면 단일 페이지를 구현해야합니다.

pushState()을 사용하여 URL을 변경하면 onpopstate()이 트리거됩니다.

예 :

http://html5.gingerhost.com/

상기 페이지는 위의 함수를 사용한다. 이 페이지는 4 개의 링크 (홈, 시애틀, 뉴욕, 런던)로 구성됩니다. 해당 링크를 클릭하면 페이지를 새로 고치지 않고 새 URL이로드됩니다.

<title></title> 
<ul id="menu" class="clearfix"> 
    <li class="current"><a href="/">Home</a></li> 
    <li><a href="/seattle">Seattle</a></li> 
    <li><a href="/new-york">New York</a></li> 
    <li><a href="/london">London</a></li> 
</ul> 
<article> 
    <h1></h1> 
    <div id="image"></div> 
    <div id="articletext"></div> 
    <div class="clear"></div> 
</article> 


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script> 

    <script> 
    // THIS IS WHERE THE MAGIC HAPPENS 
     $(function() { 
      //Capture the click on the links 
      $('#menu a').click(function(e) { 
       href = $(this).attr("href"); 
       // Call a function to load the content based on the url 
       loadContent(href); 
       // HISTORY.PUSHSTATE add the url to the history 
       history.pushState('', 'New URL: '+href, href); 
       e.preventDefault(); 
      }); 

      // THIS EVENT MAKES SURE THAT THE BACK/FORWARD BUTTONS WORK AS WELL 
      window.onpopstate = function(event) { 
       console.log("pathname: "+location.pathname); 
       // Load the content on clicking back or forward using its url 
       loadContent(location.pathname); 
      }; 

     }); 

     function loadContent(url){ 
      // USES JQUERY TO LOAD THE CONTENT PROVIDED BY content.php 
      $.getJSON("content.php", {cid: url, format: 'json'}, function(json) { 
        // output provider by content.php like {"title":"Seattle - Part of a demo for #ProSEO","h1":"Seattle","article #articletext":"<p>Seattle is the northernmost major city in the contiguous United States, and the largest city in the Pacific Northwest and the state of Washington. It is a major seaport situated on a narrow isthmus between Puget Sound (an arm of the Pacific Ocean) and Lake Washington, about 114 miles (183 km) south of the Canada - United States border, and it is named after Chief Sealth \"Seattle\", of the Duwamish and Suquamish native tribes. Seattle is the center of the Seattle-Tacoma-Bellevue metropolitan statistical area--the 15th largest metropolitan area in the United States, and the largest in the northwestern United States.<\/p><p>Seattle is the county seat of King County and is the major economic, cultural and educational center in the region. The 2010 census found that Seattle is home to 608,660 residents within a metropolitan area of some 3.4 million inhabitants. The Port of Seattle, which also operates Seattle-Tacoma International Airport, is a major gateway for trade with Asia and cruises to Alaska, and is the 8th largest port in the United States in terms of container capacity.<\/p>","#image":"<img class=\"thumbnail\" alt=\"\" src=\"seattle.jpg\">"} 
        // THIS LOOP PUTS ALL THE CONTENT INTO THE RIGHT PLACES(values will be set) 
        $.each(json, function(key, value){ 
         $(key).html(value); 
        }); 
       }); 
     } 
    </script> 

당신이 내용을 렌더링하고 JSON 응답을 제공하고 JQuery와에 통과 할 수 있습니다 당신이 할 수 그것을 달성하기 위해 할 수있는 위 등.

1

제안 : 매장 검색 매개 변수에

  1. 를 사용하여 세션, 세션이 존재하는 경우 다시 사용자가 페이지 체크를 검색 할 수 있습니다. 존재하는 경우 해당 매개 변수를 사용하여 데이터를 다시 찾거나 캐시 된 쿼리 결과를로드합니다.
  2. 더 나은 옵션은 양식 요청 유형을 POST에서 GET 방법으로 변경합니다.

    http://myhost.com/search?q=my+term&param1=zzz&param2=&param3=

사용이 URL 매개 변수가 데이터를 다시 찾을 : 당신이 검색 양식을 제출할 때 같은, 당신의 URL이 보인다. 이 옵션을 사용하면 검색 기준을 다른 사용자와 공유 할 수 있습니다.

1

검색 키를 생성하여 고유 한 키를 생성합니다. md5. 그런 다음이 키를 사용하여 결과를 캐싱 시스템 (파일, memcached)에 저장하십시오. 따라서 사용자가 동일한 검색을 다시 실행해야 db를 귀찮게하지 않고 캐시에서 결과를 직접 가져와야합니다.

$sCacheKey = md5(json_encode($this->data)); 
$result = Cache::read($sCacheKey, 'short'); 
if ($result === false) { 
    $result = $this->YourModel->find(conditions etc....); 
    Cache::write($sCacheKey, $li, 'short'); 
} 

을 또는 당신은 당신의 완전한 렌더링 된 페이지를 캐시하고 싶어 : 그것은 다음과 같이 수 있을까?