Cache fragment 2: cache Fragment
How can I cache a lot of data but only read a portion of it?
For example, 100 pieces of data are saved in the cache, but paging data is read. For example, only 10 pieces of data are read on each page. In this case, the 100 pieces of data can be split into 10 records and saved separately. The key of each cache item must be specially designed, for example, 01-10-products, 02-10-products ......
How can we avoid caching unnecessary data?
For example:
public class Teacher
{ public int Id{get;set;} public string Name{get;set;} public Department Department{get;set;}}
public class Department
{ public int Id{get;set;} public string Name{get;set;}}
Here, we only want to cache the Teacher, but if we use. NET default serialization mechanism, that is, the Serializable feature is applied to the Teacher Class. When the Teacher Class is serialized, the class Department corresponding to the reference attribute Department is also serialized.
To solve this problem, add the [NonSerialized] feature to the class that does not need to be serialized, or implement the ISerializable interface to customize the serialization logic.
How can I read the same cache item through different keys?
For example, cache products sometimes store product names as keys, and sometimes get products from the cache through a SET index. At this time, we can connect different forms of keys into strings as keys.
var product = GetProduct();
cache["prod_1"] = product;
......
var cacheProduct = cache["prod_1"];
Reference: The "DotNet" public account of Wang Yang.