fix composite cache keys and remove setByHttp

This commit is contained in:
Doug Bell 2010-04-19 14:58:35 -05:00
parent 43413fe75c
commit 92cd204b31
10 changed files with 78 additions and 71 deletions

View file

@ -350,7 +350,7 @@ around BUILDARGS => sub {
} }
} }
my $properties = eval{$session->cache->get(["asset",$assetId,$revisionDate])}; my $properties = eval{$session->cache->get("asset".$assetId.$revisionDate)};
unless (exists $properties->{assetId}) { # can we get it from cache? unless (exists $properties->{assetId}) { # can we get it from cache?
my $sql = "select * from asset"; my $sql = "select * from asset";
my $where = " where asset.assetId=?"; my $where = " where asset.assetId=?";
@ -368,7 +368,7 @@ around BUILDARGS => sub {
$session->errorHandler->error("Asset $assetId $className $revisionDate is missing properties. Consult your database tables for corruption. "); $session->errorHandler->error("Asset $assetId $className $revisionDate is missing properties. Consult your database tables for corruption. ");
return undef; return undef;
} }
eval{ $session->cache->set(["asset",$assetId,$revisionDate], $properties, 60*60*24) }; eval{ $session->cache->set("asset".$assetId.$revisionDate, $properties, 60*60*24) };
} }
if (defined $properties) { if (defined $properties) {
@ -2369,7 +2369,7 @@ sub purgeCache {
$stow->delete('assetLineage'); $stow->delete('assetLineage');
$stow->delete('assetClass'); $stow->delete('assetClass');
$stow->delete('assetRevision'); $stow->delete('assetRevision');
eval{$self->session->cache->delete(["asset",$self->getId,$self->get("revisionDate")])}; eval{$self->session->cache->delete("asset".$self->getId.$self->get("revisionDate"))};
} }

View file

@ -165,30 +165,6 @@ sub exportGetUrlAsPath {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 getCache ( $calledAsWebMethod )
Overrides the base method to handle Snippet specific caching.
=head3 $calledAsWebMethod
If this is true, then change the cache key.
=cut
sub getCache {
my $self = shift;
my $calledAsWebMethod = shift;
my $session = $self->session;
my $cacheKey = "view_".$calledAsWebMethod.'_'.$self->getId;
if ($session->env->sslRequest) {
$cacheKey .= '_ssl';
}
my $cache = WebGUI::Cache->new($session, $cacheKey);
return $cache;
}
#-------------------------------------------------------------------
=head2 getToolbar ( ) =head2 getToolbar ( )
Returns a toolbar with a set of icons that hyperlink to functions that delete, edit, promote, demote, cut, and copy. Returns a toolbar with a set of icons that hyperlink to functions that delete, edit, promote, demote, cut, and copy.

View file

@ -270,8 +270,8 @@ override purgeCache => sub {
my $self = shift; my $self = shift;
my $cache = $self->session->cache; my $cache = $self->session->cache;
eval { eval {
$cache->delete([$self->proxiedUrl,"URL"]); $cache->delete($self->proxiedUrl."_URL");
$cache->delete([$self->proxiedUrl,"HEADER"]); $cache->delete($self->proxiedUrl."_HEADER");
}; };
super(); super();
}; };
@ -317,8 +317,8 @@ sub view {
my $cache = $self->session->cache; my $cache = $self->session->cache;
if ($requestMethod =~ /^GET$/i) { if ($requestMethod =~ /^GET$/i) {
eval { eval {
$var{header} = $cache->get([$proxiedUrl,'HEADER']); $var{header} = $cache->get($proxiedUrl.'_HEADER');
$var{content} = $cache->get([$proxiedUrl,"URL"]); $var{content} = $cache->get($proxiedUrl."_URL");
}; };
} }
@ -460,8 +460,8 @@ sub view {
} }
unless ($self->cacheTimeout <= 10) { unless ($self->cacheTimeout <= 10) {
eval{ eval{
$cache->set([$proxiedUrl,'URL'], $var{content}, $self->cacheTimeout); $cache->set($proxiedUrl.'URL', $var{content}, $self->cacheTimeout);
$cache->set([$proxiedUrl,'HEADER'], $var{header}, $self->cacheTimeout); $cache->set($proxiedUrl.'HEADER', $var{header}, $self->cacheTimeout);
}; };
} }
} }

View file

