Settings and cache
Two registry objects hold values that are needed often. $this->config holds the settings of the shop — they change rarely and are edited in the admin panel. $this->cache holds results of slow work, so that the next request does not repeat it.
Reading a setting
$storeName = $this->config->get('config_name');get() returns the value by key, or null if there is no such setting. Here it is the shop name from System → Settings.
Where settings come from
| Source | What is in it |
|---|---|
Table oc_setting | Everything saved in the admin panel, including settings of extensions |
system/config/ | Defaults of the engine: cache and session engines, startup actions |
At the start of every request the engine loads the rows of oc_setting for the current store into $this->config.
Frequently used keys:
| Key | Value |
|---|---|
config_name | Shop name |
config_email | Shop email |
config_language | Code of the current language, en-gb |
config_language_id | Id of the current language |
config_currency | Default currency |
config_store_id | Id of the current store |
module_acme_hello_text | A setting of your module |
The prefix shows the owner: config_ is the core, module_, payment_, shipping_, total_ are extensions.
Changing for one request
$this->config->set('module_acme_hello_text', 'Temporary text');set() changes the value only until the end of the request. To save a setting permanently, the admin model setting/setting with its editSetting() is used — as on the settings page of the module.
The cache
A list that takes long to build is saved in the cache and read from it next time:
// extension/acme_hello/catalog/controller/module/acme_hello.php, in index()
$key = 'acme_hello.latest.' . (int) $this->config->get('config_language_id');
$products = $this->cache->get($key);
if (!$products) {
$this->load->model('catalog/product');
$products = $this->model_catalog_product->getProducts(['start' => 0, 'limit' => 4]);
$this->cache->set($key, $products);
}The first request runs the query and saves the result, the following ones take the array from the cache. The key includes the language id, because product names differ between languages.
| Method | What it does |
|---|---|
get($key) | Returns the value, or an empty value if there is none or it expired |
set($key, $value) | Saves the value |
delete($key) | Removes the value |
Where the cache is stored
By default it is files in system/storage/cache/. The engine and the lifetime of a record are set in system/config/default.php with the keys cache_engine and cache_expire; other engines, for example Redis, are available there.
Clearing
When the data changes, the cache record is deleted — otherwise the page shows old data until the record expires:
$this->cache->delete('acme_hello.latest.' . (int) $this->config->get('config_language_id'));During development it is enough to delete the files in system/storage/cache/.
Common mistake
Data that depends on the visitor — the cart, the name, prices of a customer group — is saved under a common key. The next visitor gets another person's data from the cache. Cache only what is the same for everyone, or add to the key everything the result depends on: language, currency, customer group.