Skip to content

Commit a2f722f

Browse files
committed
Cache results
1 parent 44a729c commit a2f722f

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

lib/VWF/Display.pm

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ no warnings qw(experimental::signatures);
2020
use Config::Abstraction;
2121
use CGI::Info;
2222
use Data::Dumper;
23+
use Digest::MD5 qw(md5_hex);
2324
use Digest::SHA qw(sha256_hex);
2425
use File::Spec;
2526
use Object::Configure;
@@ -56,7 +57,6 @@ my %blacklist = (
5657
);
5758

5859
our $sm;
59-
our $smcache;
6060

6161
# Main display handler for generating web pages using Template Toolkit
6262
# Handles security, throttling, localization, and template selection
@@ -201,11 +201,11 @@ sub new
201201

202202
# Social media integration
203203
if(my $twitter = $config->{'twitter'}) {
204-
$smcache ||= create_memory_cache(config => $config, logger => $params->{'logger'}, namespace => 'HTML::SocialMedia');
204+
my $smcache = create_memory_cache(config => $config, logger => $params->{'logger'}, namespace => 'HTML::SocialMedia');
205205
$sm ||= HTML::SocialMedia->new({ twitter => $twitter, cache => $smcache, lingua => $params->{lingua}, logger => $params->{logger} });
206206
$self->{'_social_media'}->{'twitter_tweet_button'} = $sm->as_string(twitter_tweet_button => 1);
207207
} elsif(!defined($sm)) {
208-
$smcache = create_memory_cache(config => $config, logger => $params->{'logger'}, namespace => 'HTML::SocialMedia');
208+
my $smcache = create_memory_cache(config => $config, logger => $params->{'logger'}, namespace => 'HTML::SocialMedia');
209209
$sm = HTML::SocialMedia->new({ cache => $smcache, lingua => $params->{lingua}, logger => $params->{logger} });
210210
}
211211
$self->{'_social_media'}->{'facebook_share_button'} = $sm->as_string(facebook_share_button => 1);
@@ -311,6 +311,16 @@ sub as_string {
311311
}
312312
}
313313

314+
my($cache, $key);
315+
316+
if(!$args->{itemsincart}) {
317+
$cache = create_memory_cache(config => $self->{config}, logger => $self->{'logger'}, namespace => ref($self));
318+
$key = cache_key_from_hashref($args);
319+
if(my $rc = $cache->get($key)) {
320+
return $rc;
321+
}
322+
}
323+
314324
# my $html = $self->html($args);
315325
# unless($html) {
316326
# return;
@@ -319,7 +329,15 @@ sub as_string {
319329

320330
# Build the HTTP response
321331
my $rc = $self->http($args);
322-
return $rc =~ /^Location:\s/ms ? $rc : $rc . $self->html($args);
332+
if($rc =~ /^Location:\s/ms) {
333+
return $rc;
334+
}
335+
$rc .= $self->html($args);
336+
if($cache) {
337+
$self->{cache_duration} ||= '5 minutes';
338+
$cache->set($key, $rc, $self->{cache_duration});
339+
}
340+
return $rc;
323341
}
324342

325343
# Determine the path to the correct template file based on various criteria such as language settings, browser type, and module path
@@ -632,4 +650,18 @@ sub _generate_csrf_token($self) {
632650
return "$token_data:$signature";
633651
}
634652

653+
sub cache_key_from_hashref {
654+
my $hashref = $_[0];
655+
656+
# Use Data::Dumper with sorted keys for consistent output
657+
local $Data::Dumper::Sortkeys = 1;
658+
local $Data::Dumper::Terse = 1;
659+
local $Data::Dumper::Indent = 0;
660+
661+
my $dumped = Dumper($hashref);
662+
663+
# Create an MD5 hash for a compact key
664+
return md5_hex($dumped);
665+
}
666+
635667
1;

0 commit comments

Comments
 (0)