Committer: emazin
LJSUP-8510: Portal "LJ Times"U branches/LJTimes/cgi-bin/LJ/ExtBlock.pm
Modified: branches/LJTimes/cgi-bin/LJ/ExtBlock.pm =================================================================== --- branches/LJTimes/cgi-bin/LJ/ExtBlock.pm 2011-05-13 01:03:34 UTC (rev 10505) +++ branches/LJTimes/cgi-bin/LJ/ExtBlock.pm 2011-05-13 01:20:54 UTC (rev 10506) @@ -10,9 +10,9 @@ use base 'LJ::MemCacheable'; *_memcache_id = \&blockid; sub _memcache_key_prefix { "sup_extblock" } -sub _memcache_stored_props { qw/ 1 blockid blocktext / } +sub _memcache_stored_props { qw/ 2 blockid blocktext last_updated / } *_memcache_hashref_to_object = \*_new; -sub _memcache_expires { 3600 } +sub _memcache_expires { shift->{expire} || 3600 } ## @@ -49,6 +49,7 @@ sub load_by_id { my $class = shift; my $id = shift; + my $params = shift; ## 1. Find in local cache return $LJ::REQ_CACHE_EXT_BLOCK{$id} if $LJ::REQ_CACHE_EXT_BLOCK{$id}; @@ -68,6 +69,9 @@ my $hash = $sth->fetchrow_hashref(); return unless $hash; + $hash->{expire} = $params->{expire} + if $params->{expire}; + $self = $class->_new($hash); $self->_store_to_memcache; $LJ::REQ_CACHE_EXT_BLOCK{$id} = $self; @@ -75,6 +79,59 @@ } ## +## Constructor: returns hashref of ID => ExtBlock object with undefs for non-exsistent ones +## + +sub load_multi { + my $class = shift; + my $ids = shift; + my $params = shift; + + return undef + unless ref $ids eq 'ARRAY' && scalar @$ids; + + my %ret = (); + + ## 1. Find in local cache + for (@$ids) { + $ret{$_} = $LJ::REQ_CACHE_EXT_BLOCK{$_}; + } + + @$ids = grep { !defined $ret{$_} } keys %ret; + return \%ret + unless scalar @$ids; + + ## 2. Find in memcache + %ret = (%ret, %{ $class->_load_from_memcache_multi($ids) }); + for (@$ids) { + $LJ::REQ_CACHE_EXT_BLOCK{$_} = $ret{$_}; + } + + @$ids = grep { !defined $ret{$_} } keys %ret; + return \%ret + unless scalar @$ids; + + ## 3. Load from DB, use master (we need to populate memcache) + my $dbh = LJ::get_db_writer(); + my $sth = $dbh->prepare('SELECT * FROM sup_extblocks WHERE blockid in ('.join(',',map {'?'} @$ids).')'); + $sth->execute(@$ids) + or confess 'failed to fetch ext blocks by ids: ' . join(',',@$ids) . ', error = ' . DBI->errstr; + + while (my $row = $sth->fetchrow_hahref) { + $row->{expire} = $params->{expire} + if $params->{expire}; + + my $obj = $class->_new($row); + $obj->_store_to_memcache; + $LJ::REQ_CACHE_EXT_BLOCK{$row->{blockid}} = $obj; + } + + $sth->finish; + + return \%ret; +} + +## ## Constructor, creates a new ExtBlock or replaces old one. ## sub create_or_replace {