Implementation of the cache container class (C #)

Source: Internet
Author: User
I have recently compiled an O/RM component (of course there are still quite a few functions ).
Everyone should be clear about updating the object Database Must undergo a series of transformations; SQL Statement generation is more cost-effective, because there are too many things in the middle.
During the design process, I thought that if an object is inserted into the database, the corresponding command will be saved in the cache; the next time you perform this operation on the same type of object, check that the cache will be used directly if it is available, which is more efficient.
With this idea, I started to design it (but I still tried it for the first time, after all ).
Because the processing of objects in the cache is complicated, there is a sharing problem in multithreading. If two threads call the same command at the same time, a processing error will occur!
To better control the sharing of command objects, a persistent interface is defined for command objects.
After a period of design and writing, it's a bit fruitful. Just share what you have done.

The following are the test cases of components:
P4 2.4 1g
SQLServerSP3

RunningCodeProbably as follows:
Entitys. Customers customer = new test. entitys. Customers ();
Datetime dt = datetime. now;
Using (hfsoft. Data. idatasession session = mapcontainer. opensession ())
{
Session. open ();
For (INT I = 0; I <2000; I ++)
{
Customer. customerid = guid. newguid (). tostring ();
Customer. companyName = "Henry ";
Session. Save (customer );
}
}
TP1 = new timespan (datetime. Now. ticks-Dt. ticks );

Do not enable cache (five threads run time)
00:00:05. 7031250
00:00:06. 8281250
00:00:05. 0156250
00:00:06. 6875000
00:00:06. 4218750
--------------------------------------------------------
Enable the caching of Five Commands (running time of five threads)
00:00:04. 8906250
00:00:03. 5625000
00:00:02. 8750000
00:00:04. 9375000
00:00:05. 4843750
---------------------------------------------------------

The following is the source code of the cache class:
/// <Summary>
/// Data cache save information asynchronous processing delegate
/// </Summary>
Delegate void eventsavecache (Object key, object value );
/// <Summary>
/// Object cache class
/// </Summary>
Public class Cache
{
Private mappingcontainer mcontainer;
/// <Summary>
/// Get or set the relational image container of the current cached object
/// </Summary>
Public mappingcontainer container
{
Get
{
Return mcontainer;
}
Set
{
Mcontainer = value;
}
}
/// <Summary>
/// Construct the cache object
/// </Summary>
Public cache ()
{
//
// Todo: add the constructor logic here
//
}
/// <Summary>
/// Hashtable used to cache data
/// </Summary>
Protected system. Collections. hashtable _ cache = new system. Collections. hashtable ();
Protected object _ lockobj = new object ();
/// <Summary>
/// Obtain the object of the specified key value
/// </Summary>
/// <Param name = "key"> key value </param>
/// <Returns> Object </returns>
Public Virtual Object GetObject (Object key)
{
If (_ cache. containskey (key ))
Return _ cache [Key];
Return NULL;
}
/// <Summary>
/// Save the object to the cache based on the specified key value
/// </Summary>
/// <Param name = "key"> key value </param>
/// <Param name = "value"> saved object </param>
Public void savecaech (Object key, object value)
{
Eventsavecache save = new eventsavecache (setcache );
Iasyncresult AR = save. begininvoke (Key, value, new system. asynccallback (results), null );
}
Private void results (iasyncresult AR)
{
Eventsavecache FD = (eventsavecache) (asyncresult) Ar). asyncdelegate;
FD. endinvoke (AR );
}
/// <Summary>
/// Save the object to the cache based on the specified key value
/// </Summary>
/// <Param name = "key"> key value </param>
/// <Param name = "value"> saved object </param>
Protected virtual void setcache (Object key, object value)
{
Lock (_ lockobj)
{
If (! _ Cache. containskey (key ))
_ Cache. Add (Key, value );
}
}
Public int count
{
Get
{
Return _ cache. count;
}
}
/// <Summary>
/// Delete the object with the specified key value in the cache
/// </Summary>
/// <Param name = "key"> key value </param>
Public Virtual void delobject (Object key)
{
Lock (_ cache. syncroot)
{
_ Cache. Remove (key );
}
}
/// <Summary>
/// Clear all objects in the cache
/// </Summary>
Public Virtual void clear ()
{
Lock (_ cache. syncroot)
{
_ Cache. Clear ();
}
}
}

/// <Summary>
/// Cache class for a record Operation Command
/// </Summary>
Public class cachepersistentcommand: Cache
{

/// <Summary>
/// Cache record operation commands to the memory
/// </Summary>
/// <Param name = "key"> identifier </param>
/// <Param name = "value"> value </param>
Protected override void setcache (Object key, object value)
{
Lock (_ lockobj)
{
Int COUNT = 0;
If (container. config. commandscache. containskey (key ))
Count = (INT) container. config. commandscache [Key];
System. Collections. ilist _ list;
// If the list of such commands already exists in the cache
If (_ cache. containskey (key ))
{
_ List = (system. Collections. ilist) _ cache [Key];

If (count> 0) // The total number of commands cached
{
If (_ list. Count <count) // The cache data volume is less than the total cache data volume.
_ List. Add (value );
}
Else
{
If (_ list. Count <container. config. commandbuffer) // the default list of components whose cache quantity is smaller
_ List. Add (value );
}
}
Else // if the list does not exist
{
If (count> 0 | container. config. commandbuffer> 0) // if the component allows object caching
{
_ List = new system. Collections. arraylist ();
_ List. Add (value );
_ Cache. Add (Key, _ list );
}
}
}
}
/// <Summary>
/// Obtain the relevant command object from the cache
/// </Summary>
/// <Param name = "key"> identifier </param>
/// <Returns> ipersistentcommand </returns>
Public override object GetObject (Object key)
{

If (_ cache. Contains (key) // If the command is cached
{
Foreach (ipersistentcommand CMD in (system. Collections. ilist) _ cache [Key])
{
If (! Cmd. State) // whether the command can be locked through the line
If (CMD. Lock () // command lock
Return cmd;
}
}
Return NULL;
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.