http://qs1969.pair.com?node_id=15037


in reply to RE: Get chatbox lines
in thread Get chatbox lines

I like the way you get the chatterbox, by breaking into the nodelet units. I may do that instead of the match on the whole page I do right now.

I think the problem with your cache is that you are resetting old messages every time. Here's what happens. Assume the first time through the chatbox has the following lines:

user1: blah user2: bleh user3: blih
So you add those to $newmessage, which then becomes $oldmessages. The next time, the box contains:
user1: blah user2: bleh user3: blih user1: howdy
As you go through the lines, the only one that gets added to $newmessages is the last one, because the others are already in $oldmessages.

Then, and here is the problem, you remove $oldmessages! So the only message you have a record of is the last one. So the next time through, the first three messages are printed again, because they are no longer in the cache.

I don't think you need the juggling of $old and $newmessages. You can just keep one hash where you cache all the messages. The problem with this (and the reason why I didn't do it that way in my code) is that you have no way of knowing which messages are older or newer, so unless you attach a timestamp to each entry, your cache will grow indefinitely. Furthermore, if the same user says the same thing in two different occasions, the second time through it will not be printed because your program will think it's a repeated message.

Hope this helps,

--ZZamboni

Replies are listed 'Best First'.
RE: RE: RE: Get chatbox lines
by swiftone (Curate) on May 26, 2000 at 21:46 UTC
    As you go through the lines, the only one that gets added to $newmessages is the last one, because the others are already in $oldmessages.

    Then, and here is the problem, you remove $oldmessages! So the only message you have a record of is the last one. So the next time through, the first three messages are printed again, because they are no longer in the cache.

    Good catch on the lost messages. Rather than your suggestion, however, I simply moved the line that places the message into the hash outside the if(!defined()) loop.

    This means that only the messages that showed up are cached...the page should never return old messages, and my cache remains a usable size (actually, much smaller than yours! :) )

    As for the repeat messages thing, that's a feature. Quite often repeated messages ARE repeats. With a limited cache, non-accidental repeat collisions should be rare.

    Thanks for the help, it's much cleaner now with that one little change. Still not nicely packaged for an interface layer like yours is, but I admit to a small attachment to code I have written.