/** * @see org.springframework.cache.Cache#put(java.lang.Object, java.lang.Object) */ @Override public void put(final Object key, final Object value) { this.nativeCache.put(key, value != null ? value : NullValue.NULL); }
@Override public Exchange recover(CamelContext camelContext, String exchangeId) { LOG.trace("Recovering an Exchange with ID {}.", exchangeId); return useRecovery ? unmarshallExchange(camelContext, cache.get(exchangeId)) : null; }
/** * @see org.springframework.cache.Cache#clear() */ @Override public void clear() { this.nativeCache.clear(); }
/** * @see org.infinispan.commons.api.BasicCache#put(Object, Object, long, TimeUnit) */ public void put(Object key, Object value, long lifespan, TimeUnit unit) { try { if (writeTimeout > 0) this.nativeCache.putAsync(key, value != null ? value : NullValue.NULL, lifespan, unit).get(writeTimeout, TimeUnit.MILLISECONDS); else this.nativeCache.put(key, value != null ? value : NullValue.NULL, lifespan, unit); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CacheException(e); } catch (ExecutionException | TimeoutException e) { throw new CacheException(e); } }
/** * @see org.springframework.cache.Cache#clear() */ @Override public void clear() { try { if (writeTimeout > 0) this.nativeCache.clearAsync().get(writeTimeout, TimeUnit.MILLISECONDS); else this.nativeCache.clear(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CacheException(e); } catch (ExecutionException | TimeoutException e) { throw new CacheException(e); } }
/** * @see org.springframework.cache.Cache#evict(Object) */ @Override public void evict(final Object key) { try { if (writeTimeout > 0) this.nativeCache.removeAsync(key).get(writeTimeout, TimeUnit.MILLISECONDS); else this.nativeCache.remove(key); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CacheException(e); } catch (ExecutionException | TimeoutException e) { throw new CacheException(e); } }
@Test public void demonstrateCachingLoadedBooksJsr107() { final Integer bookToCacheId = Integer.valueOf(5); assert !booksCache().containsKey(bookToCacheId) : "Cache should not initially contain the book with id " + bookToCacheId; final Book cachedBook = this.bookDao.findBook(bookToCacheId); this.log.infof("Book [%s] cached", cachedBook); assert cachedBook.equals(booksCache().get(bookToCacheId)) : "findBook(" + bookToCacheId + ") should have cached book"; }
@Override @ManagedOperation(description = "Adds the key to the store") public boolean add(Object key) { // need to check first as put will update the entry lifetime so it can not expire its cache lifespan if (getCache().containsKey(key)) { // there is already an entry so return false return false; } Boolean put = getCache().put(key, true); return put == null; }
@Override @ManagedOperation(description = "Does the store contain the given key") public boolean contains(Object key) { return getCache().containsKey(key); }
/** * @see org.springframework.cache.Cache#evict(java.lang.Object) */ @Override public void evict(final Object key) { this.nativeCache.remove(key); }
@Test public void testUsingTemplateNoRollback() throws SystemException, HeuristicRollbackException, HeuristicMixedException, RollbackException { final Book[] created = new Book[2]; final Book[] found = new Book[2]; assert !transactionalCache().containsKey(Integer.valueOf(9)); TransactionManager tm = getTransactionManager("booksTransactional"); assert tm.getStatus() == 0 : "Transaction should be in state 'RUNNING'"; Book nonTxBook = new Book("1-1-2-3-5", "Random author", "Title"); Book txBook = new Book("1-2-2-4-8", "Not so random author", "Title"); created[0] = bookService.createBookTransactionalCache(nonTxBook); found[0] = bookService.findBookTransactionalCache(Integer.valueOf(9)); created[1] = bookService.createBookNonTransactionalCache(txBook); found[1] = bookService.findBookNonTransactionalCache(Integer.valueOf(9)); // commit changes tm.commit(); // all items should be cached regardless of cache type assert transactionalCache().values().contains(created[0]); assert transactionalCache().values().contains(found[0]); assert nonTransactionalCache().values().contains(created[1]); assert nonTransactionalCache().values().contains(found[1]); // check whether books have been persisted assert bookService.findBookCacheDisabled(created[0].getId()) != null; assert bookService.findBookCacheDisabled(created[1].getId()) != null; }
@Override public Collection<V> values() { return delegate.values(); }
/** * @see org.springframework.cache.Cache#getName() */ @Override public String getName() { return this.nativeCache.getName(); }
@Override public int size() { return delegate.size(); }
@Test public void demonstrateCachingLoadedBooksJsr107() { final Integer bookToCacheId = Integer.valueOf(5); assert !booksCache().containsKey(bookToCacheId) : "Cache should not initially contain the book with id " + bookToCacheId; final Book cachedBook = this.bookDao.findBook(bookToCacheId); this.log.infof("Book [%s] cached", cachedBook); assert cachedBook.equals(booksCache().get(bookToCacheId)) : "findBook(" + bookToCacheId + ") should have cached book"; }
@Test public void demonstrateCacheEvictionUponUpdateJsr107() { final Integer bookToUpdateId = Integer.valueOf(2); assert !booksCache().containsKey(bookToUpdateId): "Cache should not initially contain the book with id " + bookToUpdateId; Book bookToUpdate = new Book("112-358-132", "Random Author", "Path to Infinispan Enlightenment"); booksCache().put(bookToUpdate, bookToUpdate); assert booksCache().containsKey(bookToUpdate); bookToUpdate.setTitle("Work in Progress"); Book result = bookDao.updateBook(bookToUpdate); this.log.infof("Book [%s] updated", bookToUpdate); assert !booksCache().containsKey(bookToUpdate) : "updateBook(" + bookToUpdate + ") should have removed updated book from cache"; }
/** * @see org.springframework.cache.Cache#put(Object, Object) */ @Override public void put(final Object key, final Object value) { try { if (writeTimeout > 0) this.nativeCache.putAsync(key, value != null ? value : NullValue.NULL).get(writeTimeout, TimeUnit.MILLISECONDS); else this.nativeCache.put(key, value != null ? value : NullValue.NULL); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CacheException(e); } catch (ExecutionException | TimeoutException e) { throw new CacheException(e); } }
@Test public void testFindConditionNotMet() { final Integer bookToCacheId = Integer.valueOf(1); assert !booksCache().containsKey(bookToCacheId): "Cache should not initially contain the book with id " + bookToCacheId; final Book cachedBook = getBookService().findBookCondition(bookToCacheId); this.log.infof("Book [%s] cached", cachedBook); assert !booksCache().containsKey(bookToCacheId) : "findBook(" + bookToCacheId + ") should not have cached book"; }
@Override public void confirm(CamelContext camelContext, String exchangeId) { LOG.trace("Confirming an exchange with ID {}.", exchangeId); cache.remove(exchangeId); }
@Test public void testUsingTemplateNoRollback() throws SystemException, HeuristicRollbackException, HeuristicMixedException, RollbackException { final Book[] created = new Book[2]; final Book[] found = new Book[2]; assert !transactionalCache().containsKey(Integer.valueOf(9)); TransactionManager tm = getTransactionManager("booksTransactional"); assert tm.getStatus() == 0 : "Transaction should be in state 'RUNNING'"; Book nonTxBook = new Book("1-1-2-3-5", "Random author", "Title"); Book txBook = new Book("1-2-2-4-8", "Not so random author", "Title"); created[0] = bookService.createBookTransactionalCache(nonTxBook); found[0] = bookService.findBookTransactionalCache(Integer.valueOf(9)); created[1] = bookService.createBookNonTransactionalCache(txBook); found[1] = bookService.findBookNonTransactionalCache(Integer.valueOf(9)); // commit changes tm.commit(); // all items should be cached regardless of cache type assert transactionalCache().values().contains(created[0]); assert transactionalCache().values().contains(found[0]); assert nonTransactionalCache().values().contains(created[1]); assert nonTransactionalCache().values().contains(found[1]); // check whether books have been persisted assert bookService.findBookCacheDisabled(created[0].getId()) != null; assert bookService.findBookCacheDisabled(created[1].getId()) != null; }