Results 1 to 10 of 10

Thread: Setting cache duration for SimplePie in WP?

  1. #1
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Icon5 Setting cache duration for SimplePie in WP?

    I am quite happy to see SimplePie included in WP (sick of making patches to Magpie on every upgrade) but documentation is zilch. Not uncommon with WP, but especially confusing with relatively complex object-oriented code as SimplePie has.

    I am having trouble defining cache duration, it seems to default to 12 hours which is way too long. Magpie had constant for that, but no such thing in sight for SimplePie and caching itself is confusing with special WP class that extends SimplePie object...

    Lost me. I found some filter called 'wp_feed_cache_transient_lifetime' but overloading it does nothing (or I don't overload it correctly or whatever).

    Any ideas what to kick so it works?
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

  2. #2
    DD32 is offline Hello World
    Join Date
    Jul 2009
    Posts
    39

    Default

    that filters correct.

    The reason its not working for you, is because the cached result will have the 12hr lifespan, and NEW requests will get your custom timeout.

    Also, FYI: In 2.9, that filter will also pass in t he URL, so you can do url-based filtering of the timeout :) (I have it setup for a 12hr timeout, unless its a local feed, in which case its 10mins)

    The only time i can think of that it wont work correctly, is if an Object cache is in use and doesnt respect the timeout variable.

  3. #3
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    Thanks for the info, will tweak it some more.

    For the context I am coding amazingly stupid task of making SimplePie object fake Magpie-like output. :)
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

  4. #4
    DD32 is offline Hello World
    Join Date
    Jul 2009
    Posts
    39

    Default

    You'd be better off just sticking with Magpie if you've got something that relies on it...

    I personally don't like the simplepie output, too OO, give me a standard flat array anytime..

    I started on writing a Simplepie->Magpie wrapper just to prove to someone i could.... Gave up pretty soon when i realised just how much of a waste of time it was.

    (Oh, just delete all the transient_* items from your options table in the DB every time you fiddle with your code btw :))

  5. #5
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    As above - I am sick of patching Magpie on every WP upgrade (for enclosures support). It is old.

    And I already have working wrapper (only obscure issue of conflicting tags in different namespaces left to fix). Don't know if it will be accepted into plugin I made it for but always can fork for myself otherwise.
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

  6. #6
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    Why would you want the output to be like Magpie? Magpie is incredibly difficult and annoying to use, IMO.

    I wish there was a better way to do the cache lifetime. Been thinking about it a while. Current best way:

    PHP Code:
    <?php
    // get a feed every hour
    function hourly_feed() { return 3600; }
    add_filter'wp_feed_cache_transient_lifetime''hourly_feed');
    $feed fetch_feed'http://example.com/feedurl' );
    remove_filter('wp_feed_cache_transient_lifetime''hourly_feed');

    // loop through the first five items, display title and content, with title linked
     
    foreach ($feed->get_items(05) as $item) { ?>
    <h2 class="title"><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h2>
    <?php echo $item->get_content();
     } 
    ?>
    Perhaps an extra option needs to be added to fetch_feed, to remove that filter nonsense. I'll make a ticket for it.

    Edit: yes, I know you can filter on feed url as well, like so:

    PHP Code:
    function hourly_feed($age$url) { 
    if (
    $url == 'http://example.com/feedurl'$age=3600
    return 
    $age;
    }
    add_filter'wp_feed_cache_transient_lifetime''hourly_feed',10,2); 
    Maybe removing the extraneous filter from fetch_feed makes more sense.

    Hmm.. Actually, there's a bug here. Fixing it would be best.
    Last edited by Otto; 02-02-2010 at 11:38 AM.

  7. #7
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    @Otto

    Plugin I am poking has... interesting code. :) Basically it runs array of Magpie item arrays against pseudo-code markup (from user input) and changes that array on the fly, while doing some cleanup routines at the same time.

    I see no sane way to easily upgrade that to SimplePie, and neither does developer. Since his decision was to ship own copy of Magpie with latest version of plugin (eeeek) I now really hope it gets switched to SimplePie even if using peculiar wrapper.

    Thanks for the snippet. Current cache management for SimplePie looks like a mess indeed. :)
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

  8. #8
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    Next level. :)

    Widget uses fetch_feed and I want user to control timeout for this specific widget via its settings.

    Can filter be used from inside widget object? Or I should re-code feed with my values inside widget instead of using fetch_feed?
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

  9. #9
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    Wrap the add and remove filter lines around the call to fetch_feed. Should work.

  10. #10
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    Quote Originally Posted by Otto View Post
    Wrap the add and remove filter lines around the call to fetch_feed. Should work.
    I hadn't got to studying PHP objects properly yet. :) I need a variable and function that returns it... How would this work with object?

    Declare variable as object field, assign instance data to it... Then use in method and add method as filter?

    PHP Code:

    class AppnewsWidget extends WP_Widget {

    var 
    $cache;

    function 
    set_cache() {return $this->cache;}

    function 
    widget($args$instance) {
    extract($args);
    $this->cache=$instance['cache'];

    add_filter'wp_feed_cache_transient_lifetime'$this->set_cache);
    $feed fetch_feed'http://example.com/feedurl' );
    remove_filter('wp_feed_cache_transient_lifetime'$this->set_cache); 
    Something like this?..
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •