How to use the Elxis file cache library on your own projects and for custom purposes.
Elxis handles automatically file cache on modules where the cache parameter has been set to 1 (enabled). Module caching leads to saving the HTML output of the module into the repository/cache/ folder for later usage. But what about other type of extensions or custom actions inside a module or a plugin? With the elxisCache library you can save into cache and retrieve from it easily any data block your application may need. In this article we will show you how to work with the elxisCache library.
Workcase scenario
Let's say you are up to develop a module or a plugin that needs to connect to a remote host and retrieve some data (an XML file for example). If these data dont change frequently is it very bad to connect on the remote host on each user request (click). Remote connections are slow. We need to download this file once, save it in cache for some time and when a time limit has reached refresh the cache by fetching a new version of the file from the remote host. This is a typical example in which cache should always be used. You can also use cache (or Elxis APC) on any task even if no remote connection takes place. Take CPU expensive tasks as example. This depends on your project of course and what you want to do.
Get cache instance
To get/initiate Elxis cache library is simple, just call the getCache method of Elxis factory.
$eCache = eFactory::getCache();
elxisCache methods
Public methods of the elxisCache library.
begin
Starts a new caching process. Returns cached item state (integer).
public functionbegin($element, $id, $group='', $cachetime=0, $mlang=true, $force=false, $extension='php')
(string) element A name for the cached file specific to the element it contains data for. It can be any string.
(string) id An ID string to separate the file from others of the same element. It can be any string.
(string) group The name of the folder inside the repository/cache/ directory in which you want to save the cached file. Leave empty to save it inside cache folder root. If the folder doesn't exist elxisCache will try to create it.
(int) cachetime The life time in seconds of the cached file. After this time the file will be refreshed with new data. If 0 the global cache time from Elxis configuration will be used. To make your script faster set properly this option regarding what your application does. If, for example, you get a remote XML file that is being refreshed once per day set the cachetime to 86400 (1 day).
(boolean) mlang If true Elxis Cache will save a separate instance of your data for each language. Enabled it only on multi-lingual data.
(boolean) force If true, cache will be used even if cache is disabled in Elxis configuration. If false, cache will be used only if it is enabled in Elxis configuration.
(string) extension The extension (file type) of the saved cached file. If you plan to save raw PHP code (eg. an array containg some data) use php. For the remote XML workcase we will use xml. Allowed extension types: php, xml, txt, html, css, js, csv.
Method begin may return the following result (integer):
2 The file does not exist in cache or exists but has expired and needs to be refreshed.
Usage example
Cache an XML feed from CNN web site in our test application and save it in cache for 28800 seconds (8 hours) even if cache is disabled in Elxis configuration ($force = true). Disable multilingual caching ($mlang = false) as this feed is always in English.
If state value is 1 everything is OK and we can get the cached item from cache. If state is 2 then item has expired and we need to refresh it. On any other value of state cache can not be used and we should continue without it. For our example the cached item will be saved like that:
repository/cache/test_cnnfeed.xml
After the begin method if state is 1, you can get the data in 2 ways, using fetch or fetchContents methods.
fetch
Includes the cached file. Tip: Use it only if the cached data dont need any manupulation before echoing to the browser or if your cached file is php.
public functionfetch()
fetchContents
Get the cached file contents as a string.
public functionfetchContents()
store
Save data into cache. Tip: use it when state value is 2
public functionstore($data)
(string) data the data to save into cache file.
clear
Delete all files inside a cache folder (group) and the folder it self.
public functionclear($group='')
(string) group the cache folder. Attention: If empty all cache items will be deleted!
clearItems
Delete selective items within a group (folder).
public functionclearItems($group, $filter)
(string) group the cache folder, it can be an empty string for the cache root folder.
(string) filter Only files matching (starting from) this filter will be returned.
getError
Get the last generated error message.
public functiongetError()
Real world example
Real world example of using the elxisCache library.
//In $xml we will put the XML data from cache or the remote host.