protected <T> ICompletableFuture<T> getAndRemoveAsyncInternal(
      K key, boolean withCompletionEvent, boolean async) {
    final long start = System.nanoTime();
    ensureOpen();
    validateNotNull(key);
    CacheProxyUtil.validateConfiguredTypes(cacheConfig, key);
    final Data keyData = toData(key);
    final int completionId = withCompletionEvent ? nextCompletionId() : -1;
    ClientMessage request =
        CacheGetAndRemoveCodec.encodeRequest(nameWithPrefix, keyData, completionId);
    ClientInvocationFuture future;
    try {
      future = invoke(request, keyData, completionId);
      invalidateNearCache(keyData);
    } catch (Exception e) {
      throw ExceptionUtil.rethrow(e);
    }
    ClientDelegatingFuture delegatingFuture =
        new ClientDelegatingFuture<T>(
            future, clientContext.getSerializationService(), getAndRemoveResponseDecoder);
    if (async && statisticsEnabled) {
      delegatingFuture.andThen(
          new ExecutionCallback<Object>() {
            public void onResponse(Object responseData) {
              Object response = clientContext.getSerializationService().toObject(responseData);
              handleStatisticsOnRemove(true, start, response);
            }

            public void onFailure(Throwable t) {}
          });
    }
    return delegatingFuture;
  }
  protected Object getInternal(K key, ExpiryPolicy expiryPolicy, boolean async) {
    ensureOpen();
    validateNotNull(key);
    final Data keyData = toData(key);
    Object cached = getFromNearCache(keyData, async);
    if (cached != null) {
      return cached;
    }
    final Data expiryPolicyData = toData(expiryPolicy);
    ClientMessage request = CacheGetCodec.encodeRequest(nameWithPrefix, keyData, expiryPolicyData);
    ClientInvocationFuture future;
    try {
      final int partitionId = clientContext.getPartitionService().getPartitionId(key);
      final HazelcastClientInstanceImpl client =
          (HazelcastClientInstanceImpl) clientContext.getHazelcastInstance();
      final ClientInvocation clientInvocation = new ClientInvocation(client, request, partitionId);
      future = clientInvocation.invoke();
    } catch (Exception e) {
      throw ExceptionUtil.rethrow(e);
    }
    SerializationService serializationService = clientContext.getSerializationService();
    ClientDelegatingFuture<V> delegatingFuture =
        new ClientDelegatingFuture<V>(future, serializationService, cacheGetResponseDecoder);
    if (async) {
      if (nearCache != null) {
        delegatingFuture.andThenInternal(
            new ExecutionCallback<Data>() {
              public void onResponse(Data valueData) {
                storeInNearCache(keyData, valueData, null);
              }

              public void onFailure(Throwable t) {}
            });
      }
      return delegatingFuture;
    } else {
      try {
        Object value = delegatingFuture.get();
        if (nearCache != null) {
          storeInNearCache(keyData, (Data) delegatingFuture.getResponse(), null);
        }
        if (!(value instanceof Data)) {
          return value;
        } else {
          return serializationService.toObject(value);
        }
      } catch (Throwable e) {
        throw ExceptionUtil.rethrowAllowedTypeFirst(e, CacheException.class);
      }
    }
  }
  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;
    }
  }