Cache excellent post written by sp1234 in csdn

Source: Internet
Author: User

Simulate your database carefullyProgramHave 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.

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 and weak reference of the object are saved in a 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.

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.