• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Show / Hide Table of Contents
  • Programmer's Guide
  • Client Side API Programming
    • Setting Up Development Environment
    • Basic Cache Operations
      • Initialize Cache
      • Add Data to Cache
      • Update Data in Cache
      • Fetch Data From Cache
      • Remove Data From Cache
      • Dispose Cache
    • Bulk Operations
      • Adding Collection to Cache
      • Updating Collection in Cache
      • Retrieving Collection from Cache
      • Removing Collection from Cache
      • Deleting Collection from Cache
    • Asynchronous Operations
      • Using Asynchronous Operations
      • Using Asynchronous Operations with Callback Methods
    • Groups and Subgroups
      • Adding/Updating Data Group in Cache
      • Retrieving Data Group from Cache
      • Removing Data Group from Cache
    • Tagging Data in NCache
      • Creating Tags
      • Adding Items with Tags
      • Retrieving Previously Tagged Data
      • Removing Tagged Items from Cache
    • Named Tags
    • Data Expiration Strategies
      • Using Absolute Expiration
      • Using Sliding Expiration
    • Cache Dependencies
      • Key Dependency
      • File Dependency
      • Notification based Dependencies
        • Database Dependency using SQL Server
        • Database Dependency using Oracle
      • Polling Based Dependency
      • Custom Data Source Dependency
      • Multiple Cache Sync Dependency
      • Aggregate Dependency
      • Add Dependency to Existing Item
      • Using CLR Procedures to Call NCache
    • Locking Data in NCache
      • Locking Items in Cache (Pessimistic Locking)
      • Locking Items with Cache Item Versioning (Optimistic Locking)
    • SQL Reference for NCache
      • SQL Syntax
      • Querying Samples for Operators
      • Querying Data in NCache
      • NCache Language Integrated Query (LINQ)
        • Using LINQ in NCache
        • Configuring LINQPad for NCache
        • Querying NCache Data in LINQPad
    • Event Notifications
      • Cache Level Event Notifications
      • Item Level Event Notifications
      • Custom Event Notifications
    • Publish/Subscribe (Pub/Sub) in NCache
      • Pub/Sub Topics
      • Managing Topics
      • Pub/Sub Messages
        • Message Behavior and Properties
        • Creating a Message
      • Publish Messages to Topic
      • Subscribe for Topic Messages
      • Monitoring Pub/Sub Topics
    • Continuous Query
    • Using Streams in NCache
      • Opening with Stream Modes
      • Adding and Updating Data with Streams
      • Retrieving Data from Streams
      • Closing a Stream
    • Security and Encryption
      • NCache Security
      • NCache Data Encryption
    • Data Compression
    • NCache Management API
  • Server Side API Programming
    • Cache Startup Loader
      • Components of Cache Startup Loader
      • Sample Implementation of ICacheLoader on Single Node
      • Sample Implementation of ICacheLoader with Distribution Hints
    • Data Source Providers (Backing Source)
      • Read-Through Caching
        • Configure Read-Through Provider
        • Using Read-Through with Cache Operations
      • Write-Through Caching
        • Configuring Write-Through Provider
        • Using Write-Through with Basic Operations
        • Using Write-Behind with Basic Operations
        • Using Write-Behind with Bulk Operations
        • Using Write-Behind with Async Operations
        • Monitor Write-Through Counters
    • Custom Dependency
      • Sample Implementation of Custom Dependency
      • Sample Usage of Custom Dependency
    • WAN Replication through Bridge
      • Bridge Configurations
      • Implementing Bridge Conflict Resolver
    • Entry Processor
      • Sample Implementation of IEntryProcessor Interface
      • Sample Usage of EntryProcessor
    • MapReduce
      • Sample Implementation of MapReduce Interfaces
      • Sample Usage of MapReduce
    • Aggregator
      • Sample Implementation of IValueExtractor Interface
      • Sample Implementation of IAggregator Interface
      • Sample Usage of Aggregator
    • Dynamic Compact Serialization
  • Client Side ASP.NET Features
    • ASP.NET
      • ASP.NET Session State Provider for NCache
      • Multi-Region ASP.NET Session State Provider for NCache
    • ASP.NET Core
      • Session Storage in ASP.NET Core
        • Configure NCache ASP.NET Core Session Provider
        • Configure ASP.NET Core Sessions with NCache IDistributedCache Provider
      • Multi-Region ASP.NET Core Session Provider for NCache
      • Object Caching in ASP.NET Core
    • ASP.NET SignalR
      • Using NCache Extension for SignalR
    • View State Caching
      • Configuring and Using Content Optimization
      • Group View State with Sessions
      • Limit View State Caching
      • Perform Page Level Grouping for View State
    • ASP.NET Output Cache
      • Configure ASP.NET Output Caching
      • Using ASP.NET Output Cache with Custom Hooks
  • Client Side Third Party Integrations
    • Migrating AppFabric to NCache
      • AppFabric API vs. NCache API
    • NHibernate
      • NCache as NHibernate Second Level Cache
      • Using NHibernate Query Caching
      • Configuring Database Synchronization with NHibernate
    • Entity Framework Caching Integration
      • NCache as Entity Framework Second Level Cache
      • Entity Framework Caching Config File
    • Entity Framework Core Caching
      • Installing NCache Entity Framework Core Provider
      • Configuring NCache Entity Framework Core Provider
      • Using NCache Entity Framework Core Provider
        • Caching Options for EF Core Provider
        • LINQ APIs for EF Core Provider
        • Cache Only APIs for EF Core Provider
        • Query Deferred APIs for EF Core Provider
      • Logging in NCache Entity Framework Core Provider
    • Memcached
      • NCache Memcached Gateway Approach
      • Memcached Client Plugin for .NET
    • Debug NCache Providers in Visual Studio
    • NCache for Visual Studio Extension

