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

Hi everyone, had a question rolling around in my head for a few days now and I can't seem to come up with a good solution and am looking for suggestions.

If a person were to implement Inside Out classes as described in Conway's Perl Best Practices, how does one implement caching of the individual objects with something like Cache::FastMMap or Cache::Memcached?

Because of the nature of the inside out object, it is just a unique indentifier and caching it isn't very useful, the associated data is what needs to be cached.

What I'm looking for specifically is not so much to avoid the cycles creating the object, but to avoid unnecessarily hitting a database or other storage to retrieve the data if it is cached.

Any ideas?

Frank Wiles <frank@wiles.org>
http://www.wiles.org

Replies are listed 'Best First'.
Re: Inside Out Classes with caching
by perrin (Chancellor) on Sep 19, 2005 at 17:17 UTC
    You'd have to access all of the object's data using the same mechanism it uses to fetch properties. I suspect you'd be better off just ditching the inside-out classes idea if you want to cache the objects. (I admit to being totally unconvinced about the value of inside-out objects in general.)

      I have found one circumstance where inside-out objects are required: when you have overloaded the dereferencing operator for the data type which implements you object. For Data::Postponed, I overloaded every dereferencing operator and no longer had any way to access the contents of my object.

      I haven't worked with any of the caching modules, so I'm curious to understand what the underlying issues are. If the object can be serialized, it can be cached, no? Is it just that it's easy to throw a hash-based object at the caching modules for serialization? Whereas with an inside-out object, you'd have to explicitly write freeze/thaw routines?

      It also occurs to me that it may depend on how you construct the inside-out class. To perrin's point -- you may not want to use inside-out classes for this. Some of the typical benefits of inside out classes include

      • Strong encapsulation of data
      • Ability to subclass any other class (e.g. written by someone else) without tight coupling to the underlying nature of the class

      If you're caching, you're sort of explictly breaking encapsulation anyway for the purpose of efficiency, so unless the second point is important you may not need them..

      On the other hand, there's no reason why you can't write an inside-out class that uses the blessed reference as the key to a hash with the value being a hash-reference containing the data. (Like a regular hash-based class, but with an extra level of indirection.) That preserves the ability to subclass anything and mostly keeps the strong encapsulation. It might then be easier/faster to write up a freeze/thaw routine to manipulate that underlying hash. (At which point you break encapsulation, but only via a class freeze/thaw routine.)

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        Is it just that it's easy to throw a hash-based object at the caching modules for serialization? Whereas with an inside-out object, you'd have to explicitly write freeze/thaw routines?

        Yes. In theory, you could add something to Class::Std that works with Storable's hooks, but I don't think it's there already.

      Yeah, that was my general thinking as well. I just wanted to see if someone had some brilliant idea I hadn't come up with on how to cache them.

      Frank Wiles <frank@wiles.org>
      http://www.wiles.org

Re: Inside Out Classes with caching
by adrianh (Chancellor) on Sep 22, 2005 at 08:21 UTC
    If a person were to implement Inside Out classes as described in Conway's Perl Best Practices, how does one implement caching of the individual objects with something like Cache::FastMMap or Cache::Memcached?

    Serialise with Class::Std::Storable ?