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

Performance: Add temporary PDF cache #1556

Merged
merged 1 commit into from
Jul 31, 2024
Merged

Conversation

jakejackson1
Copy link
Member

@jakejackson1 jakejackson1 commented Jul 23, 2024

Description

Automatically save PDFs to disk for up to 1 hour, and return the cached copy on subsequent requests (if it exists). This is a smart cache that auto-detects if the form, entry, or PDF settings have been modified and will generate a new PDF if that's the case.

Manually flushing the cache will be handled in a separate PR, alongside #1527

This PR standardizes the PDF generation system so all PDF generation processes (view/download/notification/preview/bulk generate/API) flow through the same set of functions/methods to create the documents and save them to the temporary cache:

1. Model_PDF::generate_and_save_pdf()
2. Model_PDF::process_and_save_pdf()
3. Helper_PDF::generate()
4. Helper_PDF::save_pdf()

The calling function/method to Model_PDF::generate_and_save_pdf() will then determine what to do with the saved PDF. When viewing/downloading documents, the PDF will be read from disk and streamed to the browser with appropriate browser caching headers. When sending notifications, the absolute path to the PDF is passed to wp_mail() as an attachment, when using the API the absolute path is returned etc.

The View_PDF::generate_pdf() workflow to view/download PDFs is deprecated and should no longer be used directly, but for backwards compatibility will still work correctly.

Testing instructions

This is a critical application path and should be thoroughly tested under all conditions to verify everything works as expected:

  • Viewing and Downloading PDF
  • Receive cached PDF when viewing and downloading PDF again
  • Updating form and viewing/downloading PDF for fresh copy
  • Updating entry and viewing/downloading PDF for fresh copy
  • Updating form PDF settings and viewing/downloading PDF for fresh copy
  • ?data=1 helper param
  • ?html=1 helper param
  • ?print=1 helper param
  • All view/download caching and URL options (above) with deprecated legacy PDF endpoint (from v3.0)
  • Set up multiple PDFs on a form and attach to multiple notifications. Verify cached copy is used when attaching the same PDF to 2+ notifications
  • Resend Notification feature uses cached PDFs when running multiple times
  • Resend Notification feature generates new PDFs if the form/entry/PDF settings are modified
  • New PDF generated after editing an entry via GravityView / Gravity Wiz / Gravity Flow
  • The gfpdf_post_save_pdf hook works as expected on form submission
  • The gfpdf_override_pdf_bypass hook bypasses the cache
  • The legacy save option works as expected on form submission
  • GPDFAPI::create_pdf() API leaves the cache intact when running
  • GPDFAPI::create_pdf() API uses cached copy on subsequent requests
  • Developer Toolkit PDFs view/download PDF
  • Developer Toolkit PDFs support view/download caching and URL options (above)
  • Set up multiple Developer Toolkit PDFs on a form and attach to multiple notifications. Verify cached copy is used when attaching the same PDF to 2+ notifications
  • Developer Toolkit Legacy PDFs (with advanced_template option on): view/download + notifications (as above)
  • PDF Background Processing queue with standard and Developer Toolkit PDFs. Verify cached copy is used when attaching the same PDF to 2+ notifications
  • Manually calling View_PDF::generate_pdf() will view/download PDF as expected (including all URL params)
  • Legacy gfpdfe_pdf_output_type filter works
  • WP External Links plugin conflict: WP External Links Plugin Conflict #386
  • Weglot plugin conflict: Better compatibility with Weglot #1505
  • Siteground Optimizer minification conflict: SG Optimizer Minify the HTML Output Breaks PDF #863
  • Auto-purge PDF if associated template/config is updated
  • Background Processing Conflict with WooCommerce GF Add-on #1403
  • Partial Entries Add-on: Notification with PDF attached is cached #1370
  • Gravity Perks Conditional Logic Date support on product fields
  • Gravity Perks Populate Anything support (live merge tags)
  • PDF Cached auto-cleaned up every hour by WordPress cron task
  • Async email sending e.g. WP Offload SES
  • Core Booster - Entry Notes
  • Gravity Perks Nested Forms - changes to nested form entries
  • PDF for GravityView works as expected
  • Enhanced Downloader
  • Bulk Generator works as expected
  • Previewer works as expected
  • PDF to Images works as expected

Checklist:

  • I've tested the code.
  • My code is easy to read, follow, and understand
  • My code follows the accessibility standards.
  • My code has proper inline documentation / docblocks.

Additional Comments

@jakejackson1 jakejackson1 mentioned this pull request Jul 23, 2024
4 tasks
Copy link

codecov bot commented Jul 23, 2024

Codecov Report

Attention: Patch coverage is 68.51312% with 108 lines in your changes missing coverage. Please review.

Project coverage is 76.12%. Comparing base (73750a2) to head (f31eca4).
Report is 3 commits behind head on development.

Files Patch % Lines
src/Model/Model_PDF.php 67.21% 40 Missing ⚠️
src/Controller/Controller_PDF.php 59.55% 36 Missing ⚠️
src/Statics/Debug.php 0.00% 12 Missing ⚠️
src/Statics/Queue_Callbacks.php 27.27% 8 Missing ⚠️
src/View/View_PDF.php 75.00% 6 Missing ⚠️
src/Controller/Controller_Pdf_Queue.php 75.00% 2 Missing ⚠️
src/Statics/Cache.php 96.66% 2 Missing ⚠️
src/Helper/Helper_Misc.php 0.00% 1 Missing ⚠️
src/Model/Model_Shortcodes.php 0.00% 1 Missing ⚠️
Additional details and impacted files
@@               Coverage Diff               @@
##           development    #1556      +/-   ##
===============================================
+ Coverage        76.07%   76.12%   +0.04%     
===============================================
  Files              248      250       +2     
  Lines            12993    13127     +134     
  Branches           370      370              
===============================================
+ Hits              9885     9993     +108     
- Misses            3100     3126      +26     
  Partials             8        8              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jakejackson1 jakejackson1 linked an issue Jul 23, 2024 that may be closed by this pull request
@jakejackson1 jakejackson1 force-pushed the pdf-tmp-cache branch 3 times, most recently from 2038e4e to 20f25df Compare July 23, 2024 06:31
@jakejackson1

This comment was marked as outdated.

@jakejackson1 jakejackson1 linked an issue Jul 26, 2024 that may be closed by this pull request
@jakejackson1
Copy link
Member Author

Core Booster support added https://github.com/GravityPDF/gravity-pdf-core-booster/pull/95

@jakejackson1
Copy link
Member Author

PDF for GravityView is not affected by the change, but doesn't currently make use of the temporary cache. Have opened issue about adding support https://github.com/GravityPDF/pdf-for-gravityview/issues/50

@jakejackson1
Copy link
Member Author

Enhanced Download support added https://github.com/GravityPDF/gravity-pdf-enhanced-download/pull/11

@jakejackson1
Copy link
Member Author

Bulk Generator is not affected by the change, but doesn't currently make use of the temporary cache. Have opened issue about adding support https://github.com/GravityPDF/gravity-pdf-bulk-generator/issues/184

@jakejackson1
Copy link
Member Author

Previewer not affected by the change, but doesn't make use of temporary cache. Have opened issue about it https://github.com/GravityPDF/gravity-pdf-previewer/issues/357

@jakejackson1 jakejackson1 linked an issue Jul 29, 2024 that may be closed by this pull request
@jakejackson1 jakejackson1 force-pushed the pdf-tmp-cache branch 2 times, most recently from 04741f0 to 2ea9244 Compare July 30, 2024 00:18
@jakejackson1
Copy link
Member Author

PDF to image compatibility added https://github.com/GravityPDF/gravity-pdf-to-image/pull/11 (still maintains independent image cache; a separate issue was created to combine the two).

@jakejackson1 jakejackson1 merged commit e68cde2 into development Jul 31, 2024
12 of 13 checks passed
@jakejackson1 jakejackson1 deleted the pdf-tmp-cache branch July 31, 2024 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant