I recently downloaded some samples to use with AppFabric caching. I noticed in the sample they used a class with static methods instead of a singleton.
I was thinking of changing it to a singleton for the following reasons:
- lazy load
- Only one instance of cache… I can’t think of a reason why more than one instance is needed.
Am I way off target or right on the money?
Below is a class they included:
public class CacheUtil
{
private static DataCacheFactory _factory = null;
private static DataCache _cache = null;
public static DataCache GetCache()
{
if (_cache != null)
return _cache;
//-------------------------
// Configure Cache Client
//-------------------------
//Define Array for 1 Cache Host
List<DataCacheServerEndpoint> servers =
new List<DataCacheServerEndpoint>(1);
//Specify Cache Host Details
// Parameter 1 = host name
// Parameter 2 = cache port number
servers.Add(new DataCacheServerEndpoint("localhost", 22233));
//Create cache configuration
DataCacheFactoryConfiguration configuration =
new DataCacheFactoryConfiguration();
//Set the cache host(s)
configuration.Servers = servers;
//Set default properties for local cache (local cache disabled)
configuration.LocalCacheProperties =
new DataCacheLocalCacheProperties();
//Disable tracing to avoid informational/verbose messages on the web page
DataCacheClientLogManager.ChangeLogLevel(System.Diagnostics.TraceLevel.Off);
//Pass configuration settings to cacheFactory constructor
_factory = new DataCacheFactory(configuration);
//Get reference to named cache called "default"
_cache = _factory.GetCache("default");
return _cache;
}
Yes. It is easy to implement: