@Test public void testGetShoppingCartFromCacheLoggedOutActive() throws Exception { final ShoppingCartStateService shoppingCartStateService = context.mock(ShoppingCartStateService.class, "shoppingCartStateService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final CartUpdateProcessor cartUpdateProcessor = context.mock(CartUpdateProcessor.class, "cartUpdateProcessor"); final TaskExecutor taskExecutor = context.mock(TaskExecutor.class, "taskExecutor"); final CacheManager cacheManager = context.mock(CacheManager.class, "cacheManager"); final Cache cartCache = context.mock(Cache.class, "cartCache"); final Cache.ValueWrapper wrapper = context.mock(Cache.ValueWrapper.class, "wrapper"); final ShoppingCart cachedCart = context.mock(ShoppingCart.class, "cachedCart"); final byte[] cached = new byte[0]; context.checking(new Expectations() {{ oneOf(cacheManager).getCache("web.shoppingCart"); will(returnValue(cartCache)); oneOf(cartCache).get("IN-CACHE"); will(returnValue(wrapper)); oneOf(wrapper).get(); will(returnValue(cached)); oneOf(mockCartSerDes).restoreState(cached); will(returnValue(cachedCart)); allowing(cachedCart).getLogonState(); will(returnValue(ShoppingCart.SESSION_EXPIRED)); }}); final ResilientCartRepositoryImpl repo = new ResilientCartRepositoryImpl(shoppingCartStateService, shopService, cartUpdateProcessor, mockCartSerDes, 60, cacheManager, taskExecutor); assertSame(cachedCart, repo.getShoppingCart("IN-CACHE")); context.assertIsSatisfied(); }
@Override public ShoppingCart getShoppingCart(final String token) { if (StringUtils.isBlank(token)) { return null; } final ShoppingCart cachedCart = getFromCache(token); if (cachedCart != null) { final boolean invalidateLogin = cachedCart.getLogonState() == ShoppingCart.LOGGED_IN && determineIfLoginInvalidationRequired( cachedCart.getModifiedTimestamp(), cachedCart.getShoppingContext().getShopId() ); if (invalidateLogin) { if (cachedCart instanceof MutableShoppingCart) { cartUpdateProcessor.invalidateShoppingCart(cachedCart); storeAsynchronously(cachedCart); return cachedCart; } else { evictFromCache(token); } } else { return cachedCart; } } final ShoppingCartState state = shoppingCartStateService.findByGuid(token); if (state != null) { final ShoppingCart dbCart = cartUpdateProcessor.restoreState(state.getState()); if (dbCart == null) { shoppingCartStateService.delete(state); } else { final boolean invalidateLogin = dbCart.getLogonState() == ShoppingCart.LOGGED_IN && determineIfLoginInvalidationRequired( state, dbCart.getShoppingContext().getShopId() ); if (invalidateLogin) { if (dbCart instanceof MutableShoppingCart) { cartUpdateProcessor.invalidateShoppingCart(dbCart); storeAsynchronously(dbCart); } else { evictFromCache(dbCart.getGuid()); return null; } } putToCache(dbCart.getGuid(), dbCart); return dbCart; } } return null; }
ResilientCartRepositoryImpl implements CartRepository { @Override public ShoppingCart getShoppingCart(final String token) { if (StringUtils.isBlank(token)) { return null; } final ShoppingCart cachedCart = getFromCache(token); if (cachedCart != null) { final boolean invalidateLogin = cachedCart.getLogonState() == ShoppingCart.LOGGED_IN && determineIfLoginInvalidationRequired( cachedCart.getModifiedTimestamp(), cachedCart.getShoppingContext().getShopId() ); if (invalidateLogin) { if (cachedCart instanceof MutableShoppingCart) { cartUpdateProcessor.invalidateShoppingCart(cachedCart); storeAsynchronously(cachedCart); return cachedCart; } else { evictFromCache(token); } } else { return cachedCart; } } final ShoppingCartState state = shoppingCartStateService.findByGuid(token); if (state != null) { final ShoppingCart dbCart = cartUpdateProcessor.restoreState(state.getState()); if (dbCart == null) { shoppingCartStateService.delete(state); } else { final boolean invalidateLogin = dbCart.getLogonState() == ShoppingCart.LOGGED_IN && determineIfLoginInvalidationRequired( state, dbCart.getShoppingContext().getShopId() ); if (invalidateLogin) { if (dbCart instanceof MutableShoppingCart) { cartUpdateProcessor.invalidateShoppingCart(dbCart); storeAsynchronously(dbCart); } else { evictFromCache(dbCart.getGuid()); return null; } } putToCache(dbCart.getGuid(), dbCart); return dbCart; } } return null; } }
ResilientCartRepositoryImpl implements CartRepository { @Override public ShoppingCart getShoppingCart(final String token) { if (StringUtils.isBlank(token)) { return null; } final ShoppingCart cachedCart = getFromCache(token); if (cachedCart != null) { final boolean invalidateLogin = cachedCart.getLogonState() == ShoppingCart.LOGGED_IN && determineIfLoginInvalidationRequired( cachedCart.getModifiedTimestamp(), cachedCart.getShoppingContext().getShopId() ); if (invalidateLogin) { if (cachedCart instanceof MutableShoppingCart) { cartUpdateProcessor.invalidateShoppingCart(cachedCart); storeAsynchronously(cachedCart); return cachedCart; } else { evictFromCache(token); } } else { return cachedCart; } } final ShoppingCartState state = shoppingCartStateService.findByGuid(token); if (state != null) { final ShoppingCart dbCart = cartUpdateProcessor.restoreState(state.getState()); if (dbCart == null) { shoppingCartStateService.delete(state); } else { final boolean invalidateLogin = dbCart.getLogonState() == ShoppingCart.LOGGED_IN && determineIfLoginInvalidationRequired( state, dbCart.getShoppingContext().getShopId() ); if (invalidateLogin) { if (dbCart instanceof MutableShoppingCart) { cartUpdateProcessor.invalidateShoppingCart(dbCart); storeAsynchronously(dbCart); } else { evictFromCache(dbCart.getGuid()); return null; } } putToCache(dbCart.getGuid(), dbCart); return dbCart; } } return null; } ResilientCartRepositoryImpl(final ShoppingCartStateService shoppingCartStateService,
                                       final ShopService shopService,
                                       final CartUpdateProcessor cartUpdateProcessor,
                                       final ShoppingCartStateSerDes shoppingCartStateSerDes,
                                       final int sessionExpiryInSeconds,
                                       final CacheManager cacheManager,
                                       final TaskExecutor taskExecutor);  ResilientCartRepositoryImpl(final ShoppingCartStateService shoppingCartStateService,
                                       final ShopService shopService,
                                       final CartUpdateProcessor cartUpdateProcessor,
                                       final ShoppingCartStateSerDes shoppingCartStateSerDes,
                                       final int sessionExpiryInSeconds,
                                       final CacheManager cacheManager); }
ResilientCartRepositoryImpl implements CartRepository { @Override public ShoppingCart getShoppingCart(final String token) { if (StringUtils.isBlank(token)) { return null; } final ShoppingCart cachedCart = getFromCache(token); if (cachedCart != null) { final boolean invalidateLogin = cachedCart.getLogonState() == ShoppingCart.LOGGED_IN && determineIfLoginInvalidationRequired( cachedCart.getModifiedTimestamp(), cachedCart.getShoppingContext().getShopId() ); if (invalidateLogin) { if (cachedCart instanceof MutableShoppingCart) { cartUpdateProcessor.invalidateShoppingCart(cachedCart); storeAsynchronously(cachedCart); return cachedCart; } else { evictFromCache(token); } } else { return cachedCart; } } final ShoppingCartState state = shoppingCartStateService.findByGuid(token); if (state != null) { final ShoppingCart dbCart = cartUpdateProcessor.restoreState(state.getState()); if (dbCart == null) { shoppingCartStateService.delete(state); } else { final boolean invalidateLogin = dbCart.getLogonState() == ShoppingCart.LOGGED_IN && determineIfLoginInvalidationRequired( state, dbCart.getShoppingContext().getShopId() ); if (invalidateLogin) { if (dbCart instanceof MutableShoppingCart) { cartUpdateProcessor.invalidateShoppingCart(dbCart); storeAsynchronously(dbCart); } else { evictFromCache(dbCart.getGuid()); return null; } } putToCache(dbCart.getGuid(), dbCart); return dbCart; } } return null; } ResilientCartRepositoryImpl(final ShoppingCartStateService shoppingCartStateService,
                                       final ShopService shopService,
                                       final CartUpdateProcessor cartUpdateProcessor,
                                       final ShoppingCartStateSerDes shoppingCartStateSerDes,
                                       final int sessionExpiryInSeconds,
                                       final CacheManager cacheManager,
                                       final TaskExecutor taskExecutor);  ResilientCartRepositoryImpl(final ShoppingCartStateService shoppingCartStateService,
                                       final ShopService shopService,
                                       final CartUpdateProcessor cartUpdateProcessor,
                                       final ShoppingCartStateSerDes shoppingCartStateSerDes,
                                       final int sessionExpiryInSeconds,
                                       final CacheManager cacheManager); @Override ShoppingCart createCart(); @Override ShoppingCart getShoppingCart(final String token); @Override String storeShoppingCart(final ShoppingCart shoppingCart); @Override void evictShoppingCart(final ShoppingCart shoppingCart); }
ResilientCartRepositoryImpl implements CartRepository { @Override public ShoppingCart getShoppingCart(final String token) { if (StringUtils.isBlank(token)) { return null; } final ShoppingCart cachedCart = getFromCache(token); if (cachedCart != null) { final boolean invalidateLogin = cachedCart.getLogonState() == ShoppingCart.LOGGED_IN && determineIfLoginInvalidationRequired( cachedCart.getModifiedTimestamp(), cachedCart.getShoppingContext().getShopId() ); if (invalidateLogin) { if (cachedCart instanceof MutableShoppingCart) { cartUpdateProcessor.invalidateShoppingCart(cachedCart); storeAsynchronously(cachedCart); return cachedCart; } else { evictFromCache(token); } } else { return cachedCart; } } final ShoppingCartState state = shoppingCartStateService.findByGuid(token); if (state != null) { final ShoppingCart dbCart = cartUpdateProcessor.restoreState(state.getState()); if (dbCart == null) { shoppingCartStateService.delete(state); } else { final boolean invalidateLogin = dbCart.getLogonState() == ShoppingCart.LOGGED_IN && determineIfLoginInvalidationRequired( state, dbCart.getShoppingContext().getShopId() ); if (invalidateLogin) { if (dbCart instanceof MutableShoppingCart) { cartUpdateProcessor.invalidateShoppingCart(dbCart); storeAsynchronously(dbCart); } else { evictFromCache(dbCart.getGuid()); return null; } } putToCache(dbCart.getGuid(), dbCart); return dbCart; } } return null; } ResilientCartRepositoryImpl(final ShoppingCartStateService shoppingCartStateService,
                                       final ShopService shopService,
                                       final CartUpdateProcessor cartUpdateProcessor,
                                       final ShoppingCartStateSerDes shoppingCartStateSerDes,
                                       final int sessionExpiryInSeconds,
                                       final CacheManager cacheManager,
                                       final TaskExecutor taskExecutor);  ResilientCartRepositoryImpl(final ShoppingCartStateService shoppingCartStateService,
                                       final ShopService shopService,
                                       final CartUpdateProcessor cartUpdateProcessor,
                                       final ShoppingCartStateSerDes shoppingCartStateSerDes,
                                       final int sessionExpiryInSeconds,
                                       final CacheManager cacheManager); @Override ShoppingCart createCart(); @Override ShoppingCart getShoppingCart(final String token); @Override String storeShoppingCart(final ShoppingCart shoppingCart); @Override void evictShoppingCart(final ShoppingCart shoppingCart);  }