缓存助手类是否是单例模式的候选者
本文关键字:单例模式 候选者 是否是 缓存 | 更新日期: 2023-09-27 17:51:24
我最近下载了一些与AppFabric缓存一起使用的示例。我注意到在示例中,他们使用带有静态方法的类而不是单例类。
我正在考虑将其更改为单例,原因如下:
- 延迟加载
- 只有一个缓存实例…我想不出为什么需要多个实例。
我是偏离目标还是正确的钱?
下面是他们包含的一个类:
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;
}
是。它很容易实现:
public class CacheUtil
{
private static DataCacheFactory _factory = null;
private static DataCache _cache = null;
// This is the single instance of this class
private static readonly CacheUtil instance = new CacheUtil();
private CacheUtil()
{
_cache = GetCache();
}
/// <summary>
/// Provides the single reference point to access this class
/// </summary>
public static CacheUtil Instance
{
get { return instance; }
}
private 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 {
Servers = servers,
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;
}
/// <summary>
/// Gets the cache
/// </summary>
public DataCache Cache { get; private set; }
}
我会说是的,我在我们的web应用程序中使用了一个单例模式的缓存(相对于我们自己的缓存接口)
我不明白你为什么要把这个看起来是静态工厂的类改成Singleton。它也会延迟加载,而且也不会有超过一个实例。
编辑
工厂方法甚至更好,因为它返回一个接口(至少我认为是这样),所以它可以在以后的版本中改变它的实现,而不会破坏客户端代码。