Castle Active Record - 使用缓存
我是 Castle Active Record 模式的新手,我正在尝试了解如何有效地使用缓存。 所以我想做(或想要做)的是在调用 GetAll 时,查明我之前是否调用过它并检查缓存,否则加载它,但我也想传递一个 bool 参数来强制缓存清除并重新查询数据库。
所以我只是在寻找最后的部分。 谢谢
public static List<Model.Resource> GetAll(bool forceReload)
{
List<Model.Resource> resources = new List<Model.Resource>();
//Request to force reload
if (forceReload)
{
//need to specify to force a reload (how?)
XmlConfigurationSource source = new XmlConfigurationSource("appconfig.xml");
ActiveRecordStarter.Initialize(source, typeof(Model.Resource));
resources = Model.Resource.FindAll().ToList();
}
else
{
//Check the cache somehow and return the cache?
}
return resources;
}
public static List<Model.Resource> GetAll()
{
return GetAll(false);
}
im new to the Castle Active Record Pattern and Im trying to get my head around how to effectivley use cache.
So what im trying to do (or want to do) is when calling the GetAll, find out if I have called it before and check the cache, else load it, but I also want to pass a bool paramter that will force the cache to clear and requery the db.
So Im just looking for the final bits.
thanks
public static List<Model.Resource> GetAll(bool forceReload)
{
List<Model.Resource> resources = new List<Model.Resource>();
//Request to force reload
if (forceReload)
{
//need to specify to force a reload (how?)
XmlConfigurationSource source = new XmlConfigurationSource("appconfig.xml");
ActiveRecordStarter.Initialize(source, typeof(Model.Resource));
resources = Model.Resource.FindAll().ToList();
}
else
{
//Check the cache somehow and return the cache?
}
return resources;
}
public static List<Model.Resource> GetAll()
{
return GetAll(false);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下缓存模式:
顺便说一句,每次调用 GetAll 时都会初始化 ActiveRecord。您只需在应用程序启动时初始化一次。
另外,像这样显式释放缓存通常不是一个好的做法。相反,使用某种策略或依赖关系(例如,参见 SqlDependency)。
此外,NHibernate 有一个可插入的二级缓存。
Take a look at the caching pattern:
BTW you're initializing ActiveRecord each time you call GetAll. You have to initialize only once, when your application starts.
Also, it's not good practice generally to explicitly release the cache like that. Instead, use some sort of policy or dependency (see for example SqlDependency)
Also, NHibernate has a pluggable second-level cache.