Skip to main content

GFPDF\Plugins\PdfForGravityView\Pdf\Caching\Cache

Server-side render cache for directory & single PDFs.

The cache filename is a hash of everything that affects the rendered output (form(s), View config incl. the union map, form definition, entry values, viewer id + roles, locale, plugin version; directory request params only when opted in, via the \RequestParamsCacheKey seam listener). Any change to a keyed input mints a new filename, so a stale file is never requested — it just becomes GC garbage. A per-View TTL (tracking GravityView's own Entry Cache Duration) backstops the dynamic content the key can't observe (merge tags, calculations, live values). Files live under get_temp_dir(), served only via the existing signed-URL + can_render gate, hardened with .htaccess + index.html (\TempStorage, shared with the bulk PDF export).

Since: 2.0

Source: src/Pdf/Caching/Cache.php:36

Details

  • Kind: class
  • Namespace: GFPDF\Plugins\PdfForGravityView\Pdf\Caching

Methods

MethodDescription
init()Bind the invalidation hooks.
is_enabled()Whether the PDF cache should be used for this View — tracks GravityView's caching gate.
ttl()The read-back expiry / GC threshold, tracking GravityView's Entry Cache Duration (per-View aware).
key()Build the cache key — a hash over everything that affects the output and is observable at key time.
get()Return the cached PDF path if a fresh (within-TTL) render exists, else null.
put()Copy a freshly-rendered PDF into the cache (atomic rename).
lock()Acquire the per-key single-flight lock.
unlock()Release a lock handle acquired via lock().
file_path()Absolute path to a cache artefact for a key (does not create anything).
sweep()Time-based garbage collection — reap PDFs past their View's TTL, orphaned locks, and emptied dirs.
invalidate_form()Remove every cached PDF for the Views connected to a form.
invalidate_from_hook()Resolve the changed form from whatever first argument an entry-change hook passes — a GF form array,

Method Reference

init()

public function init(): void

Bind the invalidation hooks.

Returns

  • void

Since: 2.0

Source: src/Pdf/Caching/Cache.php:72

is_enabled()

public function is_enabled( View $view ): bool

Whether the PDF cache should be used for this View — tracks GravityView's caching gate.

Parameters

NameTypeDefaultDescription
$viewView

Returns

  • bool

Since: 2.0

Source: src/Pdf/Caching/Cache.php:110

ttl()

public function ttl( View $view ): int

The read-back expiry / GC threshold, tracking GravityView's Entry Cache Duration (per-View aware).

Parameters

NameTypeDefaultDescription
$viewView

Returns

  • int — seconds

Since: 2.0

Source: src/Pdf/Caching/Cache.php:141

key()

public function key( View $view, $entry, array $pdf_settings, string $context, array $view_fields ): string

Build the cache key — a hash over everything that affects the output and is observable at key time.

Parameters

NameTypeDefaultDescription
$viewView
$entryEntry | Entry_Collection
$pdf_settingsarrayThe context-scoped render recipe (single vs directory).
$contextstringRenderContext::SINGLE or RenderContext::DIRECTORY.
$view_fieldsarrayThe resolved field set, as Renderer::get_view_fields() built it.

Returns

  • string — 40-char sha1

Since: 2.0

Source: src/Pdf/Caching/Cache.php:167

get()

public function get( View $view, string $key, ?int $ttl=null )

Return the cached PDF path if a fresh (within-TTL) render exists, else null.

Parameters

NameTypeDefaultDescription
$viewView
$keystring
$ttlint | nullnullThe freshness threshold, resolved once by the caller and threaded in so the double-checked-lock miss path doesn't re-read the settings + filter on the relock. Null falls back to resolving it here (every other caller).

Returns

  • string | null

Since: 2.0

Source: src/Pdf/Caching/Cache.php:220

put()

public function put( View $view, string $key, string $source_path )

Copy a freshly-rendered PDF into the cache (atomic rename).

Parameters

NameTypeDefaultDescription
$viewView
$keystring
$source_pathstring

Returns

  • string | null — The cache path, or null on failure.

Since: 2.0

Source: src/Pdf/Caching/Cache.php:250

lock()

public function lock( int $view_id, string $key, bool $blocking, string $context=RenderContext::DIRECTORY )

Acquire the per-key single-flight lock.

$blocking is a bounded poll \flock_path, not a true flock(LOCK_EX) block — nothing interrupts a blocking flock(), so a stuck holder would pile up workers with no way out. A duplicate unprotected render is far cheaper than starving the worker pool.

Parameters

NameTypeDefaultDescription
$view_idint
$keystring
$blockingboolfalse → LOCK_NB (return immediately on contention); true → poll up to \lock_wait seconds before giving up.
$contextstringRenderContext::DIRECTORYRenderContext::SINGLE or RenderContext::DIRECTORY — the two wait very differently. Defaults to the directory (longer) wait when unstated.

Returns

  • resource | false — The lock handle, or false if it could not be acquired.

Since: 2.0

Source: src/Pdf/Caching/Cache.php:292

unlock()

public function unlock( $handle ): void

Release a lock handle acquired via lock().

Parameters

NameTypeDefaultDescription
$handleresource | false

Returns

  • void

Since: 2.0

Source: src/Pdf/Caching/Cache.php:356

file_path()

public function file_path( int $view_id, string $key, string $ext='.pdf' ): string

Absolute path to a cache artefact for a key (does not create anything).

Parameters

NameTypeDefaultDescription
$view_idint
$keystring
$extstring'.pdf'

Returns

  • string

Since: 2.0

Source: src/Pdf/Caching/Cache.php:376

sweep()

public function sweep( ?string $cursor=null, ?callable $out_of_time=null ): ?string

Time-based garbage collection — reap PDFs past their View's TTL, orphaned locks, and emptied dirs.

Walks from $cursor and wraps around, so a sweep the deadline cuts short resumes where it stopped rather than restarting at the first View dir — without which the dirs behind a busy one are never reached. The caller owns the clock and the cursor; \CacheCleanup runs this as a background job.

Parameters

NameTypeDefaultDescription
$cursorstring | nullnullA cursor from an earlier sweep, or null to start at the top.
$out_of_timecallable | nullnullfn(): bool — true to stop and hand back a cursor. Null never stops.

Returns

  • string | null — The cursor to resume from, or null once the whole tree has been swept.

Since: 2.0

Source: src/Pdf/Caching/Cache.php:399

invalidate_form()

public function invalidate_form( $form_id ): void

Remove every cached PDF for the Views connected to a form.

Memoised per request: several of the entry-change hooks fire together for one save (a GravityView Edit Entry runs gform_after_update_entry and gravityview/edit_entry/after_update), and a bulk approval fires one per entry. The purge is form-wide and entry-agnostic, so the repeats resolve the same Views and delete the same (already-gone) directories.

Parameters

NameTypeDefaultDescription
$form_idint | string

Returns

  • void

Since: 2.0

Source: src/Pdf/Caching/Cache.php:519

invalidate_from_hook()

public function invalidate_from_hook( $subject ): void

Resolve the changed form from whatever first argument an entry-change hook passes — a GF form array,

a GF entry array, or a bare entry id. See init() for the per-hook signatures this covers.

Parameters

NameTypeDefaultDescription
$subjectmixed

Returns

  • void

Since: 2.0

Source: src/Pdf/Caching/Cache.php:551