5 Replies Latest reply on Dec 7, 2017 11:00 AM by Galder Zamarreño

    How to access cache used by jcache annotations?

    jimmy001 Apprentice

      Hello,

       

      following question: How can I access the cache used by the jcache annotations?

      I expected the following to work, but well.. I wouldn't ask if it would.

       

      @Stateless
      @CacheDefaults(cacheName = UserDAO.USER_CACHE_NAME)
      public class UserDAO {
           protected static final String USER_CACHE_NAME= "users";
      
      
      @CacheResult
      
          public User getUserByUsername(String username) {
                User result = ....
                return result;
         }
      }
      
      @Stateless
      public class UserAccountService {
      
       public User findUserByAccountname(String username) throws AwinoEasyBaseException {
              User result1 = userDAO.getUserByUsername(username);//result1 is NOT NULL
              CachingProvider cachingProvider = Caching.getCachingProvider();
              Cache<Object, Object> userCache = cachingProvider.getCacheManager().getCache("users");
              User result2 = (User) userCache.get(username);
              //result2 IS NULL => Must be a different cache
              return result2;
              //return userDAO.getUserByUsername(username);
          }
      

       

       

      Thx for your help.