From 2a94b0806cfd8ba08caa41d44a9203b27577439d Mon Sep 17 00:00:00 2001 From: JT Smith Date: Sun, 27 Sep 2009 17:33:46 -0500 Subject: [PATCH] added logging --- lib/WebGUI/Asset/Wobject/SyndicatedContent.pm | 2 +- lib/WebGUI/Cache.pm | 73 +++++++++++++------ lib/WebGUI/Operation/Statistics.pm | 2 +- t/Cache.t | 4 +- 4 files changed, 54 insertions(+), 27 deletions(-) diff --git a/lib/WebGUI/Asset/Wobject/SyndicatedContent.pm b/lib/WebGUI/Asset/Wobject/SyndicatedContent.pm index a0c73ba9a..5ccf47c86 100644 --- a/lib/WebGUI/Asset/Wobject/SyndicatedContent.pm +++ b/lib/WebGUI/Asset/Wobject/SyndicatedContent.pm @@ -144,7 +144,7 @@ sub generateFeed { } my $value = eval{$cache->get($url)}; unless ($value) { - $value = eval{$cache->setByHttp($url, $url, $self->get("cacheTimeout"))}; + $value = eval{$cache->setByHttp($url, $self->get("cacheTimeout"))}; $newlyCached = 1; } # if the content can be downgraded, it is either valid latin1 or didn't have diff --git a/lib/WebGUI/Cache.pm b/lib/WebGUI/Cache.pm index b4aa71478..40f53ec2f 100644 --- a/lib/WebGUI/Cache.pm +++ b/lib/WebGUI/Cache.pm @@ -117,19 +117,24 @@ Throws WebGUI::Error::Connection and WebGUI::Error. sub flush { my ($self) = @_; + my $log = $self->session->log; + $log->debug("Called flush() on cache."); my $memcached = $self->getMemcached; Memcached::libmemcached::memcached_flush($memcached); if ($memcached->errstr eq 'SYSTEM ERROR Unknown error: 0') { + $log->debug("Cannot connect to memcached server."); WebGUI::Error::Connection->throw( error => "Cannot connect to memcached server." ); } elsif ($memcached->errstr eq 'NO SERVERS DEFINED') { + $log->warn("No memcached servers specified in config file."); WebGUI::Error->throw( error => "No memcached servers specified in config file." ); } elsif ($memcached->errstr ne 'SUCCESS') { + $log->debug("Couldn't flush cache because ".$memcached->errstr); WebGUI::Error->throw( error => "Couldn't flush cache because ".$memcached->errstr ); @@ -142,7 +147,7 @@ sub flush { Retrieves a key value from the cache. -Throws WebGUI::Error::InvalidParam, WebGUI::Error::ObjectNotFound, WebGUI::Error::Connection and WebGUI::Error. +Throws WebGUI::Error::InvalidObject, WebGUI::Error::InvalidParam, WebGUI::Error::ObjectNotFound, WebGUI::Error::Connection and WebGUI::Error. =head3 name @@ -155,31 +160,43 @@ sub get { 1, { type => SCALAR | ARRAYREF }, ); + my $log = $self->session->log; + my $key = $self->parseKey($name); + $log->debug("Called get() on cache key $key."); my $memcached = $self->getMemcached; - my $content = Memcached::libmemcached::memcached_get($memcached, $self->parseKey($name)); + my $content = Memcached::libmemcached::memcached_get($memcached, $key); if ($memcached->errstr eq 'NOT FOUND' ) { + $log->debug("The cache key $key has no value."); WebGUI::Error::ObjectNotFound->throw( - error => "The cache key $name has no value.", - id => $name, + error => "The cache key $key has no value.", + id => $key, ); } elsif ($memcached->errstr eq 'NO SERVERS DEFINED') { + $log->warn("No memcached servers specified in config file."); WebGUI::Error->throw( error => "No memcached servers specified in config file." ); } elsif ($memcached->errstr eq 'SYSTEM ERROR Unknown error: 0') { + $log->debug("Cannot connect to memcached server."); WebGUI::Error::Connection->throw( error => "Cannot connect to memcached server." ); } elsif ($memcached->errstr ne 'SUCCESS') { + $log->debug("Couldn't get $key from cache because ".$memcached->errstr); WebGUI::Error->throw( - error => "Couldn't get $name from cache because ".$memcached->errstr + error => "Couldn't get $key from cache because ".$memcached->errstr ); } $content = Storable::thaw($content); - return undef unless ref $content; + unless (ref $content) { + $log->debug("Couldn't thaw value for $key."); + WebGUI::Error::InvalidObject->throw( + error => "Couldn't thaw value for $key." + ); + } return ${$content}; } @@ -216,25 +233,32 @@ sub mget { 1, { type => ARRAYREF }, ); - my @parsedNames = map { $self->parseKey($_) } @{ $names }; + my $log = $self->session->log; + my @keys = map { $self->parseKey($_) } @{ $names }; + $log->debug("Called mget() for keys (".join(", ",@keys).") on cache."); my %result; my $memcached = $self->getMemcached; - $memcached->mget_into_hashref(\@parsedNames, \%result); + $memcached->mget_into_hashref(\@keys, \%result); if ($memcached->errstr eq 'SYSTEM ERROR Unknown error: 0') { + $log->debug("Cannot connect to memcached server."); WebGUI::Error::Connection->throw( error => "Cannot connect to memcached server." ); } elsif ($memcached->errstr eq 'NO SERVERS DEFINED') { + $log->warn("No memcached servers specified in config file."); WebGUI::Error->throw( error => "No memcached servers specified in config file." ); } # no other useful status messages are returned my @values; - foreach my $name (@parsedNames) { - my $content = Storable::thaw($result{$name}); - next unless ref $content; + foreach my $key (@keys) { + my $content = Storable::thaw($result{$key}); + unless (ref $content) { + $log->debug("Cannot thaw key $key."); + next; + } push @values, ${$content}; } return \@values; @@ -259,7 +283,7 @@ sub new { 1, { isa => 'WebGUI::Session' }, ); - my ($class, $session) = @_; + $session->log->debug("Instanciated cache object."); my $config = $session->config; my $namespace = $config->getFilename; my $memcached = Memcached::libmemcached::memcached_create(); # no exception because always returns success @@ -351,22 +375,28 @@ sub set { { type => SCALAR }, { type => SCALAR | UNDEF, optional => 1, default=> 60 }, ); + my $log = $self->session->log; + my $key = $self->parseKey($name); + $log->debug("Called set() on cache key $key with $value as the value."); my $frozenValue = Storable::nfreeze(\(scalar $value)); # Storable doesn't like non-reference arguments, so we wrap it in a scalar ref. my $memcached = $self->getMemcached; - Memcached::libmemcached::memcached_set($memcached, $self->parseKey($name), $frozenValue, $ttl); + Memcached::libmemcached::memcached_set($memcached, $key, $frozenValue, $ttl); if ($memcached->errstr eq 'SYSTEM ERROR Unknown error: 0') { + $log->debug("Cannot connect to memcached server."); WebGUI::Error::Connection->throw( error => "Cannot connect to memcached server." ); } elsif ($memcached->errstr eq 'NO SERVERS DEFINED') { + $log->warn("No memcached servers specified in config file."); WebGUI::Error->throw( error => "No memcached servers specified in config file." ); } elsif ($memcached->errstr ne 'SUCCESS') { + $log->debug("Couldn't set $key to cache because ".$memcached->errstr); WebGUI::Error->throw( - error => "Couldn't set $name to cache because ".$memcached->errstr + error => "Couldn't set $key to cache because ".$memcached->errstr ); } return $value; @@ -381,13 +411,9 @@ Retrieves a document via HTTP and stores it in the cache and returns the content Throws WebGUI::Error::InvalidParam, WebGUI::Error::Connection, and WebGUI::Error. -=head3 name - -The name of the key to store the request under. - =head3 url -The URL of the document to retrieve. It must begin with the standard "http://". +The URL of the document to retrieve. It must begin with the standard "http://". This will be used as the key for this cache entry. =head3 ttl @@ -396,12 +422,13 @@ The time to live for this content. This is the amount of time (in seconds) that =cut sub setByHttp { - my ($self, $name, $url, $ttl) = validate_pos(@_, + my ($self, $url, $ttl) = validate_pos(@_, 1, - { type => SCALAR | ARRAYREF }, { type => SCALAR }, { type => SCALAR, optional => 1 }, ); + my $log = $self->session->log; + $log->debug("Called setByHttp() with URL $url."); my $userAgent = new LWP::UserAgent; $userAgent->env_proxy; $userAgent->agent("WebGUI/".$WebGUI::VERSION); @@ -411,13 +438,13 @@ sub setByHttp { my $response = $userAgent->request($request); if ($response->is_error) { - $self->session->log->error($url." could not be retrieved."); + $log->error("$url could not be retrieved."); WebGUI::Error::Connection->throw( error => "Couldn't fetch $url because ".$response->message, resource => $url, ); } - return $self->set($name, $response->decoded_content, $ttl); + return $self->set($url, $response->decoded_content, $ttl); } diff --git a/lib/WebGUI/Operation/Statistics.pm b/lib/WebGUI/Operation/Statistics.pm index 320b7924c..38c28cc1b 100644 --- a/lib/WebGUI/Operation/Statistics.pm +++ b/lib/WebGUI/Operation/Statistics.pm @@ -182,7 +182,7 @@ sub www_viewStatistics { my $cache = $session->cache; my $version = eval{$cache->get($url)}; if (not defined $version) { - $version = eval{$cache->setByHttp($url, $url, 43200)}; + $version = eval{$cache->setByHttp($url, 43200)}; } chomp $version; $output .= ''; diff --git a/t/Cache.t b/t/Cache.t index f5684b189..976e7727a 100644 --- a/t/Cache.t +++ b/t/Cache.t @@ -48,8 +48,8 @@ $cache->delete("Shawshank"); is(eval{$cache->get("Shawshank")}, undef, 'delete'); $cache->flush; is(eval{$cache->get(["andy", "dufresne"])}, undef, 'flush'); -$cache->setByHttp("google", "http://www.google.com/"); -cmp_ok($cache->get("google"), 'ne', '', 'setByHttp'); +$cache->setByHttp("http://www.google.com/"); +cmp_ok($cache->get("http://www.google.com/"), 'ne', '', 'setByHttp'); #----------------------------------------------------------------------------