1 Reply Latest reply on Aug 17, 2016 1:05 PM by andy-l

    Recompute expired cached value keeping old value on failure

    andy-l Newbie

      Hi,

      I have a cache with values loaded from a database, I am trying to write a @CacheEntryExpired listener that attempts to reload the value by querying the database but if for any reason it fails, it just places the old value back in the cache. This does not work because of the sequence the listener is notified, it seems to be notified before the expired value is removed so when I add it to the cache it gets removed. This is my code:

       

      @CacheEntryExpired
      public void entryExpired(CacheEntryEvent<String, Object> event) {

         if (!event.isPre()) {

              Cache<String, Object> cache = event.getCache();
              String key = event.getKey();
              Object value = null;
              try {

                  value = loadValue(key);
              } catch (Exception e) {

                   value = event.getValue();
              }

             cache.getAdvancedCache().withFlags(SKIP_LOCKING, IGNORE_RETURN_VALUES).put(key, value);
         }

      }

       

       

      I have a similar @CacheEntryInvalidated and this works fine, is there any other way to achieve this?