Dynamic Compact Serialization

NCache stores application data in serialized form - it is serialized on the cache client before being added to the cache. Therefore, the data you add to the cache must also be marked as serializable. It supports native binary serialization for NCache client and it also has its own serialization framework called compact serialization which is fast and compact rather than native serialization. In native serialization of Java, all data of an object along with the entire type of information is serialized. In this way, the size of the object increases, whereas in compact serialization, the entire type of information does not travel on the network, and the object size which travels on the network is reduced. As the object size decreases, network trips become faster and eventually cost decreases hence increasing the overall application performance.

Compact serialization is also binary serialization which has the following benefits:

Compact Data Size

In NCache binary data, size of the serialized object is compact. In certain scenarios, data size may be 10 times less than that of native serialization. Smaller serialized objects take less time to travel on the network which thus results in higher throughput. This also helps in reducing memory consumption on the cache’s server. In this manner, NCache achieves better performance - reducing time and cost. The small object size and fast network traffic increases the application performance.

Faster than native serialization

Compact serialization is faster than native serialization as it does not rely as much on reflection as native serialization does. Native serialization uses reflection for attribute information. This reflection is avoided in NCache’s compact serialization. Type information for compact types is loaded only once when the cache is initialized, which reduces the overhead faced using native serialization. Benchmarks have shown that in certain used cases it is 5~10 times faster than native serialization.

No Code Change

There is no code change required to make a class compact serializable. The only step required is to configure your classes through management tools (NCache Manager or command line tools).

  • Selective Serialization: NCache allows selective serialization; you can select data members of a class which you want to mark as non-serializable. The excluded attribute of any cached object will either be null or will have a default value if assigned. This feature is useful in a scenario where for certain attributes, there is no need for caching, for example, connections object for database. By this selective serialization the process becomes even more efficient and more compact.

  • Granular Control over Compact Serialization: For this, you can implement ICompactSerializable. This interface gives you more fine grained control over compact serialization, you can control which data member to serialize and which to exclude according to your business needs.

  • Support for Native Serialization: NCache supports native binary serialization. Data is stored in serialized form in cache, client native serialization is supported for both .NET and Java clients of NCache.

To use Compact Serialization, object types that are to be compact serialized need to be configured through NCache Manager. NCache internally handles serialization/deserialization of registered classes and the user does not need to explicitly code for it.

Important

Class attributes need to be deserialized in the same order in which they have been serialized otherwise the serialized stream cannot be deserialized.

However, NCache also allows to explicitly implement ICompactSerializable interface.

To utilize the ICompactSerializable interface, include the following namespace in your application: Alachisoft.NCache.Runtime.GenericTypesProvider.

To implement this interface, the custom object class needs to implement Serialize/Deserialize methods which will be called by NCache while serializing/deserializing this object. Sample code for implement this interface is given below:

//Sample code for custom ICompactSerializable class
public class CustomObject : ICompactSerializable
{
    private string _attribute1;
    private int _attribute2;
    private bool _attribute3;

    public CustomObject()
    { }

    public CustomObject(string attrib1, int attrib2, bool attrib3)
    {
        _attribute1 = attrib1;
        _attribute2 = attrib2;
        _attribute3 = attrib3;
    }

    #region ICompactSerializable Members

    public void Deserialize(Alachisoft.NCache.Runtime.Serialization.IO.CompactReader reader) {
            _attribute1 = reader.ReadObject() as string;
            _attribute2 = reader.ReadInt32();
            _attribute3 = reader.ReadBoolean();
    }

    public void Serialize(Alachisoft.NCache.Runtime.Serialization.IO.CompactWriter writer){
            writer.WriteObject(_attribute1);
            writer.Write(_attribute2);
            writer.Write(_attribute3);
    }

    #endregion
}

Implementing a Generic Type Handler

NCache also allows registering generic types in Compact Serialization. For this, each type needs to be registered through NCache Manager. There is another way for registering generic compact types through "Generic Type Handler". With Generic Type Handler, the user can add all the generic types at once by implementing the IGenericTypeProvider interface. This saves a lot of time and effort in case the user wants to register a large number of types.

To utilize the interface, include the following namespace in your application: Alachisoft.NCache.Runtime.GenericTypesProvider.

To implement this, the user needs to implement GetGenericTypes method and return the desired generic types in Type array. After this, deploy this class assembly to NCache Manager. NCache Manager will register all types populated in Type array. Sample code is given below:

public class CustomGenericType<T, K, V>
{}
public class SetGenerics : IGenericTypeProvider
{
    Type[] types = new Type[6];

    #region IGenericTypeProvider Members

    Type[] IGenericTypeProvider.GetGenericTypes()
    {
        types[0] = typeof(Dictionary<string, Customer>);
        types[1] = typeof(List<int>);
        types[2] = typeof(CustomGenericType<Employee, Product, Customer>);
        types[3] = typeof(CustomGenericType<Employee, Product, int>);
        types[4] = typeof(CustomGenericType<Employee, string, byte>);
        types[5] = typeof(CustomGenericType<string, Customer, bool>);

        return types;
    }
    #endregion

Troubleshooting

Compact Serialization Exception

A Serialization exception may occur on the node where Compact Serializable types are not registered.

Workaround

Register Compact Serializable types on all the nodes in the cluster.

Back to top Copyright © 2017 Alachisoft