@Override
  public V get(Object key) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
    initNearCache();
    if (nearCache != null) {
      Object cached = nearCache.get(key);
      if (cached != null) {
        if (cached.equals(NULL_OBJECT)) {
          return null;
        }
        return (V) cached;
      }
    }

    Data keyData = toData(key);
    ClientMessage request = ReplicatedMapGetCodec.encodeRequest(name, keyData);
    ClientMessage response = invoke(request, keyData);

    ReplicatedMapGetCodec.ResponseParameters result =
        ReplicatedMapGetCodec.decodeResponse(response);

    V value = (V) toObject(result.response);
    if (nearCache != null) {
      nearCache.put(key, value);
    }
    return value;
  }
  /** Adds near cache stats. */
  protected void addNearCacheStats(
      LocalMapStatsImpl stats,
      LocalMapOnDemandCalculatedStats onDemandStats,
      MapContainer mapContainer) {
    if (!mapContainer.getMapConfig().isNearCacheEnabled()) {
      return;
    }
    NearCache nearCache = nearCacheProvider.getOrCreateNearCache(mapContainer.getName());
    NearCacheStats nearCacheStats = nearCache.getNearCacheStats();
    long nearCacheHeapCost = mapContainer.getNearCacheSizeEstimator().getSize();

    stats.setNearCacheStats(nearCacheStats);
    onDemandStats.incrementHeapCost(nearCacheHeapCost);
  }
 @Override
 protected void onDestroy() {
   if (nearCache != null) {
     removeNearCacheInvalidationListener();
     nearCache.destroy();
   }
 }
 private void removeInvalidationListener() {
   if (nearCache != null && nearCache.isInvalidateOnChange()) {
     String registrationId = nearCacheMembershipRegistrationId;
     if (registrationId != null) {
       clientContext.getListenerService().deregisterListener(registrationId);
     }
   }
 }
 private void registerInvalidationListener() {
   if (nearCache != null && nearCache.isInvalidateOnChange()) {
     Client client = clientContext.getClusterService().getLocalClient();
     EventHandler handler = new NearCacheInvalidationHandler(client);
     ListenerMessageCodec listenerCodec = createInvalidationListenerCodec();
     nearCacheMembershipRegistrationId =
         clientContext.getListenerService().registerListener(listenerCodec, handler);
   }
 }
 @Override
 public void destroy() {
   if (nearCache != null) {
     removeInvalidationListener();
     nearCacheManager.destroyNearCache(nearCache.getName());
   }
   if (statisticsEnabled) {
     statistics.clear();
   }
   super.destroy();
 }
  protected void doListNearCaches() {
    NearCacheManager nearCacheManager = createNearCacheManager();

    Set<String> nearCacheNames = new HashSet<String>();

    Collection<NearCache> nearCaches1 = nearCacheManager.listAllNearCaches();
    assertEquals(0, nearCaches1.size());

    for (int i = 0; i < DEFAULT_NEAR_CACHE_COUNT; i++) {
      String nearCacheName = DEFAULT_NEAR_CACHE_NAME + "-" + i;
      createNearCache(nearCacheManager, nearCacheName);
      nearCacheNames.add(nearCacheName);
    }

    Collection<NearCache> nearCaches2 = nearCacheManager.listAllNearCaches();
    assertEquals(DEFAULT_NEAR_CACHE_COUNT, nearCaches2.size());

    for (NearCache nearCache : nearCaches2) {
      assertTrue(nearCacheNames.contains(nearCache.getName()));
    }
  }
  protected AbstractClientInternalCacheProxy(
      CacheConfig cacheConfig,
      ClientContext clientContext,
      HazelcastClientCacheManager cacheManager) {
    super(cacheConfig, clientContext);
    this.cacheManager = cacheManager;
    this.nearCacheManager = clientContext.getNearCacheManager();
    this.asyncListenerRegistrations =
        new ConcurrentHashMap<CacheEntryListenerConfiguration, String>();
    this.syncListenerRegistrations =
        new ConcurrentHashMap<CacheEntryListenerConfiguration, String>();
    this.syncLocks = new ConcurrentHashMap<Integer, CountDownLatch>();

    initNearCache();

    if (nearCache != null) {
      this.statistics =
          new ClientCacheStatisticsImpl(System.currentTimeMillis(), nearCache.getNearCacheStats());
    } else {
      this.statistics = new ClientCacheStatisticsImpl(System.currentTimeMillis());
    }
    this.statisticsEnabled = cacheConfig.isStatisticsEnabled();
  }
 protected void invalidateNearCache(Data key) {
   if (nearCache != null) {
     nearCache.remove(key);
   }
 }
 protected void storeInNearCache(Data key, Data valueData, V value) {
   if (nearCache != null && valueData != null) {
     Object valueToStore = nearCache.selectToSave(value, valueData);
     nearCache.put(key, valueToStore);
   }
 }