Friday, April 20, 2012

CakePHP Cache Help Needed

Does anyone have experience with Cake cache who's willing to lend me a hand?

Here is my situation:

In the footer of my website I am pulling in my 3 most recent blog posts from Wordpress.com (the website, not a local installation). The way I am accomplishing this is by parsing the XML feed from WP. I've created a component to do this:

=======================================

App::uses('Component', 'Controller');
App::uses('Xml', 'Utility');

class WordpressComponent extends Component {

    public $wp_feed = 'http://blog.mydomain.com/feed/';

    public $numberOfItems = 3;

    public function getPosts() {
        // Parse XML from Wordpress Blog
        $xml = Xml::build($this->wp_feed);
        $parsed_xml = Set::reverse($xml);
        $blog_posts = $parsed_xml['rss']['channel']['item'];

        // Build simplified array of post title and link
        $result = array();
        $counter = 0;
        foreach ($blog_posts as $post) {
            $tmp = array();
            $tmp['title'] = $post['title'];
            $tmp['link'] = $post['link'];
            $tmp['pubDate'] = date("F d, Y", strtotime($post['pubDate']));
            array_push($result, $tmp);
            $counter++;
            if ($counter >= $this->numberOfItems) {
                break;
            }
        }
        return $result;
    }

}

=======================================

Then, since I need the posts to show up on all my pages, I've done the following in my AppController:

=======================================

class AppController extends Controller {

    public $components = array('Session', 'Wordpress');

    public function beforeFilter() {
        $wordpress_posts = $this->Wordpress->getPosts();
        $this->set('wordpress_posts', $wordpress_posts);
    }

}

=======================================

In am then displaying the results in the footer of my layout file.

The problem is that it's taking a noticeable amount of time to parse the XML and relay the posts to my layout. I'm assuming this is because it's doing the same thing on every page.

What I would like to know is how I can cache the posts so that I'm not having to parse the XML for every page load. Unfortunately, however, I don't have any experience working with cache - will someone please help me come up with a solution?

Thanks in advance.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: