views:

163

answers:

1

Hi All, 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);

    }
+1  A: 

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.

Mauricio Scheffer