CakePHP中的整页缓存

时间:2015-01-23 11:10:48

标签: cakephp caching browser-cache

我有一个搜索页面,其中包含一些搜索条件。从该页面我转到其他页面,然后单击浏览器后退按钮意味着页面刷新。实际上我不应该刷新页面而不是刷新页面应该显示一些结果而不刷新。

请给我建议。

3 个答案:

答案 0 :(得分:1)

一些建议:

  1. 使用会话存储搜索参数,当用户返回搜索页面时检查会话是否存在。如果存在则使用该参数再次查找数据或加载缓存的查询结果。
  2. 更好的选项,将表单请求类型从POST更改为GET方法。因此,当您提交搜索表单时,您的网址如下:

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

  3. 使用此url参数再次查找数据。此选项允许您与其他用户共享搜索条件。

答案 1 :(得分:1)

创建搜索值以创建唯一键,例如用md5。然后将带有此键的结果存储到您的缓存系统(文件,memcached等)。 因此,如果用户应该再次运行相同的搜索,则直接从缓存中获取结果,而不会打扰数据库。 它看起来像这样:

$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');
}

或者您想要缓存完整的渲染页面吗?

答案 2 :(得分:1)

从您的问题我明白,您需要导航到其他页面,而不像Google一样刷新页面。

但是没有使用缓存。有一个名为pushState()onpopstate()的函数可以实现此功能。

如何运作?

使用函数pushState(),我们可以在不刷新页面的情况下更改页面的URL。之前的网址会自动添加到历史记录中。因此,我们需要实现单个页面,以使用基于URL的ajax加载页面内容。

使用onpopstate()更改网址时,系统会触发pushState()功能。

示例:

http://html5.gingerhost.com/

上面的页面使用了上述功能。该页面包含4个链接(Home,Seattle,New York,London)。当我们点击这些链接时,将加载一个新的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,您可以实现这一点。