GFPDF\Plugins\PdfForGravityView\Pdf\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/Cache.php:34
Details
- Kind:
class - Namespace:
GFPDF\Plugins\PdfForGravityView\Pdf
Methods
| Method | Description |
|---|---|
init() | Bind the cleanup + 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). |
run_gc() | 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 cleanup + invalidation hooks.
Returns
void
Since: 2.0
Source: src/Pdf/Cache.php:50
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
| Name | Type | Default | Description |
|---|---|---|---|
$view | View |
Returns
bool
Since: 2.0
Source: src/Pdf/Cache.php:91
ttl()
public function ttl( View $view ): int
The read-back expiry / GC threshold, tracking GravityView's Entry Cache Duration (per-View aware).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$view | View |
Returns
int— seconds
Since: 2.0
Source: src/Pdf/Cache.php:122
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
| Name | Type | Default | Description |
|---|---|---|---|
$view | View | ||
$entry | Entry | Entry_Collection | ||
$pdf_settings | array | The context-scoped render recipe (single vs directory). | |
$context | string | RenderContext::SINGLE or RenderContext::DIRECTORY. | |
$view_fields | array | The resolved field set, as Renderer::get_view_fields() built it. |
Returns
string— 40-char sha1
Since: 2.0
Source: src/Pdf/Cache.php:148
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
| Name | Type | Default | Description |
|---|---|---|---|
$view | View | ||
$key | string | ||
$ttl | int | null | null | The 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/Cache.php:201
put()
public function put( View $view, string $key, string $source_path )
Copy a freshly-rendered PDF into the cache (atomic rename).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$view | View | ||
$key | string | ||
$source_path | string |
Returns
string|null— The cache path, or null on failure.
Since: 2.0
Source: src/Pdf/Cache.php:231
lock()
public function lock( int $view_id, string $key, bool $blocking )
Acquire the per-key single-flight lock.
$blocking is a bounded poll, 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
| Name | Type | Default | Description |
|---|---|---|---|
$view_id | int | ||
$key | string | ||
$blocking | bool | false → LOCK_NB (return immediately on contention); true → poll up to gfpdf_gv_cache_lock_wait_timeout seconds (default 30) before giving up. |
Returns
resource|false— The lock handle, or false if it could not be acquired.
Since: 2.0
Source: src/Pdf/Cache.php:271
unlock()
public function unlock( $handle ): void
Release a lock handle acquired via lock().
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$handle | resource | false |
Returns
void
Since: 2.0
Source: src/Pdf/Cache.php:305
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
| Name | Type | Default | Description |
|---|---|---|---|
$view_id | int | ||
$key | string | ||
$ext | string | '.pdf' |
Returns
string
Since: 2.0
Source: src/Pdf/Cache.php:325
run_gc()
public function run_gc(): void
Time-based garbage collection — reap PDFs past their View's TTL, orphaned locks, and emptied dirs.
Returns
void
Since: 2.0
Source: src/Pdf/Cache.php:341
invalidate_form()
public function invalidate_form( $form_id ): void
Remove every cached PDF for the Views connected to a form.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$form_id | int | string |
Returns
void
Since: 2.0
Source: src/Pdf/Cache.php:457
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
| Name | Type | Default | Description |
|---|---|---|---|
$subject | mixed |
Returns
void
Since: 2.0
Source: src/Pdf/Cache.php:481