Plesk key-value storage is used for storing user settings or secret keys. To access the Plesk key-value storage, use the pm_Settings class. Keys and values are stored in the context of an extension.
If you need to assign a variable, use the method
Note: Before Plesk 17.0, this statement would result in an exception to be thrown. Beginning with Plesk 17.0, this statement will cause the corresponding element to be removed from the key-value storage.
Now you can retrieve the value of this variable by using the method
$authToken = pm_Settings::get('authToken');
An additional optional parameter can be used to specify a default value to be returned in case the corresponding key is not found:
$message = pm_Settings::get('message', 'default message');
To obtain access to them in some places of code, initialize the context first:
It is mandatory to initialize the context in the following places in code:
- Scheduled tasks.
- Post- and Pre- installation scripts.
- Event handlers.
The key-value storage allows for storing and retrieving data in encrypted form.
Note: It is strongly advised to use this particular approach when working with sensitive data, such as user credentials, etc.
- setEncrypted() method stores an encrypted value in the key-value storage
- getDecrypted() method retrieves an encrypted value from the key-value storage.
$secretValue = pm_Settings::getDecrypted('secretValue');
Starting with Plesk 12.5, it is possible to clear the extension settings using the clean() method. You can use it to add the Reset to default button to the UI of your extension.
The string length of the stored value is limited to 2000 characters.
Avoid storing values that are larger than 2000 characters in length.
Note: Before Plesk 17.0, only the first 2000 characters will be saved, the remainder will be truncated. Beginning with Plesk 17.0, the value will not be saved at all, an exception will be thrown.
Avoid storing values that can tend to grow in length, for example, a list of domains as a JSON string.
- Check lengths of values stored by the extension.
- For the stored values whose length is near 2000 characters, check how the extension validates the value’s string length.
- Test extension on multiple domains or in similar conditions (multiplying entities), depending on the extension’s functionality.
The data saved in the key-value storage is stored as plain text and is not encrypted.
Avoid storing sensitive information.
Plesk key-value storage is backed up as part of the server back up. It is therefore possible that restoring a Plesk server from a backup may revert some or all changes done to the key-value storage data by the extension.
Check that the extension behaves correctly following a server restore. For example, create a backup, remove some extension-related entries from the key-value storage, restore the server from the backup, make sure the extension functions as expected.
To ensure there are no object collisions, use Plesk object GUIDs (or IDs, when GUIDs are unavailable) to uniquely identify domains, users, etc. This will help safely deal with situations when an object is removed from system by one user and then created with the same name by a different user.
Check that GUIDs are used as object identifiers.
Key-value storage is automatically cleared when extension is removed.
You don’t need to do it yourself in