NineRays Object Pool/Cache Pattern

Environment: .NET

Introduction

Object pooling and caching are very useful object-orented patterns. They work well for large or hardweight objects. For example, you can store GDI+ pens in some PenStore, which stores its pens in maintained cache. This pattern greatly improves the speed of your code without adding to the object heap's overhead.

How to Create the Cache

.NET objects are built on a garbage-collected heap. So, we can use weak references to objects to use garbage collector functionality for our needs. Garbage collector does all we need without any lines of code because of its "generations" idiom. It tracks the lifetime of objects and collects garbage when required.

So, if an object was going out of scope, there is a time while an object is still alive and can be reused by the cache. (It is true also in the case of using of an object in other threads.) Yet another aspect of the Cache pattern in the .NET world is determining whether the object has been disposed. It matters because no one wants to work with disposed objects (if it supports IDisposable).

There is no generic .NET way to determine whether this object has been disposed, but some other ways exist (the Control.IsDisposed method, for example). The IDisposableEx interface helps to determine your object's state (look at the attached Disposable class for details).

How to Represent the Cache

The cache looks like a dictionary. So, we can copy the IDictionary interface contract that the cache has agreed to. But we cannot use the IDictionary interface itself because a null value in the cache may be returned sometimes, even when a nonnull value was posted into the cache before (in the case of a value, the object was excluded from the cache in the process of garbage collection).

This behavior looks like none of the dictionaries, so we do not use the cache to implement the IDictionary interface. Our sources are fully self-documented through C# XML documentation tags.

The demo application is included as a download.


Downloads

Download demo project - 26 Kb



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Hundreds of millions of users have adopted public cloud storage solutions to satisfy their Private Online File Sharing and Collaboration (OFS) needs. With new headlines on cloud privacy issues appearing almost daily, the need to explore private alternatives has never been stronger. Join ESG Senior Analyst Terri McClure and Connected Data in this on-demand webinar to take a look at the business drivers behind OFS adoption, how organizations can benefit from on-premise deployments, and emerging private OFS …

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

Most Popular Programming Stories

More for Developers

RSS Feeds