Uploaded image for project: 'Runtime'
  1. Runtime
  2. RUNTIME-3387

Expose a CachedValue implementation

XMLWordPrintable

    • Icon: Task Task
    • Resolution: Fixed
    • Icon: Major Major
    • 4.3.0
    • 4.3.0
    • None
    • None
    • 4.3 RC2

      In order to avoid the following pattern which is very very present :

      private SomeAttribute _lazyGetSomeAttribute()
      {
          if (_someAttribute == null)
          {
              _someAttribute = _loadSomeAttributeFromWhatever();
          }
          return _someAttribute;
      }

      which is not thread-safe (most of the time the keyword synchronized is forgotten), error-prone, is always the same (reinventing the wheel) and stil expose a private attribute _someAttribute  which can be accidentally used elsewhere and lead to a NPE.

       

      I propose to have a class to wrap that behavior, on a similar API than in ThreadLocal, with at least those methods:

      public static <T> CachedValue<T> withInitial(Supplier<T> supplier)
      public T get()

       

      I propose to have an implementation based on com.google.common.cache.LoadingCache

      It has the major benefit of being thread-safe and so on, it can be more trusted than any of our own implementation

      Plus it would enable some other interesting features for future improvements:

      • expiry duration: the value is removed from the cache and recomputed (or not) every X seconds/minutes/hour/...
      • ...

       

      Note that I'm surprised it does not exist elsewhere. I've searched in Guava, ApacheCommons but did not find anything Maybe I did not searched the correct keywords.

            Unassigned Unassigned
            sprieul Simon Prieul (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: