Download the PHP package jnilla/joomla-cache-helper without Composer
On this page you can find all versions of the php package jnilla/joomla-cache-helper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jnilla/joomla-cache-helper
More information about jnilla/joomla-cache-helper
Files in jnilla/joomla-cache-helper
Package joomla-cache-helper
Short Description Use Joomla cache support fast and easy
License MIT
Homepage https://github.com/jnilla/joomla-cache-helper
Informations about the package joomla-cache-helper
joomla-cache-helper
A Joomla cache helper for easier usage.
This helper is build on top of the native Joomla cache support. We implemented a simpler API and added few extra features to make the helper practical.
Installation
Install using Composer:
Load the library using the Composer autoloader:
Basic Usage
Declaration:
Store data to cache:
Get data from cache:
The proxy method:
If the flag isValid
is true
you can use the cache item data safely.
The most practical way to work with this library is using the proxy
method.
This methods works as 2 in 1 intermediary that get and updates the cache item automatically if needed.
How it works:
- If the cache item is not stale the method returns the cache item.
- If the cache item is stale the method executes de callback, store the result in the cache and returns the updated cache item.
Example with remote data:
The lifetime is the time before a cache item is considered stale and needs to be updated.
It's useful to know that we can use the $lifetime
argument as a request rate limit mechanism using a formula like this 60 seconds / number_of_request = lifetime. Example: For a rate limit of 10 requests per minute use: 60 seconds / 10 requests = 10 seconds.
Wait Mechanism
There is a wait mechanism that is useful for certain cases of use.
The methods get
and proxy
share the following argument:
- @param integer $wait: Time in seconds to force current operation to wait if the cache item is updating.
Case 1:
5 users request remote data using the proxy
method at the same time for the first time and the $wait
is not 0
.
The first user to use the proxy
method triggers the isUpdading
flag and everyone gets to wait for the remote fetch operation to finish. After this, everyone gets the same data at the same time.
Case 2:
5 users request remote data using the proxy
method at the same time for the first time and the $wait
is 0
.
The first user to use the proxy
method triggers the isUpdading
flag and only this user gets to wait for the remote fetch operation to finish. Everyone else gets an empty cache item at the same time.
This case is useful if you don't want to lock everyone on a waiting period the firt time the remote data is requested.
This behavior helps to aliviate server RAM usage because requests/CPU Threads live shorter.
Case 3:
5 users request remote data using the proxy
method at the same time for the first time and the $wait
is not 0
, but the remote operation takes too long.
The first user to use the proxy
method triggers the isUpdading
flag and everyone gets to wait. If $wait
time is reached the isUpdading
flag is set to false
, the isTimeout
flag is set to true
and everyone gets an empty cache item at the same time.
This prevents too long or infinite wait times for eveyone.
License
This project is under the MIT License.