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 Controller
Using the persistence
arguments in the setNextEvent()
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 actualy 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.
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 setNextEvent() 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 thesetNextEvent()
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:
Clears the temporary storage bin
Clears the persistence flash storage implementation
Discards all or some keys from flash storage. You can use this method to implement flows.
Checks if a key exists in the flash storage
Get's a value from flash storage and you can even pass a default value if it does not exist.
Get a list of all the objects in the temp flash scope.
Get a reference to the permanent storage implementation of the flash scope.
Get the flash temp request storage used throughout a request until flashed at the end of a request.
Check if the flash scope is empty or not
Keep all or a single flash temp variable alive for another relocation. Usually called from interceptors or event handlers to create conversations and flows of data from event to event.
Persist keys from the ColdBox request collection into flash scope. If using exclude, then it will try to persist the entire request collection but excluding certain keys. Including will only include the keys passed from the request collection.
This is the main method to place data into the flash scope. You can optionally use the arguments to save the flash immediately, inflate to RC or PRC on the next request and if the data should be auto purged for you. 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.
Same as the put() method but instead you pass in an entire structure of name-value pairs into the flash scope.
Remove an object from the temporary flash scope so when the flash scope is serialized it will not be serialized. If you would like to remove a key from the flash scope and make sure your changes are reflected in the persistence storage immediately, use the saveNow argument.
Remove the entire flash storage. We recommend using the clearing methods instead.
Save the flash storage immediately. This process looks at the temporary request flash scope and serializes if it needs to 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
Get the number of the items in flash scope