Cache Keys and Data Overview
NCache uses an enhanced key-value structure for storing objects. This implies that while conventional key-value stores contain a string key against a string value, NCache allows adding primitive data types, custom objects NCache specific objects (CacheItems), data structures and JSON as the value of each key. Each object added to the cache must have a unique string key associated with it.
Cache Keys
This key-value structure is particularly useful for retrieving objects from cache. While NCache supports multiple ways of retrieving data from cache, like querying and tagging, keys act as a quick and effective way to 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 data associated with them. For e.g, you are likely to forget what "key1" is associated with compared to a key named "Product:1001". This key name describes that the key belongs to 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 e.g, your data is such that Product and Customer objects can have "1001" as key. These can be named 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 e.g, "Product_Item:1001".
Location Affinity between Items
Note
This feature is only available in NCache Enterprise Edition.
NCache provides a mechanism to store data in the cache in such a way that an affinity is created 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 fetch operations.
Location affinity can be enabled manually while entering data into the cache. The key of an item has to be exactly the same inside braces {}
as in the other item which you want to create affinity with. For example, to create location affinity between Product and Order objects, you can specify key "Product:1001" for a Product object in cache, and then create 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.
To learn how to use Location Affinity while adding data to cache, refer to Add Data with Location Affinity.
Supported Data in Cache
The objects stored in the cache can be:
Primitive data types
NCache supports all .NET primitive data types:
byte/sbyte | int/uint | short/ushort | long/ulong | object |
char | string | float | double | decimal |
bool | DateTime | TimeSpan |
Custom class objects
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.
Dynamic 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
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
Note
This feature is only available in NCache Enterprise Edition.
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:
- List
- Queue
- Set
- Dictionary
- Counter
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
Note
This feature is only available in NCache Enterprise Edition.
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 is supported for JSON:
- JSONObject
- JSONArray
- JSONValue
- JSONNull
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