Manikandan Thangaraj
Manikandan Thangaraj

Reputation: 1594

Full Page Cache in CakePHP

I have one search page with some search criteria. From that page I going to some other page and clicking browser back button means the page was refreshing. Actually I page should not be refresh instead on refreshing the page should display with some result with out refreshing.

Kindly give me suggestions.

Upvotes: 2

Views: 394

Answers (3)

Mahendran Sakkarai
Mahendran Sakkarai

Reputation: 8549

From you question i understand that you need navigate to some other page without refreshing the page as like google.

But its not done using cache. There are function called pushState() and onpopstate() to make this.

How it will work?

With the function pushState(), we can able to change the url of a page without refreshing the page. And the previous url will automatically added to the history. So we need implement a single page to load the content of the page using ajax based on the url.

The function onpopstate() will be triggered when a url is changed using pushState().

Example:

http://html5.gingerhost.com/

The above page uses the above function. That page consist of 4 links (Home, Seattle, New York, London). When we click on those link a new url will be loaded without refreshing the page.

<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>

As like above you can able to render the content and provide a json response and pass it to the jquery and you can able to achieve that.

Upvotes: 1

Calamity Jane
Calamity Jane

Reputation: 2686

Create the search values to create a unique key e.g. with md5. Then store the result with this key to your caching system ( file, memcached whatever). So if a user should run the same search again fetch the result directly from cache without bothering the db. It could look like this:

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

Or do you want to cache your complete rendered page?

Upvotes: 1

Salines
Salines

Reputation: 5767

Some suggestions:

  1. Use session to store search parameters, when user back to search page check if session exist. If exist use that parameters to find data again or load cached query results.

  2. Better options, change form request type from POST to GET method. So when you submit search form, your url looks like:

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

Use this url parameters to find data again. This option allow you to share search criteria with other users.

Upvotes: 1

Related Questions