缓存辅助类是单例模式的候选者吗?
我最近下载了一些示例以与 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想说是的,我在我们的网络应用程序中使用单例模式作为缓存(针对我们自己的缓存接口)
I would say yes, I use a singleton pattern for the Cache in our web app (against our own cache Interface)
我不明白为什么你想将这个似乎是静态工厂的类更改为单例。它还执行延迟加载,并且不会有多个实例。
编辑
工厂方法甚至更好,因为它返回一个接口(至少我猜是这样),因此它可以在以后的版本中更改其实现,而不会破坏客户端代码。
I don't see why you want to change the class, which seems to be a static factory, into a Singleton. It also does lazy loading, and there also won't be more than one instance.
EDIT
The factory approach is even better in that it returns an interface (at least I would guess so), so it could change it's implementation in later releases without breaking client code.
是的。它很容易实现:
Yes. It is easy to implement: