Recently done a C # project, just finished the basic data, now the caching mechanism for everyone to share:
The students who have done the basic data should know that every time it comes to querying data, it will go to the database to query out the basic data of the configuration, so that each operation is slow. Then every time we open a new page will go to the database to query the basic data, so that the database will be frequently connected, the user will be particularly slow after the more. Because once the data is properly set, it is seldom changed, so we can make a cache to cache the underlying data for each query, thus improving query efficiency. Well, nonsense not much to say, on the code.
I declare that I am using the dictionary data dictionary, you can also use the cache and other techniques, the implementation mechanism is the same.
I used an interface, an abstract class, a factory class, a generic class.
First we have to define the cache, get the cache, delete the cache method, but also to define the size of the cache, too big will not control, consumption of memory. Define the method in the interface, and then inherit the interface implementation method with the abstract class, because the cache is kept in the entire Web site during the run, so it cannot be instantiated, we have to use the abstract class, the Factory mode to implement the call. One thing to be aware of is that we can define the type in dictionary as a generic class instead of the object type, so you don't have to box and unboxing when you use it.
The following is the interface Icache code:
<summary>
Setting up the cache
</summary>
<param name= "Key" ></param>
<param name= "Value" ></param>
void Setcache (TKey key, TValue value);
<summary>
Get Cache
</summary>
<param name= "Key" ></param>
<returns></returns>
TValue GetCache (TKey key);
<summary>
Emptying the specified cache
</summary>
void Clear (TKey key);
<summary>
Empties the cache for the currently logged-on user
</summary>
<param name= "Key" ></param>
void ClearAll ();
<summary>
Define Cache Key
</summary>
<param name= "Key" ></param>
<returns></returns>
TKey Joinkey (TKey key);
<summary>
Cache size
</summary>
int CacheSize {get; set;}
Next, we use an abstract class (Abstractcache) to inherit the interface, most of which are virtual methods, because we use it to consider that there are other caches, so you can override these methods in the inheriting class.
Public Abstractcache (string type)
{
This._type = type;
if (Abstractcache<tkey, Tvalue>._cachelist = = null)
{
Abstractcache<tkey, tvalue>._cachelist = new dictionary< String, Dictionary<tkey, tvalue>> ();
}
if (! Abstractcache<tkey, Tvalue>._cachelist.containskey (_type))
{
Abstractcache<tkey, Tvalue>._cachelist.add (This._type, New Dictionary<tkey, tvalue> ());
}
}
private static dictionary<string, Dictionary<tkey, tvalue>> _cachelist;
private string _type;
Private int _cachesize = +;
Public virtual void Setcache (TKey key, TValue value)
{
Dictionary<tkey, tvalue> dic = This.gettypecache ();
Lock (Abstractcache<tkey, tvalue>._cachelist)
{
TKey FullKey = this. Joinkey (key);
if (!dic. ContainsKey (FullKey))
{
if (dic. Count >= this._cachesize)
{
if (dic. Keys.count > 0)
{
TKey tmpkey = default (TKey);
foreach (TKey k in DiC. Keys)
{
Tmpkey = k;
break;
}
DiC. Remove (Tmpkey);
}
}
if (value! = null)
{
DiC. ADD (FullKey, value);
}
else
{
DiC. ADD (FullKey, Default (TValue));
}
}
else
{
if (value = null)
{
Dic[fullkey] = value;
}
else
{
Dic[fullkey] = default (TValue);
}
}
}
}
Public virtual TValue GetCache (TKey key)
{
Dictionary<tkey, tvalue> dic = This.gettypecache ();
TKey FullKey = this. Joinkey (key);
if (DIC. ContainsKey (FullKey))
{
return Dic[fullkey];
}
return default (TValue);
}
public virtual void Clear (TKey key)
{
Dictionary<tkey, tvalue> dic = This.gettypecache ();
Lock (Abstractcache<tkey, Tvalue>._cachelist)
{
TKey FullKey = this. Joinkey (key);
if (DIC. ContainsKey (FullKey))
{
Dic. Remove (FullKey);
}
}
}
public virtual void ClearAll ()
{
throw new NotImplementedException ();
}
Public abstract TKey Joinkey (TKey key);
public int CacheSize
{
Get
{
return _cachesize;
}
Set
{
_cachesize = value;
}
}
<summary>
Gets the current type of cached data
</summary>
<returns></returns>
Protected Dictionary<tkey, tvalue> Gettypecache ()
{
Dictionary<tkey, tvalue> dic = Abstractcache<tkey, tvalue>._cachelist[this._type];
if (dic = = null)
{
throw new Exception ("Incorrect initialization mode");
}
return dic;
}
Next we need to top a Dataauthcache class to inherit the abstract class to rewrite some of the methods we need,
Public Dataauthcache ()
: Base ("Dataauthcache") //This parameter is the that distinguishes the cache from that category;
{
}
private static Object _lockobj = new Object ();
public override String Joinkey (String entityname)
{ &NBSP
Return Entity.Session.Manage.Current.AuthContext.OrgID + "_" + Entity.Session.Manage.Current.AuthContext.UserID + "_" + entityname;
}
public override void ClearAll ()
{
long orgkey = entity.session.manage.current.authcontext.orgid;
Long UserKey = entity.session.manage.current.authcontext.userid;
Lock (_lockobj)
{
dictionary<string, string> dic = This.gettypecache ();
String containkey = Orgkey + "_" + USERKEY;&NBSP;
list<string> keys = new list<string> ();
//dictionary The keys cannot access directly by index;
foreach (String Key in DiC. Keys)
{
keys. ADD (key);
}
if (keys. Count > 0)
{
foreach (string key in keys)
{
if (key. IndexOf (Containkey) > 0)
{
Dic. Remove (key);
}
}
}
}
}
We can then define a factory class (abstractcachefactory) to implement external calls
private static icache<string, string> _dataauthcache = null;
public static abstractcache<string, string> Getdataauthcache ()
{
if (_dataauthcache = = null)
{
_dataauthcache = new Dataauthcache ();
}
Return (abstractcache<string, string>) _dataauthcache;
}
Okay, here we go, we're done with the cache, it's easy!
Abstractcache<string, String> DAC =abstractcachefactory.getdataauthcache ();
String cachehql = DAC. GetCache (TPL. EntityName);
CACHEHQL is our cache of data query fragments, well, share here. You are welcome to put forward your valuable opinions and share the good ways.
C # underlying data cache