|
1 | | -# Cache |
| 1 | +ILIAS Caching Service |
| 2 | +===================== |
| 3 | + |
| 4 | +The ILIAS Caching Service replaces the old GlobalCache Service with ILIAS 9. |
| 5 | +Internally, the GlobalCache service currently also uses the new cache service, |
| 6 | +but will be removed in the medium term. |
| 7 | + |
| 8 | +# Cache data |
| 9 | + |
| 10 | +## When does it make sense to cache data? |
| 11 | + |
| 12 | +Data caching should be used when simple data does not change between two |
| 13 | +requests and is not user-independent. It must also be clear when the data in the |
| 14 | +cache becomes invalid and needs to be updated. Caching must also always include |
| 15 | +reading the data from a persistence layer (database, file system) as a fallback. |
| 16 | + |
| 17 | +## Differentiation from artifacts |
| 18 | + |
| 19 | +Artifacts are always suitable when the data not only does not change between two |
| 20 | +requests, but is always the same depending on the exact version of the ILIAS |
| 21 | +source code. Artifacts are generated when ILIAS is built, cache is filled at |
| 22 | +runtime. This can be easily explained using the following example: |
| 23 | + |
| 24 | +The translations in ILIAS (Language Service) would be a candidate for artifacts |
| 25 | +under the following circumstances: |
| 26 | + |
| 27 | +> The exact content of the translations in ILIAS is only available in the form |
| 28 | +> of language files. In this case, this content could be built as an Artifact |
| 29 | +> and |
| 30 | +> thus be available with high performance. |
| 31 | +
|
| 32 | +But then why are translations not in Artifacts? |
| 33 | + |
| 34 | +> ILIAS offers administration in the GUI for the translations, i.e. the exact |
| 35 | +> translations do not necessarily have to come from the language files, but can |
| 36 | +> change through customization. |
| 37 | +
|
| 38 | +Would the translations be a candidate for the cache? |
| 39 | + |
| 40 | +> Yes, under the following circumstances: It is known exactly when the content |
| 41 | +> of the translations changes (e.g. through changes to the language files |
| 42 | +> through |
| 43 | +> an ILIAS update or through manual adjustments in the administration. If these |
| 44 | +> situations are known, the language service can invalidate the cache at the |
| 45 | +> right |
| 46 | +> moment, read the correct content and rebuild the cache. |
| 47 | +
|
| 48 | +## How to use the cache service |
| 49 | + |
| 50 | +The data is stored in a cache container. To obtain such a container, a cache |
| 51 | +container request is implemented. This is very simple and can also be |
| 52 | +implemented on existing classes, for example, which is often useful for |
| 53 | +repositories. |
| 54 | + |
| 55 | +```php |
| 56 | +use ILIAS\Cache\Container\Request; |
| 57 | + |
| 58 | +class MyRepository implements Request { |
| 59 | + public function getContainerKey() : string{ |
| 60 | + return 'my_repository'; |
| 61 | + } |
| 62 | + public function isForced() : bool{ |
| 63 | + return true; |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +The two methods to be implemented can be described as follows: |
| 69 | + |
| 70 | +`getContainerKey`: This is the namespace for your container. ILIAS will |
| 71 | +check that two components are not using the same namespace and can therefore |
| 72 | +overwrite each other's cache. |
| 73 | + |
| 74 | +`isForced`: Individual containers can be activated via the setup, see the README |
| 75 | +for the setup. However, this can be overwritten with isForces if the cotnaienr |
| 76 | +is to be active in any case. This should not be used in the core but only in |
| 77 | +plugins. |
| 78 | + |
| 79 | +The container can then be obtained via the cache service: |
| 80 | + |
| 81 | +```php |
| 82 | +global DIC; |
| 83 | + |
| 84 | +$container = $DIC->chache()->get(new MyRepository()); |
| 85 | +``` |
| 86 | +The following methods are then available in the container: |
| 87 | + |
| 88 | +```php |
| 89 | +use ILIAS\Cache\Container\Container; |
| 90 | +/** @var Container $container */ |
| 91 | +$container = $DIC->globalCache()->get(new MyRepository()); |
| 92 | + |
| 93 | +$container->has('key'); // Check if a key exists |
| 94 | +$container->get('key'); // Get a value |
| 95 | +$container->set('key', 'value'); // Set a value |
| 96 | +$container->delete('key'); // Delete a value |
| 97 | +$container->flush(); // Delete all values |
| 98 | +$container->lock(3.5); // Lock the container, see below |
| 99 | +``` |
| 100 | + |
| 101 | +## Storable Values |
| 102 | +The service only supports simple scalar values for storage, e.g. no objects |
| 103 | +can be stored. The values can be stored individually or as an array. |
| 104 | + |
| 105 | +## Locking |
| 106 | +The COntainer can be locked for a certain period of time, i.e. `has` always |
| 107 | +returns false during this time, `get` always returns null. |
0 commit comments