/// <summary>
        /// Writes this instance to the specified writer.
        /// </summary>
        /// <param name="writer">The writer.</param>
        internal void Write(BinaryWriter writer)
        {
            // Make sure system marshaller is used.
            Debug.Assert(writer.Marshaller == BinaryUtils.Marshaller);

            writer.WriteInt((int)AtomicityMode);
            writer.WriteInt(Backups);
            writer.WriteInt((int)CacheMode);
            writer.WriteBoolean(CopyOnRead);
            writer.WriteBoolean(EagerTtl);
            writer.WriteBoolean(Invalidate);
            writer.WriteBoolean(KeepBinaryInStore);
            writer.WriteBoolean(LoadPreviousValue);
            writer.WriteLong((long)LockTimeout.TotalMilliseconds);
#pragma warning disable 618
            writer.WriteLong((long)LongQueryWarningTimeout.TotalMilliseconds);
#pragma warning restore 618
            writer.WriteInt(MaxConcurrentAsyncOperations);
            writer.WriteString(Name);
            writer.WriteBoolean(ReadFromBackup);
            writer.WriteInt(RebalanceBatchSize);
            writer.WriteLong((long)RebalanceDelay.TotalMilliseconds);
            writer.WriteInt((int)RebalanceMode);
            writer.WriteLong((long)RebalanceThrottle.TotalMilliseconds);
            writer.WriteLong((long)RebalanceTimeout.TotalMilliseconds);
            writer.WriteBoolean(SqlEscapeAll);
            writer.WriteInt(WriteBehindBatchSize);
            writer.WriteBoolean(WriteBehindEnabled);
            writer.WriteLong((long)WriteBehindFlushFrequency.TotalMilliseconds);
            writer.WriteInt(WriteBehindFlushSize);
            writer.WriteInt(WriteBehindFlushThreadCount);
            writer.WriteBoolean(WriteBehindCoalescing);
            writer.WriteInt((int)WriteSynchronizationMode);
            writer.WriteBoolean(ReadThrough);
            writer.WriteBoolean(WriteThrough);
            writer.WriteBoolean(EnableStatistics);
            writer.WriteString(DataRegionName);
            writer.WriteInt((int)PartitionLossPolicy);
            writer.WriteString(GroupName);
            writer.WriteObject(CacheStoreFactory);
            writer.WriteInt(SqlIndexMaxInlineSize);
            writer.WriteBoolean(OnheapCacheEnabled);
            writer.WriteInt(StoreConcurrentLoadAllThreshold);
            writer.WriteInt(RebalanceOrder);
            writer.WriteLong(RebalanceBatchesPrefetchCount);
            writer.WriteInt(MaxQueryIteratorsCount);
            writer.WriteInt(QueryDetailMetricsSize);
            writer.WriteInt(QueryParallelism);
            writer.WriteString(SqlSchema);
            writer.WriteBoolean(EncryptionEnabled);

            writer.WriteCollectionRaw(QueryEntities);

            if (NearConfiguration != null)
            {
                writer.WriteBoolean(true);
                NearConfiguration.Write(writer);
            }
            else
            {
                writer.WriteBoolean(false);
            }

            EvictionPolicyBase.Write(writer, EvictionPolicy);
            AffinityFunctionSerializer.Write(writer, AffinityFunction);
            ExpiryPolicySerializer.WritePolicyFactory(writer, ExpiryPolicyFactory);

            if (NodeFilter != null)
            {
                writer.WriteBoolean(true);

                var attributeNodeFilter = NodeFilter as AttributeNodeFilter;
                if (attributeNodeFilter == null)
                {
                    throw new NotSupportedException(string.Format(
                                                        "Unsupported CacheConfiguration.NodeFilter: '{0}'. " +
                                                        "Only predefined implementations are supported: '{1}'",
                                                        NodeFilter.GetType().Name, typeof(AttributeNodeFilter).Name));
                }

                attributeNodeFilter.Write(writer);
            }
            else
            {
                writer.WriteBoolean(false);
            }

            writer.WriteCollectionRaw(KeyConfiguration);

            if (PlatformCacheConfiguration != null)
            {
                writer.WriteBoolean(true);
                PlatformCacheConfiguration.Write(writer);
            }
            else
            {
                writer.WriteBoolean(false);
            }

            if (PluginConfigurations != null)
            {
                writer.WriteInt(PluginConfigurations.Count);

                foreach (var cachePlugin in PluginConfigurations)
                {
                    if (cachePlugin == null)
                    {
                        throw new InvalidOperationException("Invalid cache configuration: " +
                                                            "ICachePluginConfiguration can't be null.");
                    }

                    if (cachePlugin.CachePluginConfigurationClosureFactoryId != null)
                    {
                        writer.WriteBoolean(true);
                        writer.WriteInt(cachePlugin.CachePluginConfigurationClosureFactoryId.Value);

                        int pos = writer.Stream.Position;
                        writer.WriteInt(0);  // Reserve size.

                        cachePlugin.WriteBinary(writer);

                        writer.Stream.WriteInt(pos, writer.Stream.Position - pos - 4);  // Write size.
                    }
                    else
                    {
                        writer.WriteBoolean(false);
                        writer.WriteObject(cachePlugin);
                    }
                }
            }
            else
            {
                writer.WriteInt(0);
            }
        }
        /// <summary>
        /// Reads data into this instance from the specified reader.
        /// </summary>
        /// <param name="reader">The reader.</param>
        private void Read(BinaryReader reader)
        {
            // Make sure system marshaller is used.
            Debug.Assert(reader.Marshaller == BinaryUtils.Marshaller);

            AtomicityMode     = (CacheAtomicityMode)reader.ReadInt();
            Backups           = reader.ReadInt();
            CacheMode         = (CacheMode)reader.ReadInt();
            CopyOnRead        = reader.ReadBoolean();
            EagerTtl          = reader.ReadBoolean();
            Invalidate        = reader.ReadBoolean();
            KeepBinaryInStore = reader.ReadBoolean();
            LoadPreviousValue = reader.ReadBoolean();
            LockTimeout       = reader.ReadLongAsTimespan();
#pragma warning disable 618
            LongQueryWarningTimeout = reader.ReadLongAsTimespan();
#pragma warning restore 618
            MaxConcurrentAsyncOperations = reader.ReadInt();
            Name                            = reader.ReadString();
            ReadFromBackup                  = reader.ReadBoolean();
            RebalanceBatchSize              = reader.ReadInt();
            RebalanceDelay                  = reader.ReadLongAsTimespan();
            RebalanceMode                   = (CacheRebalanceMode)reader.ReadInt();
            RebalanceThrottle               = reader.ReadLongAsTimespan();
            RebalanceTimeout                = reader.ReadLongAsTimespan();
            SqlEscapeAll                    = reader.ReadBoolean();
            WriteBehindBatchSize            = reader.ReadInt();
            WriteBehindEnabled              = reader.ReadBoolean();
            WriteBehindFlushFrequency       = reader.ReadLongAsTimespan();
            WriteBehindFlushSize            = reader.ReadInt();
            WriteBehindFlushThreadCount     = reader.ReadInt();
            WriteBehindCoalescing           = reader.ReadBoolean();
            WriteSynchronizationMode        = (CacheWriteSynchronizationMode)reader.ReadInt();
            ReadThrough                     = reader.ReadBoolean();
            WriteThrough                    = reader.ReadBoolean();
            EnableStatistics                = reader.ReadBoolean();
            DataRegionName                  = reader.ReadString();
            PartitionLossPolicy             = (PartitionLossPolicy)reader.ReadInt();
            GroupName                       = reader.ReadString();
            CacheStoreFactory               = reader.ReadObject <IFactory <ICacheStore> >();
            SqlIndexMaxInlineSize           = reader.ReadInt();
            OnheapCacheEnabled              = reader.ReadBoolean();
            StoreConcurrentLoadAllThreshold = reader.ReadInt();
            RebalanceOrder                  = reader.ReadInt();
            RebalanceBatchesPrefetchCount   = reader.ReadLong();
            MaxQueryIteratorsCount          = reader.ReadInt();
            QueryDetailMetricsSize          = reader.ReadInt();
            QueryParallelism                = reader.ReadInt();
            SqlSchema                       = reader.ReadString();
            EncryptionEnabled               = reader.ReadBoolean();

            QueryEntities = reader.ReadCollectionRaw(r => new QueryEntity(r));

            NearConfiguration = reader.ReadBoolean() ? new NearCacheConfiguration(reader) : null;

            EvictionPolicy      = EvictionPolicyBase.Read(reader);
            AffinityFunction    = AffinityFunctionSerializer.Read(reader);
            ExpiryPolicyFactory = ExpiryPolicySerializer.ReadPolicyFactory(reader);

            NodeFilter = reader.ReadBoolean() ? new AttributeNodeFilter(reader) : null;

            KeyConfiguration = reader.ReadCollectionRaw(r => new CacheKeyConfiguration(r));

            if (reader.ReadBoolean())
            {
                PlatformCacheConfiguration = new PlatformCacheConfiguration(reader);
            }

            var count = reader.ReadInt();

            if (count > 0)
            {
                PluginConfigurations = new List <ICachePluginConfiguration>(count);
                for (int i = 0; i < count; i++)
                {
                    if (reader.ReadBoolean())
                    {
                        // FactoryId-based plugin: skip.
                        reader.ReadInt();                             // Skip factory id.
                        var size = reader.ReadInt();
                        reader.Stream.Seek(size, SeekOrigin.Current); // Skip custom data.
                    }
                    else
                    {
                        // Pure .NET plugin.
                        PluginConfigurations.Add(reader.ReadObject <ICachePluginConfiguration>());
                    }
                }
            }
        }