Let’s now detail this DataCacheStorageConfig class.
This class is annotated @Configuration, it can be started using the component analysis (Dependency Injection).
Let’s define a logger that we will use later. Feel free to use your favorite logger, in this example we used slf4j.
Let’s move on to more interesting things:
We need to define 3 properties:
-> the path to our backup file (jsonFilePath).
-> the maximum number of elements we can inject in Dacas (limit).
-> the default expiration date (in seconds) if an element injected in Dacas has no clearly defined expiration date (ttl) .
So we will have to define the three following keys in our property file. We’ll come back to this later.
Let’s now declare a bean thanks to the @Bean decorator. This is what we do with the runDacas() method.
The DacasManager constructor takes 3 parameters :
–> DacaStorageSupportEnum.
—> FILE (storage in memory and saving memory to file)
—> MEMORY (storage only in memory)
–> limit (defined above)
–> jsonFilePath (defined above)
–> ttl (defined above)
The last two methods of classification (not mandatory but highly recommended) allow to:
–> automatically purge items whose expiration date has passed.
–> create a backup of the data present in memory to a backup file.
These two scheduled tasks are annotated @Scheduled. In our example, these tasks will be executed every half hour for the purge and every hour for the backup.
It is up to you to configure the frequency of these tasks according to the needs of your applications.
Please note that every time you make a backup, Dacas deletes the expired data beforehand.