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.