@@ -20,6 +20,7 @@ no warnings qw(experimental::signatures);
2020use Config::Abstraction;
2121use CGI::Info;
2222use Data::Dumper;
23+ use Digest::MD5 qw( md5_hex) ;
2324use Digest::SHA qw( sha256_hex) ;
2425use File::Spec;
2526use Object::Configure;
@@ -56,7 +57,6 @@ my %blacklist = (
5657);
5758
5859our $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+
6356671;
0 commit comments