コード例 #1
0
  protected Object putIfAbsentInternal(
      final K key,
      final V value,
      final ExpiryPolicy expiryPolicy,
      final boolean withCompletionEvent,
      final boolean async) {
    final long start = System.nanoTime();
    ensureOpen();
    validateNotNull(key, value);
    CacheProxyUtil.validateConfiguredTypes(cacheConfig, key, value);
    final Data keyData = toData(key);
    final Data valueData = toData(value);
    final Data expiryPolicyData = toData(expiryPolicy);
    final int completionId = withCompletionEvent ? nextCompletionId() : -1;
    ClientMessage request =
        CachePutIfAbsentCodec.encodeRequest(
            nameWithPrefix, keyData, valueData, expiryPolicyData, completionId);
    ClientInvocationFuture future;
    try {
      future = invoke(request, keyData, completionId);
    } catch (Exception e) {
      throw ExceptionUtil.rethrow(e);
    }
    ClientDelegatingFuture delegatingFuture =
        new ClientDelegatingFuture<Boolean>(
            future, clientContext.getSerializationService(), putIfAbsentResponseDecoder);
    if (!async) {
      try {
        Object response = delegatingFuture.get();
        if (nearCache != null) {
          if (cacheOnUpdate) {
            storeInNearCache(keyData, valueData, value);
          } else {
            invalidateNearCache(keyData);
          }
        }
        if (statisticsEnabled) {
          handleStatisticsOnPutIfAbsent(start, (Boolean) response);
        }
        return response;
      } catch (Throwable e) {
        throw ExceptionUtil.rethrowAllowedTypeFirst(e, CacheException.class);
      }
    } else {
      if (nearCache != null || statisticsEnabled) {
        delegatingFuture.andThen(
            new ExecutionCallback<Object>() {
              @Override
              public void onResponse(Object responseData) {
                if (nearCache != null) {
                  if (cacheOnUpdate) {
                    storeInNearCache(keyData, valueData, value);
                  } else {
                    invalidateNearCache(keyData);
                  }
                }
                if (statisticsEnabled) {
                  Object response = clientContext.getSerializationService().toObject(responseData);
                  handleStatisticsOnPutIfAbsent(start, (Boolean) response);
                }
              }

              @Override
              public void onFailure(Throwable t) {}
            });
      }
      return delegatingFuture;
    }
  }
コード例 #2
0
 @Override
 public <T> T decodeClientMessage(ClientMessage clientMessage) {
   return (T) Boolean.valueOf(CachePutIfAbsentCodec.decodeResponse(clientMessage).response);
 }