Data in NCache
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 and NCache specific objects as the value of each key. Each object added to the cache must have a unique string key associated with it.
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
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".
Supported Data Types in Cache
The objects stored in the cache can be:
1. Primitive data types
NCache supports all .NET primitive data types:
2. Custom class objects
Data can also be any custom serializable class object, for example, Product class objects. The custom object data must 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.
3. 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.