Implementation of the Cache class (C #)

Source: Internet
Author: User
Cache

Little brother is writing a o/rm component recently (of course the function is still quite small).
Everyone should make it clear that updating the entity objects to the database must undergo a series of transformations, especially the generation of SQL statements is a relatively cost resource, because there are too many things in the middle.
In the process of designing, I think that if an object is inserted into the database and the corresponding command is stored in the cache, the next time an object of the same type is doing this, the cache will be detected if there is a direct use this efficiency should be higher.
With this idea began to design (but the heart is still up and down, after all, the first attempt).
Because the object handling in the cache is more complex, there is a shared problem in multiple threads, and if two threads call the same command at the same time, it will certainly result in a processing error!
In order to better control the sharing of command objects, a persistent interface is defined specifically for the command.
After a period of time to design and write, calculate a little results, shun the things you do share.


The following are the scenarios for component testing
P4 2.4 1G
SQL Server SP3

The code that runs is probably 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 open cache (5 threads run time)
00:00:05.7031250
00:00:06.8281250
00:00:05.0156250
00:00:06.6875000
00:00:06.4218750
--------------------------------------------------------
Turn on 5 command caches (5 threads run time)
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 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>
Gets or sets the relationship image container where the current cache object resides
</summary>
Public Mappingcontainer Container
{
Get
{
return mcontainer;
}
Set
{
Mcontainer = value;
}
}
<summary>
Constructing Cache objects
</summary>
Public Cache ()
{
//
TODO: Add constructor logic here
//
}
<summary>
Hashtable for caching data
</summary>
protected System.Collections.Hashtable _cache = new System.Collections.Hashtable ();
Protected object _lockobj = new Object ();
<summary>
Gets the object that specifies the 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>
Saves the object to the cache by the specified key value
</summary>
<param name= "key" > Key value </param>
<param name= "Value" > Saved objects </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>
Saves the object to the cache by the specified key value
</summary>
<param name= "key" > Key value </param>
<param name= "Value" > Saved objects </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>
Object that deletes 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>
Clears 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>
To cache record operation commands in memory
</summary>
<param name= "key" > Logo </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 a list of such commands already exists in the cache
if (_cache.containskey (key))
{
_list = (System.Collections.IList) _cache[key];

Total number of buffers for if (count >0)//commands
{
if (_list. Count < count)//cached data amount less than total cache
_list. ADD (value);
}
Else
{
if (_list. Count < Container.Config.CommandBuffer)//cache count is less than the default list of components
_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>
Get the related command object from the cache
</summary>
<param name= "key" > Logo </param>
<returns>IPersistentCommand</returns>
public override Object GetObject (object key)
{

if (_cache.contains (key))//If the command exists in a buffer
{
foreach (Ipersistentcommand cmd in (System.Collections.IList) _cache[key])
{
if (!cmd. State)//whether the command can be locked through the row
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.