@ -115,23 +115,36 @@ Combines all feeds into a single XML::FeedPP object.
sub generateFeed { sub generateFeed {
my $self = shift; my $self = shift;
my $limit = shift || $self->maxHeadlines; my $limit = shift || $self->maxHeadlines;
my $session = $self->session;
my ( $log, $cache ) = $session->quick(qw( log cache ));
my $feed = XML::FeedPP::Atom->new(); my $feed = XML::FeedPP::Atom->new();
my $log = $self->session->log;
# build one feed out of many # build one feed out of many
my $newlyCached = 0; my $newlyCached = 0;
my $cache = $self->session->cache;
foreach my $url (split(/\s+/, $self->rssUrl)) { foreach my $url (split(/\s+/, $self->rssUrl)) {
$log->info("Processing FEED: ".$url); $log->info("Processing FEED: ".$url);
$url =~ s/^feed:/http:/; $url =~ s/^feed:/http:/;
if ($self->processMacroInRssUrl) { if ($self->processMacroInRssUrl) {
WebGUI::Macro::process($self->session, \$url); WebGUI::Macro::process($self->session, \$url);
} }
my $value = eval{$cache->get($url)};
unless ($value) { my $value = $cache->compute( $url, sub {
$value = eval{$cache->setByHttp($url, $self->cacheTimeout)}; my $ua = LWP::UserAgent->new(
$newlyCached = 1; env_proxy => 1,
} agent => "WebGUI/" . $WebGUI::VERSION,
timeout => 30,
);
my $r = $ua->get( $url );
if ( $r->is_error ) {
$session->log->warn( "Could not get syndicated content from '$url': " . $r->status_line );
}
else {
$newlyCached = 1;
return $r->decoded_content;
}
}, $self->cacheTimeout );
# if the content can be downgraded, it is either valid latin1 or didn't have # if the content can be downgraded, it is either valid latin1 or didn't have
# an HTTP Content-Encoding header. In the second case, XML::FeedPP will take # an HTTP Content-Encoding header. In the second case, XML::FeedPP will take
# care of any encoding specified in the XML prolog # care of any encoding specified in the XML prolog
@ -142,7 +155,7 @@ sub generateFeed {
$feed->merge_item($singleFeed); $feed->merge_item($singleFeed);
}; };
if ($@) { if ($@) {
$log->error("Syndicated Content asset (".$self->getId.") has a bad feed URL (".$url."). Failed with ".$@); $log->warn("Syndicated Content asset (".$self->getId.") has a bad feed URL (".$url."). Failed with ".$@);
} }
} }
@ -159,7 +172,7 @@ sub generateFeed {
} }
} }
my %seen = {}; my %seen = ();
my @items = $feed->get_item; my @items = $feed->get_item;
$feed->clear_item; $feed->clear_item;
ITEM: foreach my $item (@items) { ITEM: foreach my $item (@items) {

View file

@ -457,16 +457,19 @@ sub setByHttp {
if ($debug) { if ($debug) {
$self->session->log->debug("Called setByHttp() with URL $url."); $self->session->log->debug("Called setByHttp() with URL $url.");
} }
my $userAgent = new LWP::UserAgent;
$userAgent->env_proxy; # Why is this being done?
$userAgent->agent("WebGUI/".$WebGUI::VERSION); my $referer = "http://webgui.http.request/".$self->session->env->get("SERVER_NAME").$self->session->env->get("REQUEST_URI");
$userAgent->timeout(30); chomp $referer;
my $header = new HTTP::Headers;
my $referer = "http://webgui.http.request/".$self->session->env->get("SERVER_NAME").$self->session->env->get("REQUEST_URI"); my $ua = LWP::UserAgent->new(
chomp $referer; env_proxy => 1,
$header->referer($referer); agent => "WebGUI/" . $WebGUI::VERSION,
my $request = HTTP::Request->new(GET => $url, $header); timeout => 30,
my $response = $userAgent->request($request); default_headers => HTTP::Headers->new( referer => $referer ),
);
my $response = $ua->get( $url );
if ($response->is_error) { if ($response->is_error) {
$self->session->log->error("$url could not be retrieved."); $self->session->log->error("$url could not be retrieved.");
if ($debug) { if ($debug) {

View file

@ -182,13 +182,26 @@ sub www_viewStatistics {
return $session->privilege->adminOnly() unless canView($session); return $session->privilege->adminOnly() unless canView($session);
my ($output, $data); my ($output, $data);
my $i18n = WebGUI::International->new($session); my $i18n = WebGUI::International->new($session);
my $url = "http://update.webgui.org/latest-version.txt";
# Get the latest WebGUI version
my $url = "http://update.webgui.org/latest-version.txt";
my $cache = $session->cache; my $cache = $session->cache;
my $version = eval{$cache->get($url)}; my $value = $cache->compute( $url, sub {
if (not defined $version) { my $ua = LWP::UserAgent->new(
$version = eval{$cache->setByHttp($url, 43200)}; env_proxy => 1,
} agent => "WebGUI/" . $WebGUI::VERSION,
chomp $version; timeout => 30,
);
my $r = $ua->get( $url );
if ( $r->is_error ) {
$session->log->warn( "Could not get latest WebGUI version from '$url': " . $r->status_line );
}
else {
return $r->decoded_content;
}
} );
$output .= '<table>'; $output .= '<table>';
$output .= '<tr><td align="right" class="tableHeader">'.$i18n->get(145).':</td><td class="tableData">'.$WebGUI::VERSION.'-'.$WebGUI::STATUS.'</td></tr>'; $output .= '<tr><td align="right" class="tableHeader">'.$i18n->get(145).':</td><td class="tableData">'.$WebGUI::VERSION.'-'.$WebGUI::STATUS.'</td></tr>';
if ($version ne $WebGUI::VERSION) { if ($version ne $WebGUI::VERSION) {

View file

@ -65,7 +65,7 @@ sub delete {
my $value = delete $self->{_data}{$name}; my $value = delete $self->{_data}{$name};
my $session = $self->session; my $session = $self->session;
my $id = $session->getId; my $id = $session->getId;
eval{$session->cache->set(["sessionscratch",$id], $self->{_data}, $session->setting->get('sessionTimeout'))}; eval{$session->cache->set("sessionscratch_".$id, $self->{_data}, $session->setting->get('sessionTimeout'))};
$session->db->write("delete from userSessionScratch where name=? and sessionId=?", [$name, $id]); $session->db->write("delete from userSessionScratch where name=? and sessionId=?", [$name, $id]);
return $value; return $value;
} }
@ -84,7 +84,7 @@ sub deleteAll {
delete $self->{_data}; delete $self->{_data};
my $session = $self->session; my $session = $self->session;
my $id = $session->getId; my $id = $session->getId;
eval{$session->cache->delete(["sessionscratch",$id])}; eval{$session->cache->delete("sessionscratch_".$id)};
$session->db->write("delete from userSessionScratch where sessionId=?", [$id]); $session->db->write("delete from userSessionScratch where sessionId=?", [$id]);
} }
@ -198,7 +198,7 @@ The current session.
sub new { sub new {
my ($class, $session) = @_; my ($class, $session) = @_;
my $scratch = eval{$session->cache->get(["sessionscratch",$session->getId])}; my $scratch = eval{$session->cache->get("sessionscratch_".$session->getId)};
unless (ref $scratch eq "HASH") { unless (ref $scratch eq "HASH") {
$scratch = $session->db->buildHashRef("select name,value from userSessionScratch where sessionId=?",[$session->getId], {noOrder => 1}); $scratch = $session->db->buildHashRef("select name,value from userSessionScratch where sessionId=?",[$session->getId], {noOrder => 1});
} }
@ -253,7 +253,7 @@ sub set {
$self->{_data}{$name} = $value; $self->{_data}{$name} = $value;
my $session = $self->session; my $session = $self->session;
my $id = $session->getId; my $id = $session->getId;
eval{$session->cache->set(["sessionscratch",$id], $self->{_data}, $session->setting->get('sessionTimeout'))}; eval{$session->cache->set("sessionscratch_".$id, $self->{_data}, $session->setting->get('sessionTimeout'))};
$session->db->write("replace into userSessionScratch (sessionId, name, value) values (?,?,?)", [$id, $name, $value]); $session->db->write("replace into userSessionScratch (sessionId, name, value) values (?,?,?)", [$id, $name, $value]);
} }

View file

@ -71,7 +71,7 @@ sub end {
my $self = shift; my $self = shift;
my $session = $self->session; my $session = $self->session;
my $id = $self->getId; my $id = $self->getId;
eval{$session->cache->delete(['session',$id])}; eval{$session->cache->delete($id)};
$session->scratch->deleteAll; $session->scratch->deleteAll;
$session->db->write("delete from userSession where sessionId=?",[$id]); $session->db->write("delete from userSession where sessionId=?",[$id]);
delete $session->{_user}; delete $session->{_user};
@ -176,7 +176,7 @@ sub new {
$self->start(1); $self->start(1);
} }
else { ##existing session requested else { ##existing session requested
$self->{_var} = eval{$session->cache->get(['session',$sessionId])}; $self->{_var} = eval{$session->cache->get($sessionId)};
unless ($self->{_var}{sessionId} eq $sessionId) { unless ($self->{_var}{sessionId} eq $sessionId) {
$self->{_var} = $session->db->quickHashRef("select * from userSession where sessionId=?",[$sessionId]); $self->{_var} = $session->db->quickHashRef("select * from userSession where sessionId=?",[$sessionId]);
} }
@ -202,7 +202,7 @@ sub new {
} }
else { else {
$self->{_var}{nextCacheFlush} = $time + $session->config->get("hotSessionFlushToDb"); $self->{_var}{nextCacheFlush} = $time + $session->config->get("hotSessionFlushToDb");
eval{$session->cache->set(['session',$sessionId], $self->{_var}, $timeout)}; eval{$session->cache->set($sessionId, $self->{_var}, $timeout)};
} }
$self->session->{_sessionId} = $self->{_var}{sessionId}; $self->session->{_sessionId} = $self->{_var}{sessionId};
return $self; return $self;
@ -264,7 +264,7 @@ sub start {
userId => $userId userId => $userId
}; };
$self->session->{_sessionId} = $sessionId; $self->session->{_sessionId} = $sessionId;
eval{$session->cache->set(['session',$sessionId], $self->{_var}, $timeout)}; eval{$session->cache->set($sessionId, $self->{_var}, $timeout)};
delete $self->{_var}{nextCacheFlush}; delete $self->{_var}{nextCacheFlush};
$session->db->setRow("userSession","sessionId",$self->{_var},$sessionId); $session->db->setRow("userSession","sessionId",$self->{_var},$sessionId);
$self->{_sessionId} = $sessionId; $self->{_sessionId} = $sessionId;
@ -283,7 +283,7 @@ sub switchAdminOff {
my $self = shift; my $self = shift;
$self->{_var}{adminOn} = 0; $self->{_var}{adminOn} = 0;
my $session = $self->session; my $session = $self->session;
eval{$session->cache->set(['session',$self->getId], $self->{_var}, $session->setting->get('sessionTimeout'))}; eval{$session->cache->set($self->getId, $self->{_var}, $session->setting->get('sessionTimeout'))};
delete $self->{_var}{nextCacheFlush}; delete $self->{_var}{nextCacheFlush};
$session->db->setRow("userSession","sessionId", $self->{_var}); $session->db->setRow("userSession","sessionId", $self->{_var});
} }
@ -300,7 +300,7 @@ sub switchAdminOn {
my $self = shift; my $self = shift;
$self->{_var}{adminOn} = 1; $self->{_var}{adminOn} = 1;
my $session = $self->session; my $session = $self->session;
eval{$session->cache->set(['session',$self->getId], $self->{_var}, $session->setting->get('sessionTimeout'))}; eval{$session->cache->set($self->getId, $self->{_var}, $session->setting->get('sessionTimeout'))};
delete $self->{_var}{nextCacheFlush}; delete $self->{_var}{nextCacheFlush};
$self->session->db->setRow("userSession","sessionId", $self->{_var}); $self->session->db->setRow("userSession","sessionId", $self->{_var});
} }

View file

@ -282,7 +282,7 @@ sub cache {
for my $k (qw(_userId _user _profile)) { for my $k (qw(_userId _user _profile)) {
$userData{$k} = $self->{$k}; $userData{$k} = $self->{$k};
} }
eval{$self->session->cache->set(["user",$self->userId], \%userData, 60*60*24)}; eval{$self->session->cache->set($self->userId, \%userData, 60*60*24)};
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -1058,7 +1058,7 @@ sub new {
my $userId = shift || 1; my $userId = shift || 1;
my $overrideId = shift; my $overrideId = shift;
$userId = _create($session, $overrideId) if ($userId eq "new"); $userId = _create($session, $overrideId) if ($userId eq "new");
my $self = eval{$session->cache->get(["user",$userId])} || {}; my $self = eval{$session->cache->get($userId)} || {};
bless $self, $class; bless $self, $class;
$self->{_session} = $session; $self->{_session} = $session;
unless ($self->{_userId} && $self->{_user}{username}) { unless ($self->{_userId} && $self->{_user}{username}) {
@ -1332,7 +1332,7 @@ Deletes this user object out of the cache.
sub uncache { sub uncache {
my $self = shift; my $self = shift;
eval{$self->session->cache->delete(["user",$self->userId])}; eval{$self->session->cache->delete($self->userId)};
} }
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------

View file

@ -145,6 +145,8 @@ checkModule("Business::PayPal::API", "0.62" );
checkModule("Locales", "0.10" ); checkModule("Locales", "0.10" );
checkModule("Test::Harness", "3.17" ); checkModule("Test::Harness", "3.17" );
checkModule("DateTime::Event::ICal", "0.10" ); checkModule("DateTime::Event::ICal", "0.10" );
checkModule( "CHI", );
checkModule( "Cache::FastMmap", );
failAndExit("Required modules are missing, running no more checks.") if $missingModule; failAndExit("Required modules are missing, running no more checks.") if $missingModule;