Cache Keys and Data Caching Overview
NCache uses an enhanced key-value structure for storing objects and data caching. So, while conventional key-value stores contain a string key against a string value, NCache allows primitive data types, custom objects, NCache-specific objects (CacheItems), data structures, and JSON as values of each key. Therefore, each item added to the cache must have a unique string key.
Note
This feature is also available in NCache Professional.
Cache Keys in Data Caching
This key-value structure is advantageous for retrieving objects from the cache. While NCache supports multiple cache data retrieval methods - like querying and tagging - keys efficiently fetch the associated items without executing queries over the entire cache.
Keys in NCache have the following properties:
- Unique - no duplicate keys are allowed
- String-based only
- Valid string - null/empty strings are not allowed
- Case sensitive
Tip
Good practices for naming keys:
- Provide meaningful names to your keys that describe the associated data. For example, you are likely to forget what "key1" is associated with compared to a key named Product:1001. This key name describes itself as a Product item with ProductID 1001.
- For multiple classes, you can add a prefix before the key name to mark the key for the class. For instance, your data is such that Product and Customer objects can have "1001" as the key. These can be in the format [ClassName]_[KeyName] so that they are Product_1001 and Customer_1001 for clarification.
- For key names with multiple words, you can use any character to make it readable, a few common ones being
_ , & : - =
. For example, Product_Item:1001.
Location Affinity between Items
NCache provides a mechanism to store data in the cache that creates an affinity between items of different classes. You can store items of different types on the same node to save the matching cost while fetching items with similar keys. This mechanism maps similar entries at the back end, thus further speeding up data caching and retrieving.
Location affinity can be enabled manually while entering data into the cache. The item key must be the same inside braces {}
as in the other item. For example, to create location affinity between Product and Order objects, you can specify key Product:1001 for a Product object in the cache and then create an affinity of the associated order by specifying Product:1001 within {}
. So the Order object key can be specified as Order_{Product:1001}. This will ensure that the product and orders exist within the same node.
Supported Data in Data Caching
The objects stored in the cache for data caching can be:
Primitive data types
Note
This feature is also available in NCache Professional.
NCache supports the following primitive data types:
- byte/sbyte
- int/uint
- short/ushort
- long/ulong
- char
- float
- double
- decimal
- bool
The non-primitive data types include:
- object
- string
- DateTime
- TimeSpan
Custom class objects
Note
This feature is also available in NCache Professional.
Data can also be any custom serializable class object, for example, Product class objects. The custom object data must be serializable, otherwise NCache will throw a Serialization exception.
NCache provides two ways of serializing custom objects:
Native Serialization: You can use the .NET provided [Serializable] attribute in your custom class.
Compact Serialization: NCache provides a custom serialization framework for custom objects. This framework provides cost-effective serialization for registered classes dynamically.
NCache's CacheItem objects
Note
This feature is also available in NCache Professional.
Data can also be encapsulated in NCache's CacheItem class. CacheItem allows you to add additional metadata along with the value being cached. This metadata defines the properties of the item like expiration, dependencies, and more.
You can read more about the properties of CacheItem here.
Data Structures
NCache now provides exclusive support for adding/updating data structures, where the data is manipulated directly over the server, improving performance. NCache provides support for the following data structures:
Just like any data in NCache, these can be queried, locked, expired, and more. To learn more about using data structures in cache, refer to Data Structures in Cache.
JSON
NCache also supports adding/modifying cache data in the form of JSON. JSON provides flexibility to data retrieval as custom objects can be retrieved as JSON and JSON objects can be retrieved as custom objects. The following are supported for JSON:
Similar to CacheItems, JSON data can be queried, locked, expired, and more. To learn more about using JSON in NCache, refer to Cache Data as JSON.
See Also
How to Connect to Cache
Add Data to Cache
Retrieve Existing Cache Data
Remove Data from Cache
Create Cache