| The code is as follows |
Copy Code |
|
<summary>
Cache Action Class
</summary>
public class Cache
{
private static list<string> _allusecachekey = new list<string> ();
private static ObjectCache _cache = Memorycache.default;
<summary>
Add cache
</summary>
<param name= "key" >key unique </param>
<param name= "Value" > Value </param>
<param name= "Cacheoffset" > Timeout </param>
public static void Add (string key, Object value, DateTimeOffset cacheoffset)
{
if (_allusecachekey.contains (key))
{
Remove (key);
}
_allusecachekey.add (key);
_cache. ADD (key, value, Cacheoffset);
}
///<summary>
/ Remove Cache
///</summary>
///<param name= "key" >KEY</PARAM>
Public static void Remove (String key)
{
if (_allusecachekey.contains (key))
{
_allusecachekey.remove (key);
}
_cache. Remove (key);
}
///<summary>
/ Read Cache
///</summary>
///<param name= "key" >KEY</PARAM>
///< RETURNS></RETURNS>
public static object Read (string key)
{
if (_allusecachekey.contains (key))
return _cache[key];
return null;
}
<summary>
Clear all Caches
</summary>
public static void Clear ()
{
foreach (string value in _allusecachekey)
{
_cache. Remove (value);
}
_allusecachekey.clear ();
}
}
|
The code is as follows
| The code is as follows |
Copy Code |
|
public class Sysuserstorageservice
{
private int _adminid;
Public sysuserstorageservice (int adminid)
{
_adminid = Adminid;
}
<summary>
Caching some content
</summary>
public void Storage ()
{
Tbl_admin user = new Tbl_admin ()//Get object based on actual requirement
Cache.Add (_adminid + "-admin-" + systemsetting.wms, user, systemsetting.offsettime);
Cache.Add (_adminid + "-lastlogintime-" + systemsetting.wms, DateTime.Now, systemsetting.offsettime);
}
Public Tbl_admin Getadmin
{
get {return (tbl_admin) cache.read (_adminid + "-admin-" + systemsetting.wms);}
}
public string Getlastlogintime
{
get {return (DateTime) cache.read (_adminid + "-lastlogintime-" + systemsetting.wms)). ToString ("Yyyy-mm-dd HH:mm:ss"); }
}
}
public class Systemsetting
{
<summary>
Cache definition
</summary>
Public Const string SHOPWMS = "Wms";
<summary>
Cache Timeout Time
</summary>
public static DateTimeOffset Offsettime = DateTimeOffset.Now.AddHours (24);
<summary>
Logon session
</summary>
Public Const string loginsession = "";
}
Sysuserstorageservice storage = new Sysuserstorageservice (Adminid);//call based on ID
Tbl_admin User = storage. Getadmin;
|
Page Caching
[OutputCache (Duration = VaryByParam = "Productno;brand;years")]
The above code declares the page cache using the @outputcatch directive, which is cached for 300 seconds. The @OutputCatch directive includes 10 properties that allow you to cache settings for different scenarios of the page, and the common properties are as follows:
CacheProfile: Gets or sets the outputCacheProfile name.
Duration: Gets or sets the time that a cache entry needs to remain in the cache.
VaryByHeader: Gets or sets a well delimited set of HTTP header names that are used to change cache entries.
Location: Gets or sets a value that determines the location of the cached item, including any, Clint, downstream, None, server, and ServerAndClient. The default value is any.
VaryByControl: Gets or sets a cluster of delimited control identifiers that are contained within the current page or user control to change the current cache entry.
Nostore: Gets or sets a value that determines whether the "Http cache-control:no-store" directive is set.
VaryByCustom: Gets the list of custom strings that the output cache uses to change cache entries.
Enabled: Gets or sets a value that indicates whether output caching is enabled for the current content.
VaryByParam: Gets a list of query strings or form post parameters.