A cache written by the lazy programmer in the world can also greatly speed up your complex computing programs (database programs)

Source: Internet
Author: User
Simulate your database carefully Program Have you found that database data is frequently read repeatedly during computing (especially during multi-user computing ?! This is the primary killer of database processing program performance.

Some people say that before writing a programAlgorithmMake sure that the data records are read in batches and at one time. However, this is often not possible because complicated programs are close to the logical process to be clearly maintained. Moreover, this cannot solve the problem of multi-user access. It is best that we don't have to consider whether the data will be read repeatedly when writing a program to change the program process, but it can naturally prevent repeated reading of the database. When optimization is performed on the computing process without disrupting the logic clarity, it is certainly necessary to rely on a simple cache-the object can only be cached for a few seconds.

Next I will write a simple cache written by the world's lazy programmer using. net, but it can often greatly increase the computing speed of complex computing programs.

C # code
    Using  System;  Using  System. Collections. Generic;  Namespace Domainbase {  Public     Class  Objectcache {  //  Dictionary <K, T> automatically maintains an empty linked list to save unused units.  //  Here, the "weak reference" of the cached objects is used to allow these objects to be reclaimed by garbage collection.      Private  Dictionary  <  String  , Weakreference >  Buffer  =     New  Dictionary  <  String  , Weakreference  >  ();  Public     Object     This  [  String Key] {  Get  {Weakreference ret;  If  (Buffer. trygetvalue (key,  Out  RET)  &&  Ret. isalive)  Return  Ret. target;  Else    Return    Null  ;}  Set  {Weakreference ret;  If  (Buffer. trygetvalue (key,  Out  RET) ret. Target  =  Value;  Else  Buffer. Add (key,  New  Weakreference (value ));}} Public     Void  Remove (  String  Key) {buffer. Remove (key );}}} 

This is the simplest cache. For example:

Public class user
{
Static objectcache buffer = new objectcache ();

Public static getuser (string ID)
{
User ret = buffer [ID];
If (ret = NULL)
{
Ret = read the database and generate the user object (ID );
Buffer [ID] = ret;
}
Return ret;
}
.....

Here, the key of the object and the "weak reference" of the object are saved in a dictionary <K, T> structure dictionary ". In this way, GC will release cached objects when the memory is insufficient. This simplest cache is useful when we need to cache objects in seconds.

. Net Framework has a lot of things that need to be understood by programmers. Don't just hold those empty and affordable books with a wide range of standards. You have time to read more practical analysis. NET framework system. One or two details can be found in every technology. Just like mastering DNA technology, this detail will allow you to expand your principles and get huge benefits through hands-on capabilities.

 

 

Original post connection: http://topic.csdn.net/u/20081124/19/d3461f16-4e25-41dd-9372-0d80ef599156.html? 99628

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.