Alachisoft NCache 4.1 - Online Documentation


NCache provides a second level or process level distributed cache for NHibernate by implementing ICacheProvider and ICache interfaces. You can plug-in NCache to your existing NHibernate application without any code change. Just change your NHibernate configuration file to plug-in NCache as your cache provider and you're all set to start using distributed caching.
Follow these steps to use NCache with NHibernate:
  1. Register 'nhibernate' and 'ncache' configuration sections in the <configSections> of the web.config or app.config as follows:
    <section name = "hibernate-configuration" type = "NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
    <section name = "ncache" type = "Alachisoft.NCache.Integrations.NHibernate.Cache.NCacheSection, Alachisoft.NCache.Integrations.NHibernate.Cache"/>
  2. Define section details for <nhibernate> as follows:
    <hibernate-configuration xmlns = "urn:nhibernate-configuration-2.2">
    <property name = "cache.provider_class"> Alachisoft.NCache.Integrations.NHibernate.Cache.NCacheProvider, Alachisoft.NCache.Integrations.NHibernate.Cache </property>
  3. Define section details for <ncache> as follows:
            <region name = "default">
              <add key = "cacheName" value = "mycache"/>
              <add key = "enableCacheException" value = "false"/>
              <class name = "nhibernator.BLL.Customer">
                <add key = "priority" value = "1"/>
                <add key = "useAsync" value = "false"/>
                <add key = "relativeExpiration" value = "180"/> or <add key = "staticExpiration" value = "100"/>
The above example of app.config or web.config contains the following options for you to configure. Each of them is explained below as well.
  • hibernate.cache.provider_class: This option lets you specify that NCache is your second level cache provider. You need to mention two classes which implement NHibernate.Cache.ICacheProvider and NHibernate.Cache.ICache interfaces respectively. This is how NHibernate knows how to call this second level cache.
  • The "region" attribute of the NCache determines the Cache in which the particular entity will be cached.
  • cacheName: This option is included inside the <ncache> tag and lets you specify name of the cache that you have configured through NCache Manager. All caches in NCache are named and this name allows NCache provider to connect to the right cache.
  • enableCacheException: This flag allows you to turn on exception handling which by default is turned off. This will throw an exception which you'll have to catch in your .NET application.
  • class: This lets you specify options specific to a .NET class. All the options below are class-specific. Class name must be the same as specified in the mapping file of the class ( .hbm.xml file).
  • priority: The priority you want to use for items cached. The possible values are from 1 to 6.
  • useAsync: This is a flag that tells the provider whether to use the standard synchronized "Add" and "Insert" or the asynchronous "AddAsync" and "InsertAsync" operations. The asynchronous operations immediately return control back to the client and queues up the operation in a background queue. Then, a background thread executes the command.
  • relativeExpiration: This value is specified as number of seconds. It indicates the idle-time expiration for your items, meaning how long your item can stay without being accessed.
  • Calling new Configuration() reads the <nhibernate> section only.
  • Calling no-parameter version of cfg.Configure() configures NHibernate from app.config if it contains <nhibernate-configuration>, otherwise hibernate.cfg.xml is used.
See Also
Copyright © 2005-2012 Alachisoft. All rights reserved.