Skip to content

S3 Caching

Panis26 edited this page Nov 7, 2023 · 2 revisions

Caching Strategies

Kotlin

The FormProgressCache class provided in your code is a custom implementation of a cache that stores a limited number of forms in memory. It uses a LinkedHashMap data structure to keep track of the forms.

  • maxSize: This parameter defines the maximum number of forms the cache can hold. When a new form is added and the maximum capacity is reached, the cache will evict the oldest form to make space for the new one.

  • cache: It is a LinkedHashMap that is initialized with a maximum size and a load factor of 0.75. The accessOrder option is set to true, indicating that access order will be used to order the elements in the cache.

  • get(key): Retrieves the value associated with the provided key. It uses a synchronized block to ensure thread-safe access to the cache in a multithreaded environment.

  • put(key, value): Adds a key-value pair to the cache. It also uses a synchronized block to ensure thread safety.

  • remove(key): Removes the entry associated with the provided key in the cache.

The EventCreationFragment code snippet makes use of this cache to temporarily store form data when the fragment is paused or resumed. Upon pausing the fragment, the form data is stored in the cache, and upon resuming the fragment, the form data is retrieved from the cache and used to restore the form's state. This is useful for preserving form data in case the application unexpectedly closes or if the user navigates back and forth between fragments.

The FormProgressCache class implements a simple and effective caching strategy for storing and managing forms in the application's memory. It employs a "last-used, first-out" approach to maintain a limited number of forms in the cache.

Flutter

Caching in Flutter was implemented with a global HashMap variable called "cache" which also provides a set of methods available in any place in the app, in order to allow its usage for general purposes along the app. This is the implementation of this variable and its service functions:

This variable is used on all the form views, which correspond to SignUp, LogIn, and EventCreate views, and it's used to both write and read information from Cache.

For example, the information from the cache is read when the forms are first rendered, for example:

On the other hand, the form field information is saved to the cache every time that something is typed on the field; for this, every form field has its own "onChanged" method that writes the current data to the cache, and also validates that the data typed is valid, as shown in this "description" field:

Finally, when the operation is successful (either for SignUp, LogIn or EventCreate), the cache is flushed in order to avoid overwriting future data.

Clone this wiki locally