KSURi has asked for the wisdom of the Perl Monks concerning the following question:

Hello, monks!


I wonder, if there is any way to store XS object (e.g. an instance of XML::LibXML) to memcache?

Replies are listed 'Best First'.
Re: Saving XS-objects to memcached
by ikegami (Patriarch) on Jul 21, 2009 at 20:04 UTC

    What do you mean by XS object? Objects created by XS code are no different than objects create by Perl code if they are visible to Perl.

    And if you're talking about something that's not visible to Perl, then Perl can't help you.

    Unfortunately, the beef of LibXML objects is not visible to Perl.

    You could convert the LibXML objects to XML and store that in the cache. The objects need to be serialized anyway, so why not serialize them to XML.

    # Store into cache $cache->set( xml => $xml_doc->toString() ); # Fetch from cache my $xml_doc = XML::LibXML->new()->parse_string( $cache->get( 'xml' ));

    Update: Added possible solution.

      Yes, I was talking about internals of objects created by XS code. The main reason for saving LibXML instances is that I want to save XML parsing time. Because of this, storing stringified XML is not an option for me (actually, this approach will save only file reading time).

        The main reason for saving LibXML instances is that I want to save XML parsing time.

        That statement is nonsense. Like I said earlier, the objects need to be serialised no matter what. What reason do you have to believe that serialising to XML would be slower to serialising to whatever you can come up with? Given that XML is very easy to generate and parse, and given that LibXML is a *very* fast parser, serialising to XML might actually be faster! In fact, considering it has direct access to the underlying objects, I'd bet it runs circles around your serialiser and parser. Come back when you have benchmarks.

Re: Saving XS-objects to memcached
by Khen1950fx (Canon) on Jul 21, 2009 at 19:35 UTC