Summary of performance optimization (4): Design of Preload

Source: Internet
Author: User
Tags dataloader

This section describes data pre-loading. This section has nothing to do with SQL. We mainly talk about how to design a pre-loaded class library in the gix4 project. The content is as follows:

    1. What is preload? Why?
    2. The APIS we need
    3. A simple example

 

What is preload?

In fact, pre-loading is before you actually start using data,FirstAsynchronousLoad the data and use the previously loaded data when necessary.. At this time, because the data has been loaded, instead of waiting for a long loading processProgramThe speed has been significantly improved.

So when should I use it? In my opinion, this is mainly the case:When we canForesightIn the next step of the program, some data is likely to be used. When it is time-consuming to obtain the data, we can use the pre-load method to prepare the data in advance..

The asynchronous method is used for pre-loading, that is, the background thread is used to load data. The advantage of doing so is that it will not block the current main thread. (However, if the current thread is used to asynchronously load data, there is no need to start a new thread .)

We can use many methods to implement asynchronous loading: In. in the class library of the Net Framework, many places provide the asynchronous programming mode (asynchronous programming design patterns) API, using this mode, you can easily implement various asynchronous loading. Of course, we can also use threadpool. queueuserworkitem provided in 2.0 to implement some lightweight asynchronous operations. In the latest API of. net4.0, the task class is provided to represent executable tasks.

However, these are not the apis I want ......

The APIS we need

Currently,Scenario requirementsYes:

    1. Pre-loading can encapsulate the specified loading action and call it as needed.
    2. The data module (User) does not necessarily know who it is and when it is loaded in advance. It only applies for data.
    3. The initiator of the module that initiates asynchronous loading should know who the user is.
    4. There is no relationship between multiple initiators, but a user can initiate a pre-load. However, the actual data loading operation is only performed once.
    5. Reload is supported.
    6. In a class, different data loading methods are supported to facilitate on-demand loading.
    7. From the user's point of view, no matter whether there is any initiator to pre-load it, it can apply for and get the desired data. That is to say:
      When no initiators perform pre-loading for it, its data application will result in real-time data loading;
      If the pre-load has been initiated and the data has been loaded, the loaded data is obtained directly;
      If the data is not complete, the data user needs to wait until the data is loaded to obtain the data and continue the current operation.

Among them, the most important is the last point.

As you can see, asynchronous operations and inter-thread synchronization are required here. Therefore, we need to implement it based on the various APIs mentioned above. Here we use a simple thread pool method, which is relatively simple and will not be repeated.

The finally designed API is roughly like this:

 Namespace Openexpressapp { Public Enum  Loaderstatus {Notstarted = 0, running = 1, completed = 2, failed = 3 ,} Public class  Foreasyncloader { Public Foreasyncloader ( Action Loadaction ); Public  Loaderstatus Status {Get ;} Public event  Eventhandler Actionsucceeded; /// <Summary> ///  Apply to enable the thread for pre-loading.  ///  Note:    ///  This method can be reimported,Multiple requests only execute the ladaction once.  /// </Summary>  Public void Beginloading (); /// <Summary> /// Reset the loader.  ///  After this method is used, the loadaction is executed again when the pre-load request is applied again.  /// </Summary>  Public void Reset (); /// <Summary> ///  Wait until data loading is complete.  /// </Summary>  Public void Waitforloading ();}}
 
 
 
Example
 
When using a client program, you need to define an attribute for it, for example:
 
Data holder:
Public classDataholder{Private object_ Data;Public objectData {Get{
 
Return this. _ DATA ;}}Private objectGetdatafromweb (){
 
//...} If its data1 data loading is slow, we can define a preload attribute for it:
 
PrivateForeasyncloader_ Dataloader;/// <Summary> ///Data Loader
    //    Public   foreasyncloader  dataloader { Get  { If  ( This . _ dataloader =  null ) { This . _ dataloader =  New   foreasyncloader  (() ==>{  // load data   This . _ DATA =  This . getdatafromweb () ;}) ;}< span style = "color: Blue"> return this . _ dataloader ;}}in this way, the data "consumer" can use this data: 
Public classDataconsumer{Private voidProcess (DataholderHolder) {holder. dataloader. waitforloading ();VaRData = holder. Data;
 
// Consume data...
 
}}
 
Here, although the user does not know whether there are otherCodeThe data is pre-loaded for holder, but after the waitforloading method is executed, the data must be obtained locally. Therefore, you can directly use the data. We can even put this code in the get code block of the data attribute, so that users do not even know the data acquisition solution!
 
Then, you can apply for a pre-load for this "dataholder" in the code before running it. For example, we start the pre-load when the application starts. The following method calls the beginloading method. This method uses the background thread to load data, so it will return immediately:
Public classInvoker{PrivateDataholder_ Holder =NewDataholder();Private voidApp_start (){This. _ Holder. dataloader. beginloading ();// Do other things} Now, a simple pre-load is completed.
 
The process is as follows:
 
 
 
(I am not familiar with the picture. I hope you can correct it. Thank you .)
 
 
 
Summary
 
This article mainly describes how to design a pre-loaded API to meet the application requirements in the current system.
 
Pre-loading is a frequently used mode and is expected to be useful to you.
 
 
In the next article, I will write about the content that is highly correlated with the current system: the "combined application of pre-loading, delayed loading, and aggregate SQL" RELATED TO THE gix4 object model. In addition, it may be noted that, by the way, how to enable the csla Server framework to support multi-thread concurrency.

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.