Teach you how to write an ORM (7)

Source: Internet
Author: User
I will sell a token first. If you are interested, you can study it on your own.
Today, I will talk about the caching problem first. For data caching, I have experienced many iterations, but in. net Community Many people support on-demand reading, but I think this is a misunderstanding. Program Because the personal computer memory is relatively small and versatile, many programs emphasize to use as little memory as possible. However, it is a misunderstanding when writing a service program. caching common data and calling it quickly as needed is a magic weapon to improve performance, now, when the memory of a personal computer is measured in GB, it is right to make full use of the memory properly.
So we decided to implement a cache in our demo Orm. What should we cache ?, The execution result of the insert statement is obviously no longer needed. We only need to cache the results of each select statement. In addition, due to the limitations of. Net cache objects (which can only be used on the Web), we only have to implement one by ourselves.
In fact, the design is not difficult. We have used cache for configuration processing. We only need to modify it. First, the infrastructure is static hashtable, however, you must first determine what is used as the key. The value indicates that the value of hashtable is an object. Therefore, an ilist can be saved, and an int can be used as well as an object, therefore, it is suitable for select results.
This is mainly about key determination.
Here, we determine that if the data remains unchanged, the Select results will be the same if the SQL statements are the same, so we will directly use the SQL statement as the key. However, some SQL statements contain variables, such as select XX from TT where id = @ ID,
At this time, we can process the SQL statement and drop @ ID Replace with the value :}

But there is another problem. If a table is inserted, updated, or deleted, then what is selected will not change? Indeed, after performing these operations, we need to determine which cache items need to be refreshed. At this time, we need to include the table information in the key. Although we can analyze the SQL directly, it increases the cost, therefore, when generating the key, we add the table involved in this SQL statement to the top of the key using the [tablename] method.

Finally, let's take a look. Code : Namespace Alexander. xbase. xcache
{
Public   Class Objcache
{
Private   Static Sortedlist < String , Object > Data =   New Sortedlist < String , Object > ();

Public   Static   Void Insert ( String Key, Object Dat)
{
Data. Add (Key, dat );
}
Public   Static Sortedlist < String , Object > GetObject
{< br> Get
{< br> return data;
}
}
Public   Static   Void Flash ( String Classname)
{
Arraylist removelist =   New Arraylist ();
For ( Int I =   0 ; I < Data. Count; I ++ )
{
If (Data. Keys [I]. startswith (classname ))
{
Removelist. Add (data. Keys [I]);
}
}
Foreach ( Object O In Removelist)
{
Data. Remove (O. tostring ());
}
}
}
}

After talking about it for a long time, there are not many codes. Sometimes it is as simple as it is complicated.
However, this type of cache is limited by the possibility of redundant data in the cache.

To be continue ......

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.