Skip to content

Commit

Permalink
fix: cache /api/v0/preferences and attribute_groups (#10341)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanegigandet authored May 23, 2024
1 parent 25cdde6 commit 8134720
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
4 changes: 4 additions & 0 deletions html/js/product-preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ function display_user_product_preferences(target_selected, target_selection_form
attribute_groups = data;
display_user_product_preferences(target_selected, target_selection_form, change);
});

return;
}

if (!preferences) {
Expand All @@ -179,6 +181,8 @@ function display_user_product_preferences(target_selected, target_selection_form
preferences = data;
display_user_product_preferences(target_selected, target_selection_form, change);
});

return;
}

if (attribute_groups && preferences && !displayed_user_product_preferences) {
Expand Down
10 changes: 7 additions & 3 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ BEGIN {

use vars @EXPORT_OK;

use ProductOpener::HTTP qw(write_cors_headers);
use ProductOpener::HTTP qw(write_cors_headers set_http_response_header write_http_response_headers);
use ProductOpener::Store qw(get_string_id_for_lang retrieve);
use ProductOpener::Config qw(:all);
use ProductOpener::Paths qw/%BASE_DIRS/;
Expand Down Expand Up @@ -10193,6 +10193,8 @@ sub display_preferences_api ($request_ref, $target_lc) {
push @{$request_ref->{structured_response}}, $preference_ref;
}

set_http_response_header($request_ref, "Cache-Control", "public, max-age=86400");

display_structured_response($request_ref);

return;
Expand Down Expand Up @@ -10240,6 +10242,8 @@ sub display_attribute_groups_api ($request_ref, $target_lc) {

$request_ref->{structured_response} = $attribute_groups_ref;

set_http_response_header($request_ref, "Cache-Control", "public, max-age=86400");

display_structured_response($request_ref);

return;
Expand Down Expand Up @@ -10539,6 +10543,8 @@ sub display_product_history ($request_ref, $code, $product_ref) {
sub display_structured_response ($request_ref) {
# directly serve structured data from $request_ref->{structured_response}

write_http_response_headers($request_ref);

$log->debug(
"Displaying structured response",
{
Expand Down Expand Up @@ -10619,9 +10625,7 @@ sub display_structured_response ($request_ref) {
. $data . ");";
}
else {
$log->warning("XXXXXXXXXXXXXXXXXXXXXX");
write_cors_headers();
$log->warning("YYYYYYYYYYYYYYYY");
print header(
-status => $status_code,
-type => 'application/json',
Expand Down
46 changes: 45 additions & 1 deletion lib/ProductOpener/HTTP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ BEGIN {
@EXPORT_OK = qw(
&get_cors_headers
&write_cors_headers
&set_http_response_header
&write_http_response_headers
); #the fucntions which are called outside this file
%EXPORT_TAGS = (all => [@EXPORT_OK]);
}
Expand Down Expand Up @@ -147,7 +149,49 @@ sub write_cors_headers ($allow_credentials = 0, $sub_domain_only = 0) {
my $headers_ref = get_cors_headers($allow_credentials, $sub_domain_only);
my $r = Apache2::RequestUtil->request();
# write them
while (my ($header_name, $header_value) = each %$headers_ref) {
foreach my $header_name (sort keys %$headers_ref) {
my $header_value = $headers_ref->{$header_name};
$r->err_headers_out->set($header_name, $header_value);
}
return;
}

=head2 set_http_response_header($request_ref, $header_name, $header_value)
This function sets a header in the response.
=head3 Parameters
=head4 $request_ref - Reference to the request object.
=head4 $header_name - Name of the header.
=head4 $header_value - Value of the header.
=cut

sub set_http_response_header($request_ref, $header_name, $header_value) {
not defined $request_ref->{http_response_headers} and $request_ref->{http_response_headers} = {};
$request_ref->{http_response_headers}{$header_name} = $header_value;
return;
}

=head2 write_http_response_headers($http_response_headers_ref)
This function writes the headers in the response.
=head3 Parameters
=head4 $http_response_headers_ref - Reference to a hash with headers.
=cut

sub write_http_response_headers($request_ref) {
my $http_response_headers_ref = $request_ref->{http_response_headers};
return unless $http_response_headers_ref;
my $r = Apache2::RequestUtil->request();
foreach my $header_name (sort keys %$http_response_headers_ref) {
my $header_value = $http_response_headers_ref->{$header_name};
$r->err_headers_out->set($header_name, $header_value);
}
return;
Expand Down

0 comments on commit 8134720

Please sign in to comment.