Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cache /api/v0/preferences and attribute_groups #10341

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading