# Memcache caching
## How to calculate infinite expiration timestamp
1. open SSH connection to a web server (or where the Memcache is installed)
2. run this command:
```
lang=bash
(sleep 1; echo "stats"; sleep 1; echo "quit";) | telnet localhost 11211 | grep 'time'
```
Example output:
```
STAT uptime 61335
STAT time 1676435743
```
3. subtract the number after the `uptime` word from the number after the `time` word (e.g. `1676435743 - 61335 = 1676374408`) and remember this number
## How to determine key expiration
1. open SSH connection to a web server (or where the Memcache is installed)
2. run this command (replace `slab_number_here` with a positive number starting from 1; replace `key_name_here` with a key name you want to get info about):
```
lang=bash
(sleep 1; echo "stats cachedump slab_number_here 0"; sleep 1; echo "quit";) | telnet localhost 11211 | grep 'key_name_here'
```
Example output:
```
ITEM key_name_here [value_here b; timestamp_here s]
```
3. if no data is returned try incrementing the number used in place of the `slab_number_here` text by 1 and running that command again
4. if the number in place of the `timestamp_here` text matches the infinite expiration timestamp, then the key will never expire
5. otherwise the key will expire at the given date/time
## How to determine every key expiration/presence in the cache
# same steps as above, but omit the ` | grep 'key_name_here'` part of the command
# iterate all slab numbers until no data between `Escape character is '^]'.` and `END` lines is returned
## How to clean the cache
1. if you have CentOS, then run the `systemctl restart memcached.service` command as the `root` user on the server
2. when testing on the Memcache PHP extension (not Memcached PHP extension) please refresh Admin Console (the login will do) to initialize the Memcache, because otherwise, it won't work properly
# Database caching
## How to determine key expiration
# open the database (e.g. via phpMyAdmin)
# execute the `SELECT LifeTime FROM SystemCache WHERE VarName = 'key_name_here';` database query
# if you'll get `-1` it means, that the key won't automatically expire
# if you'll get a timestamp it means, that the key will expire after that time
## How to determine every key expiration/presence in the cache
1. same steps as above, but omit the ` WHERE VarName = 'key_name_here'` part of the database query
## How to clean the cache
# open the database (e.g. via phpMyAdmin)
# run the `TRUNCATE SystemCache;` database query in the
# Actual tests
## Testing clean install
# perform a clean install via the `/core/install.php` script
# confirm, that at the `System Configuration` installation step the `Maximum Cache Duration (in days)` setting has an initial value of `30`
# change value to `25` and proceed with the install
# confirm, that after the installation was done the `MaxCacheDuration` setting in the `/system/config.php` has a value of `25`
## Testing an upgrade
# remove the `$_CONFIG['Misc']['MaxCacheDuration'] = ...;` line in the `/system/config.php` file
# open the `/core/install.php` script of the existing In-Portal installation
# on the `Installation Maintenance` screen:
* type `root` user login/password
* select the `Upgrade In-Portal` radio button
* press `Continue`
# perform the upgrade
# confirm, that after the installation was done the `MaxCacheDuration` setting in the `/system/config.php` has a value of `30`
## Testing "setCache"
NOTE: Repeat with `CacheHandler` set to `Memcached` as well.
* setup:
* clean the memory cache (see instructions above)
* change the value of the `MaxCacheDuration` setting in the `/system/config.php` file to the `25`
* change the value of the `CacheHandler` setting in the `/system/config.php` file to the `Memcache`
* set without expiration:
* replace code fragment between `$application->Init();` and `$application->Done();` in the `/index.php` file with this code:
```
lang=php
$application->deleteCache('key_with_def_exp');
$application->setCache('key_with_def_exp', 'v1');
```
* confirm (see instructions above), that the "key_with_def_exp" key has an expiration of 25 days (from the time key was set via the "setCache" method call)
* set with expiration:
* replace code fragment between `$application->Init();` and `$application->Done();` in the `/index.php` file with this code:
```
lang=php
$application->deleteCache('key_with_exp');
$application->setCache('key_with_exp', 'v2', 3600);
```
* confirm (see instructions above), that the "key_with_exp" key has an expiration of 1 hour (from the time key was set via the "setCache" method call)
## Testing "addCache"
NOTE: Repeat with `CacheHandler` set to `Memcached` as well.
* setup:
* clean the memory cache (see instructions above)
* change the value of the `MaxCacheDuration` setting in the `/system/config.php` file to the `25`
* change the value of the `CacheHandler` setting in the `/system/config.php` file to the `Memcache`
* add without expiration:
* replace code fragment between `$application->Init();` and `$application->Done();` in the `/index.php` file with this code:
```
lang=php
$application->deleteCache('key_with_def_exp');
$application->addCache('key_with_def_exp', 'v1');
```
* confirm (see instructions above), that the "key_with_def_exp" key has an expiration of 25 days (from the time key was set via the "addCache" method call)
* add with expiration:
* replace code fragment between `$application->Init();` and `$application->Done();` in the `/index.php` file with this code:
```
lang=php
$application->deleteCache('key_with_exp');
$application->addCache('key_with_exp', 'v2', 3600);
```
* confirm (see instructions above), that the "key_with_exp" key has an expiration of 1 hour (from the time key was set via the "addCache" method call)
* add won't overwrite:
* replace code fragment between `$application->Init();` and `$application->Done();` in the `/index.php` file with this code:
```
lang=php
$application->addCache('key_with_exp', 'v3', 86400);
echo $application->getCache('key_with_exp');
```
* confirm, that both key value (that is shown on screen) and expiration (see instructions above) match `$application->addCache('key_with_exp', 'v2', 3600);` command executed above this test
## Testing "setDBCache"
* setup:
* clean the database cache (see instructions above)
* change the value of the `MaxCacheDuration` setting in the `/system/config.php` file to the `25`
* change the value of the `CacheHandler` setting in the `/system/config.php` file to the `Fake`
* set without expiration:
* replace code fragment between `$application->Init();` and `$application->Done();` in the `/index.php` file with this code:
```
lang=php
$application->deleteDBCache('db_key_with_def_exp');
$application->setDBCache('db_key_with_def_exp', 'v1');
```
* confirm (see instructions above), that "db_key_with_def_exp" key has expiration of 25 days (from the the time key was set via "setDBCache" method call)
* set with expiration:
* replace code fragment between `$application->Init();` and `$application->Done();` in the `/index.php` file with this code:
```
lang=php
$application->deleteDBCache('db_key_with_exp');
$application->setDBCache('db_key_with_exp', 'v2', 3600);
```
* confirm (see instructions above), that "db_key_with_exp" key has expiration of 1 hour (from the the time key was set via "setDBCache" method call)
## Testing internal caches expiration for Memcache
NOTE: Repeat with `CacheHandler` set to `Memcached` as well.
* setup:
* clean the memory cache (see instructions above)
* change the value of the `MaxCacheDuration` setting in the `/system/config.php` file to the `25`
* change the value of the `CacheHandler` setting in the `/system/config.php` file to the `Memcache`
* populate cache:
* login to In-Portal admin console
* open any category record for editing (to populate `master:StructureTree` key)
* make some changes
* save changed record to the database (to set category serial key)
* visit the Front-End in another web browser tab (to populate `master:template_mapping`, `master:cms_menu` and `master:domains_parsed` keys)
* confirm, that:
* these keys have infinite expiration (the `1738250904` number might differ on your install):
* `test`
* `site_serial:1738250904`
* `site_serial:1738250904:1:master:configs_parsed`
* `site_serial:1738250904:1:master:last_cache_rebuild`
* `site_serial:1738250904:1:master:StructureTree`
* `site_serial:1738250904:1:master:template_mapping`
* `site_serial:1738250904:1:master:cms_menu`
* `site_serial:1738250904:1:master:sections_parsed`
* `site_serial:1738250904:1:master:domains_parsed`
* `site_serial:1738250904:1:master:config_files`
* all other keys have an expiration of 25 days (with a few seconds margin) relative to the time they're set
## Testing internal caches expiration for DB cache
* setup:
* clean the memory cache (see instructions above)
* change the value of the `MaxCacheDuration` setting in the `/system/config.php` file to the `25`
* change the value of the `CacheHandler` setting in the `/system/config.php` file to the `Fake`
* populate cache:
* login to In-Portal admin console
* open any category record for editing (to populate `StructureTree` key)
* make some changes
* save changed record to the database (to set category serial key)
* visit the Front-End in another web browser tab (to populate `template_mapping`, `cms_menu` and `domains_parsed` keys)
* confirm, that:
* these keys have infinite expiration:
* `configs_parsed`
* `last_cache_rebuild`
* `StructureTree`
* `template_mapping`
* `cms_menu`
* `sections_parsed`
* `domains_parsed`
* `config_files`
* all other keys have an expiration of 25 days (with a few seconds margin) relative to the time they're set
## Testing template fragment caching in Memcache has an expiration cap
NOTE: Repeat with `CacheHandler` set to `Memcached` as well.
* setup:
* clean the memory cache (see instructions above)
* change the value of the `MaxCacheDuration` setting in the `/system/config.php` file to the `25`
* change the value of the `CacheHandler` setting in the `/system/config.php` file to the `Memcache`
* populate cache:
# login to the Admin Console
# go to the {nav Configuration > Website > Advanced} section
# enable the `Enable Tag Caching` system setting
# save changes
# open the Front-End
* confirm, that:
* `site_serial:1738250904:1:parser_35334543` like keys (all numbers may vary) are present
* they have an expiration of 25 days (with a few seconds margin) relative to the time they're set (page, that was setting them on the Front-End was accessed)
## Testing template fragment caching in Memcache can configure expiration
NOTE: Repeat with `CacheHandler` set to `Memcached` as well.
* setup:
* clean the memory cache (see instructions above)
* change the value of the `MaxCacheDuration` setting in the `/system/config.php` file to the `25`
* change the value of the `CacheHandler` setting in the `/system/config.php` file to the `Memcache`
* populate cache:
# change `<inp2:m_Cache key="prefix:lang;skip_var:t,page,per_page,sort_by">` line in the `themes/advanced/platform/elements/header.elm.tpl` template to `<inp2:m_Cache key="prefix:lang;skip_var:t,page,per_page,sort_by" cache_timeout="3600">`
# login to the Admin Console
# go to the {nav Configuration > Website > Advanced} section
# enable the `Enable Tag Caching` system setting
# save changes
# open the Front-End
* confirm, that:
* `site_serial:1738250904:1:parser_35334543` like keys (all numbers may vary) are present
* they some of them now have an expiration of 1 hour (with a few seconds margin) relative to the time they're set (page, that was setting them on the Front-End was accessed)