Applications often have a set of data that is accessed globally; i.e. data that is the same for any user, which is the result of an initialization process. Once calculated, it is useful to have this data accessible and updated only when the application requires it.
A possible use scenario is one in which the data does not change too often (it can be obtained from the database or by calling a service), and accessing the database or retrieving the data by calling a web service is too expensive.
The Web Session could be a solution, but it is defined for each user of the application. On the contrary, the Cache API is defined and accessible for all the users of the application.
The Cache API allows storing global data of an application, access the data and invalidate it when necessary. The Cache can be local to the application server, or it can be a distributed cache depending on the Cache Provider property.
Cache is a data type and it exposes different methods to manage the application server or the distributed cache. Below are the methods exposed:
|Server Cache static methods
||Retrieves a cache instance or creates a new one if it does not exist.
||Clears all the caches, both the ones created by the user and the others (the Smart Devices cache and the Database Cache (*)).
|Server Cache non static methods
|&Cache.Set(Character: Key, Character: Value, [Numeric: DurationMinutes])
Stores the value in the cache under the specified key. If the duration value is 0 (the default), the item never expires (although it may be deleted from the server to make room for other items).
The function can be called in two ways:
&Cache.Set("key", "value", 1)
In the second case, the duration will be set to the default value 0 and the item won't expire.
||If the item is found it returns the item that was previously stored under the key. Otherwise, an empty string is returned.
||If the item is found it returns true; otherwise, false is returned.
||Removes an item from the cache.
||Clears the cache, removing all the items from it.
Consider an application which shows the value of the currency.
&CacheName = "CurrencyValueCache"
&Cache=Cache.GetCache(&CacheName) //&Cache is Cache data type
//Here call a service which returns the currency value and loads the &value variable.
In GeneXus, there is a caching mechanism, explained in detail in Caching in GeneXus applications. For the Smart Devices caching as well as the ResultSet caching there are also some useful methods to manage the cache expiration.
In some cases, it may be necessary to forcefully clear the cache (for example, if you need the SQL query to be re-executed and not to retrieve the data from the database cache). This is especially useful when using any distributed cache mechanism.
In order to reset the cache, you can use the Cache.SmartDevices.Clear method and the Cache.Database.Clear method for SD and ResultSet caching respectively.
||Clears the smart devices cache, removing all the items from it.
Removes the SD cache of the indicated Key. The Key is a table name.
||Clears the database cache, removing all the items from it.
Download the sample from Cache API sample.
Keyword : global session