[917] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[D-O-H] r904 - branches/barnowl_sqlite/owl/perl/lib/BarnOwl/MessageList

daemon@ATHENA.MIT.EDU (nelhage@MIT.EDU)
Thu Oct 29 18:11:07 2009

Resent-From: nelhage@mit.edu
Resent-To: barnowl-dev-mtg@charon.mit.edu
X-Original-To: nelhage@nelhage.com
To: dirty-owl-hackers@MIT.EDU
From: nelhage@MIT.EDU
Reply-to: dirty-owl-hackers@MIT.EDU
Date: Thu, 24 Jan 2008 18:52:07 -0500 (EST)

Author: nelhage
Date: 2008-01-24 18:52:06 -0500 (Thu, 24 Jan 2008)
New Revision: 904

Modified:
   branches/barnowl_sqlite/owl/perl/lib/BarnOwl/MessageList/SQL.pm
Log:
Cache messages using Cache::Memory


Modified: branches/barnowl_sqlite/owl/perl/lib/BarnOwl/MessageList/SQL.pm
===================================================================
--- branches/barnowl_sqlite/owl/perl/lib/BarnOwl/MessageList/SQL.pm	2008-01-24 22:59:22 UTC (rev 903)
+++ branches/barnowl_sqlite/owl/perl/lib/BarnOwl/MessageList/SQL.pm	2008-01-24 23:52:06 UTC (rev 904)
@@ -18,20 +18,22 @@
 use SQL::Abstract;
 use JSON;
 use POSIX qw(ctime);
+use Cache::Memory;
 
 my $MESSAGES = 'messages';
 my $message_fields = [qw(id msg_time protocol body direction)];
 my $ATTRS = 'attributes';
 my $attr_fields    = [qw(message_id key value)];
 
-__PACKAGE__->mk_ro_accessors(qw(db));
+__PACKAGE__->mk_ro_accessors(qw(db cache));
 __PACKAGE__->mk_accessors(qw(msg_iter attr_iter attr_lookahead _next_id));
 
 sub new {
     my $class = shift;
     my $db = DBIx::Simple->new("DBI:SQLite:dbname=$ENV{HOME}/.owl/messagedb",
                              {RaiseError => 1, AutoCommit => 1});
-    my $self = {db => $db};
+    my $cache = Cache::Memory->new(default_expires => '30 sec');
+    my $self = {db => $db, cache => $cache};
     bless($self, $class);
 
     my $maxq = $self->db->query("SELECT MAX(id) FROM $MESSAGES")
@@ -110,6 +112,7 @@
 sub iterate_next {
     my $self = shift;
     my %msg;
+    my $msg;
     my $next = $self->msg_iter->fetch;
     unless($next) {
         return undef;
@@ -123,14 +126,21 @@
         load_attr(\%msg, $attr);
         $attr = $self->attr_iter->fetch;
     }
-    return BarnOwl::Message->new(%msg);
+    $msg = BarnOwl::Message->new(%msg);
+    $self->cache->set($msg->id => $msg);
+    return $msg;
 }
 
 sub get_by_id {
     my $self = shift;
     my $id = shift;
+    my $msg;
     my %msg;
-    my $msg = $self->db->select($MESSAGES, $message_fields, {id => $id})
+
+    $msg = $self->cache->get($id);
+    if($msg) { return $msg; }
+
+    $msg = $self->db->select($MESSAGES, $message_fields, {id => $id})
       or die($self->db->error);
     my $attrs = $self->db->select($ATTRS, $attr_fields, {message_id => $id})
       or die($self->db->error);
@@ -142,7 +152,10 @@
     while(my $row = $attrs->fetch) {
         load_attr(\%msg, $row);
     }
-    return BarnOwl::Message->new(%msg);
+
+    $msg = BarnOwl::Message->new(%msg);
+    $self->cache->set($msg->id => $msg);
+    return $msg;
 }
 
 sub add_message {


home help back first fref pref prev next nref lref last post