Using Flash RAM
There are several ways to interact with the ColdBox Flash RAM:
Using the
flash
scope object (Best Practice)Using the
persistVariables()
method from the super type and ColdBox ControllerUsing the
persistence
arguments in therelocate()
method from the super type and ColdBox Controller.
All of these methods interact with the Flash RAM object but the last two methods not only place variables in the temporary storage bin but actually serialize the data into the Flash RAM storage immediately. The first approach queues up the variables for serialization and at the end of a request it serializes the variables into the correct storage scope, thus saving precious serialization time. In the next section we will learn what all of this means.
Flash Scope Object
The flash
scope object is our best practice approach as it clearly demarcates the code that the developer is using the flash scope for persistence. Any flash scope must inherit from our AbstractFlashScope
and has access to several key methods that we will cover in this section. However, let's start with how the flash scope stores data:
The flash persistence methods are called for saving data, the data is stored in an internal temporary request bin and awaiting serialization and persistence either through relocation or termination of the request.
If the flash methods are called with immediate save arguments, then the data is immediately serialized and stored in the implementation's persistent storage.
If the flash's
saveFlash()
method is called then the data is immediately serialized and stored in the implementation's persistent storage.If the application relocates via relocate() or a request finalizes then if there is data in the request bin, it will be serialized and stored in the implementation's storage.
Caution By default the Flash RAM queues up serializations for better performance, but you can alter the behavior programmatically or via the configuration file.
Info If you use the
persistVariables()
method or any of the persistence arguments on therelocate()
method, those variables will be saved and persisted immediately.
To review the Flash Scope methods, please go to the API and look for the correct implementation or the AbstractFlashScope
.
Info Please note that the majority of a Flash scope methods return itself so you can concatenate method calls. Below are the main methods that you can use to interact with the Flash RAM object:
clear()
Clears the temporary storage bin
clearFlash()
Clears the persistence flash storage implementation
discard()
Discards all or some keys from flash storage. You can use this method to implement flows.
exists()
Checks if a key exists in the flash storage
get()
Get a value from flash storage and optionally pass a default value if it does not exist.
getKeys()
Get a list of all the objects in the temp flash scope.
getFlash()
Get a reference to the permanent storage implementation of the flash scope.
getScope()
Get the flash temp request storage used throughout a request until flashed at the end of a request.
isEmpty()
Check if the flash scope is empty or not.
keep()
Keep flash temp variable(s) alive for another relocation. Usually called from interceptors or event handlers to create conversations and flows of data from event to event.
persistRC()
Persist variable(s) from the ColdBox request collection into flash scope. Persist the entire request collection or limit the variables to be persisted by providing the keys in a list. "Include" will only try to persist request collection variables with keys in the list. "Exclude" will try to persist the entire request collection except for variables with keys in the list.
put()
The main method to place data into the flash scope. Optional arguments control whether to save the flash immediately, inflate to RC or PRC on the next request, and if the data should be auto-purged. You can also use the configuration settings to have a consistent flash experience, but you can most certainly override the defaults. By default all variables placed in flash RAM are automatically purged in the next request once they are inflated UNLESS you use the keep() methods in order to persist them longer or create flows. However, you can also use the autoPurge argument and set it to false so you can control when the variables will be removed from flash RAM. Basically a glorified ColdFusion scope that you can use.
putAll()
Pass an entire structure of name-value pairs into the flash scope (similar to the put() method).
remove()
Remove an object from the temporary flash scope so it will not be included when the flash scope is serialized. To remove a key from the flash scope and make sure your changes are effective in the persistence storage immediately, use the saveNow argument.
removeFlash()
Remove the entire flash storage. We recommend using the clearing methods instead.
saveFlash()
Save the flash storage immediately. This process looks at the temporary request flash scope, serializes it if needed, and persists to the correct flash storage on demand.
Info We would advice to not overuse this method as some storage scopes might have delays and serializations
size()
Get the number of the items in flash scope.
More Examples
Last updated