NCache 4.6 - Online Documentation


The purpose of using an index is to optimize speed and performance in finding relevant data for a search. For searching purposes, all searchable attributes should be indexed. Without an index, the search would scan the whole data, which would require considerable time and computing power. The additional computer storage required to store the index, as well as the considerable increase in the time required for an update to take place are traded off for the time saved during information retrieval.
NCache provides its own indexing mechanism. You can select objects in Jar assemblies that you want to index. NCache uses Jar Reflection to extract data from the indexed attributes and builds its internal index. When you insert data in the cache which should be indexed, NCache creates an index tree and adds the associated keys in them. These indexes then later on contain all the keys from the cache associated with them. When you query the cache, NCache uses these indexes to quickly find the desired objects and returns all related keys or items.
NCache internally uses indexing in different modules to boost searching on cache performance and keeps track of it for different purposes:
Cache Searching Index
For searching purposes, you only have to index all the searchable attributes. NCache will not search the non-indexed attribute from the cache because it has to traverse through whole cache to find related data which will affect the cache performance. Indexes are defined against the actual data types of the cached items. Through indexing, NCache knows what kind of data will be stored and it will store it in a searchable way.
NCache provides two ways to define searching indexes:
1. Pre-Defined Index (Static Index)
To search custom class objects like Product in a cluster cache, these indexes need to be defined in advance. In this way, cache will save that type of data in the form where it can be easily fetched through queries. Indexes can be configured for public, private and protected primitive data members (including string).
These kind of indexes are defined before cache starts. To define new indexes, the cache needs to be stopped first and restarted after modifications for deploying changes. On insertion of the indexed type of data in cache, NCache automatically detects its type and it can be used  in searching queries afterwards.
2. Runtime Index (Dynamic Index)
If there are attributes in an application that are defined and used at runtime, then runtime indexing should be used. For example, there may be some calculated values assigned to a  product related to its sales. When any product is purchased by a customer, the sale's details are saved in the cache for generating daily report of sales trend through cached values later. This value is defined at runtime so it is the runtime attribute of the product.
NCache will automatically create indexes for data that is added by the cache client. It means that indexes do not need to be configured before using them for runtime attributes.
Runtime index can be further categorized into following types:
i. Tag Index
One or more identification marks can be associated with cache items. These identification marks are called tags. Through tags, the user can associate keyword(s) with cache items. Collection of cached items can be found and removed from the cache by specifying tags too. Tags are only allowed in string format. For every new tag, a new tag entry will be created in the tag index and all related cached items will be associated with that index.
ii. Named Tag Index
If there is a need to use high level of tagging where tags can have different data types or names, and the requirement is to query data related to specific type of tag, then Named Tags should be used.
"Named Tags" is the enhancement of Tags. Named tags allows the user to store additional information (of any type) required to query the object stored as string. For example, "Highest-Sale-Time" Named Tag with value of time can be stored when any product's sale is the highest in whole day. Named tags allow indexing the data according to required attributes at runtime. Later on, a query can be constructed using named tags to fetch the desired result set. Multiple named tags can be associated with one cached item and vice versa.
When a data is added with a named tag that does not exist in cache, a new index is created for this new named tag. All the related data will be assigned to that index for searching. Unlike Tag, Named Tag index can be defined for all primitive types, String and DateTime. So there is more flexibility to add wide range of searchable data with different data types. 
If there are multiple applications that are sharing the same cache and all of them are supposed to add named tags, then make sure that the same named tags have homogenous data types, e.g., if one client is adding named tag "ProductID" with String data type, then all other clients should add values of "ProductID" in String format not in Integer or other for the same cache.
Indexing Attributes of the Cached Item
Following example demonstrates the procedure for defining indexes on attributes. Suppose that the cache contains a Product object where the definition of Product is as below:
    public class Product
        private int productID;
        private string productName;
        /// <summary>
        /// Get/Set the ProductID.
        /// </summary>
        public int ProductID
            get { return productID; }
            set { productID = value; }
        /// <summary>
        /// Get/Set the Product name.
        /// </summary>
        public string ProductName
            get { return productName; }
            set { productName = value; }
See Also