star

Tip

This page only contains information on the Jt.cache API. For a deeper dive into caching and how to use it, check out Caching.

Return the app cache. The app cache is shared across all sessions. Put values in this map that are meant to be shared across all users. For instance: database long-lived connections, ML models loaded weights, etc...

See documentation.

Method Signatures and Parameters

Jt.cache()

Returns after .use()

(io.jeamlit.datastructure.TypedMap)

The current io.jeamlit.datastructure.TypedMap value of the component.

Examples

Caching expensive computations

 import io.jeamlit.core.Jt;

 public class CacheApp {
      public static void main(String[] args) {
          String cacheKey = "long_running_operation";
          Long result = Jt.cache().getLong(cacheKey);

          if (result == null) {
              Jt.text("Performing a long running operation. This will take a few seconds").use();
              result = long_running_operation();
              Jt.cache().put(cacheKey, result);
          }

          Jt.text("Result of long operation: " + result).use();
          Jt.text("Refresh or Open the page in another tab: the long running operation result will be cached").use();
      }

      private static long long_running_operation(){
          try {
              Thread.sleep(5000);
          } catch (InterruptedException ignored) {
          }
          return 42;
      }
  }

Sharing data across users

 import io.jeamlit.core.Jt;

 public class SharedDataApp {
     public static void main(String[] args) {
         // initialization
         Jt.cache().putIfAbsent("counter", 0);
         // increment visits
         int totalVisits = Jt.cache().computeInt("counter", (k, v) -> v + 1);

         Jt.text("Total app visits: " + totalVisits).use();
     }
 }

Deleting values in the cache:

 
 // remove all values
 Jt.cache().clear();
 // remove a single key
 Jt.cache().remove("my_key");
 
 
TypedMap simply extends the java Map type with quality-of-life casting methods like getInt, getDouble, getString, etc...

forum

Still have questions?

Go to our discussions forum for helpful information and advice from Jeamlit experts.