philipnewcomer/persistent-transients

Persistent transients for WordPress

1.0.1 2017-01-25 20:31 UTC

This package is auto-updated.

Last update: 2024-03-21 19:09:50 UTC


README

This library exists to fill a very specific use case: when you need transients that are guaranteed to exist until their expiration timestamp.

In most cases, you should be using normal transients instead. Don't use this library unless you have a good reason to.

Background

WordPress transients are not guaranteed to persist until their expiration timestamp. There are a number of things, including core updates, or a full in-memory object cache, that can cause a transient to be deleted before it is due to expire.

However, sometimes you may need to store data that is transient, cannot be regenerated as you would typically do with a normal transient, and must be available up to the time when it is due to expire. An example of this use case is an email confirmation link, with a unique key generated by uniqid(). This data cannot be regenerated, and must be guaranteed to persist in the database until its expiration timestamp.

Using a custom post type or custom database table may be overkill for this type of thing, and that's where this library comes into play.

What This Does

Persistent Transients stores transient data in the WordPress options table. Expired transients are garbage collected once per day. Because it does not use the regular WordPress transients system, and is not stored in the in-memory object cache, these transients are not affected by any of the things that may cause a normal transient to expire prematurely.

Usage

Persistent Transients provides three drop-replacements for the regular WordPress transient functions: set, get, and delete.

PersistentTransients\set( $transient, $value, $expiration );
  • $transient (string) A unique identifier for the cached data.
  • $value (array|object) The data to save.
  • $expiration (integer) The number of seconds to keep the data.

Saves a persistent transient. Will overwrite an existing transient with the same name if it exists.

PersistentTransients\get( $transient );
  • $transient (string) A unique identifier for the cached data.

Returns the transient value, or false if it has expired or does not exist.

PersistentTransients\delete( $transient );
  • $transient (string) A unique identifier for the cached data.

Deletes a transient. Use if you want to remove a transient before its expiration.

Installation

This is a Composer package, not a traditional WordPress plugin. Install via Composer.