Castle Active Record - 使用缓存

发布于 2024-08-28 04:10:24 字数 935 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风月客 2024-09-04 04:10:24

看一下缓存模式:

顺便说一句,每次调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文