Monday, July 6, 2009

Client Model Pooling

One simple way to improve performance of your data access services is to create a client model pool. Creating a new instance of a ClientModel for each service request can get expensive especially if the ClientModel makes use of a SemanticServerGraph. By creating a pool of client models that can be reused you benefit from cached statistics and compiled queries.

Client model pooling is implemented is by creating a single instance of a ClientModelPool. The example below creates a pool that holds a maximum of 10 client models that expire after 5 minutes.

ClientModel pool = new ClientModelPool(10, new TimeSpan(0, 5, 0));

// assign it to some global
app.ModelPool = pool;

You will want to keep this pool around in as a static member or in some other global container such as the ASP.NET Application object. Next you must create a method for creating a new instance of a client model that is configured for your application. This can be done in one of two ways:

  1. Derive an application specific class from ClientModelPool and override its Create method.
  2. Implement your own method for creating a client model on some centralized configuration class that already exists in your application.

Option 2 seems to be the most popular choice. So a simple method for getting a client model in your application class might look like.

public ClientModel GetClientModel()
{
//get a pooled model
ClientModel model = ModelPool.Get();

if (model == null)
{
// create a new one and make it pooled
model = new ClientModel();
model.SetPool(ModelPool);

// TODO: configure for your application
}

return model;
}

You should call this method in your application in the context of a using statement. When the client model is disposed then it is returned to the pool.

using (ClientModel cm = app.GetClientModel())
{
//do something
}

You should quickly discover this can have a big effect on performance.

0 comments:

Post a Comment