Code example for Cache

Methods: getIfPresentinvalidate

0
                    transactionID, shard.path());
        } 
 
        // Lookup the cohort entry that was cached previously (or should have been) by 
        // transactionReady (via the ForwardedReadyTransaction message). 
        final CohortEntry cohortEntry = cohortCache.getIfPresent(transactionID);
        if(cohortEntry == null) {
            // Either canCommit was invoked before ready(shouldn't happen)  or a long time passed 
            // between canCommit and ready and the entry was expired from the cache. 
            IllegalStateException ex = new IllegalStateException(
                    String.format("No cohort entry found for transaction %s", transactionID));
            LOG.error(ex.getMessage());
            sender.tell(new Status.Failure(ex), shard);
            return; 
        } 
 
        cohortEntry.setCanCommitSender(sender);
        cohortEntry.setShard(shard);
 
        if(currentCohortEntry != null) {
            // There's already a Tx commit in progress - attempt to queue this entry to be 
            // committed after the current Tx completes. 
            LOG.debug("Transaction {} is already in progress - queueing transaction {}", 
                    currentCohortEntry.getTransactionID(), transactionID);
 
            if(queuedCohortEntries.size() < queueCapacity) {
                queuedCohortEntries.offer(cohortEntry);
            } else { 
                removeCohortEntry(transactionID);
 
                RuntimeException ex = new RuntimeException(
                        String.format("Could not enqueue transaction %s - the maximum commit queue"+
                                      " capacity %d has been reached.", 
                                transactionID, queueCapacity));
                LOG.error(ex.getMessage());
                sender.tell(new Status.Failure(ex), shard);
            } 
        } else { 
            // No Tx commit currently in progress - make this the current entry and proceed with 
            // canCommit. 
            cohortEntry.updateLastAccessTime();
            currentCohortEntry = cohortEntry;
 
            doCanCommit(cohortEntry);
        } 
    } 
 
    private void doCanCommit(final CohortEntry cohortEntry) {
 
        try { 
            // We block on the future here so we don't have to worry about possibly accessing our 
            // state on a different thread outside of our dispatcher. Also, the data store 
            // currently uses a same thread executor anyway. 
            Boolean canCommit = cohortEntry.getCohort().canCommit().get();
 
            cohortEntry.getCanCommitSender().tell(
                    canCommit ? CAN_COMMIT_REPLY_TRUE : CAN_COMMIT_REPLY_FALSE, cohortEntry.getShard());
 
            if(!canCommit) {
                // Remove the entry from the cache now since the Tx will be aborted. 
                removeCohortEntry(cohortEntry.getTransactionID());
            } 
        } catch (InterruptedException | ExecutionException e) {
            LOG.debug("An exception occurred during canCommit", e);
 
            // Remove the entry from the cache now since the Tx will be aborted. 
            removeCohortEntry(cohortEntry.getTransactionID());
            cohortEntry.getCanCommitSender().tell(new Status.Failure(e), cohortEntry.getShard());
        } 
    } 
 
    /** 
     * Returns the cohort entry for the Tx commit currently in progress if the given transaction ID 
     * matches the current entry. 
     * 
     * @param transactionID the ID of the transaction 
     * @return the current CohortEntry or null if the given transaction ID does not match the 
     *         current entry. 
     */ 
    public CohortEntry getCohortEntryIfCurrent(String transactionID) {
        if(isCurrentTransaction(transactionID)) {
            return currentCohortEntry;
        } 
 
        return null; 
    } 
 
    public CohortEntry getCurrentCohortEntry() { 
        return currentCohortEntry;
    } 
 
    public CohortEntry getAndRemoveCohortEntry(String transactionID) {
        CohortEntry cohortEntry = cohortCache.getIfPresent(transactionID);
        cohortCache.invalidate(transactionID);
        return cohortEntry;
    } 
 
    public void removeCohortEntry(String transactionID) {
        cohortCache.invalidate(transactionID);
    } 
 
    public boolean isCurrentTransaction(String transactionID) {
        return currentCohortEntry != null &&
                currentCohortEntry.getTransactionID().equals(transactionID);
Stop searching for code, let great code find you!  Add Codota to your java IDE