Caching can be used in situations where data retreived from other places does not change often, or is slow to fetch.
Examples are Static lists in databases or entire web-pages that are static.
The cache in SPIN applications is always available through the cache()
static class.
The cache is configured in the config-<environment>.json
file under the caches
key:
"caches": {
"apcu": { // Name of Cache
"adapter": "APCu", // Adaptername
"class": "\\Spin\\Cache\\Adapters\\Apcu", // The class to load
"options": {} // Optional options for class
}
}
Using multiple caches is possible through the naming of the cache.
The config would look like this:
"caches": {
"apcu": { // Name of Cache
"adapter": "APCu", // Adaptername
"class": "\\Spin\\Cache\\Adapters\\Apcu", // The class to load
"options": {} // Optional options for class
},
"redis": { // Name of Cache
"adapter": "Redis", // Adaptername
"class": "\\Spin\\Cache\\Adapters\\Redis", // The class to load
"options": {} // Optional options for class
}
}
If the code wants to use the "redis" cache then it would be accessed via cache('redis')
.
# Set cache key 'aKey' = 'value' for 10 seconds
$ok = cache()->set('aKey', 'value', 10);
# Get cache key 'aKey' into $value
$value = cache()->get('aKey');
TBD