Skip to content

Commit 5e4520f

Browse files
fredericDelaportehazzik
authored andcommitted
Improve second level cache documentation
- Requirement of caching with composite id - Cache settings - Formatting fixes
1 parent 1b9d44e commit 5e4520f

File tree

2 files changed

+138
-16
lines changed

2 files changed

+138
-16
lines changed

doc/reference/modules/component_mapping.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@
225225
notion of composite key equality.
226226
</para>
227227
</listitem>
228+
<listitem>
229+
<para>
230+
It should re-implement <literal>ToString()</literal> if you consider
231+
using the second level cache. See <xref linkend="NHibernate.Caches-howto" />.
232+
</para>
233+
</listitem>
228234
</itemizedlist>
229235

230236
<para>

doc/reference/modules/nhibernate_caches.xml

Lines changed: 132 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
<programlisting>
126126
&lt;property name="cache.provider_class"&gt;<literal>XXX</literal>&lt;/property&gt;<co id="hibernate.cache.provider_class-co" linkends="hibernate.cache.provider_class"/>
127127
&lt;property name="cache.default_expiration"&gt;<literal>120</literal>&lt;/property&gt;<co id="nhcaches-expiration-co" linkends="nhcaches-expiration"/>
128+
&lt;property name="cache.use_sliding_expiration"&gt;<literal>true</literal>&lt;/property&gt;<co id="nhcaches-sliding-co" linkends="nhcaches-sliding"/>
128129
</programlisting>
129130
<calloutlist>
130131
<callout arearefs="hibernate.cache.provider_class-co" id="hibernate.cache.provider_class">
@@ -138,7 +139,15 @@
138139
<callout arearefs="nhcaches-expiration-co" id="nhcaches-expiration">
139140
<para>
140141
The <literal>expiration</literal> value is the number of seconds you wish
141-
to cache each entry (here two minutes). This example applies to SysCache only.
142+
to cache each entry (here two minutes). Not all providers support this setting, it may
143+
be ignored. Check their respective documentation.
144+
</para>
145+
</callout>
146+
<callout arearefs="nhcaches-sliding-co" id="nhcaches-sliding">
147+
<para>
148+
The <literal>use_sliding_expiration</literal> value is whether you wish to use a
149+
sliding expiration or not. Defaults to <literal>false</literal>.
150+
Not all providers support this setting, it may be ignored. Check their respective documentation.
142151
</para>
143152
</callout>
144153
</calloutlist>
@@ -156,15 +165,31 @@
156165
<formalpara>
157166
<title>Be careful</title>
158167
<para>
159-
Caches are never aware of changes made to the persistent store by another process (though they may be
160-
configured to regularly expire cached data). As the caches are created at the session factory level,
161-
they are destroyed with the SessionFactory instance; so you must keep them alive as long as you need
162-
them.
163-
</para>
164-
<para>
165-
The second level cache requires the use of transactions, be it through transaction scopes or NHibernate
166-
transactions. Interacting with the data store without an explicit transaction is discouraged, and will
167-
not allow the second level cache to work as intended.
168+
<itemizedlist>
169+
<listitem>
170+
<para>
171+
Most caches are never aware of changes made to the persistent store by another process (though they may be
172+
configured to regularly expire cached data). As the caches are created at the session factory level,
173+
they are destroyed with the SessionFactory instance; so you must keep them alive as long as you need
174+
them.
175+
</para>
176+
</listitem>
177+
<listitem>
178+
<para>
179+
The second level cache requires the use of transactions, be it through transaction scopes or NHibernate
180+
transactions. Interacting with the data store without an explicit transaction is discouraged, and will
181+
not allow the second level cache to work as intended.
182+
</para>
183+
</listitem>
184+
<listitem>
185+
<para>
186+
To avoid issues with composite ids and some cache providers, <literal>ToString()</literal> needs to
187+
be overridden on composite id classes. It should yield an unique string representing the id. If the
188+
composite id is mapped as a component, overriding the component <literal>ToString()</literal>
189+
is enough. See <xref linkend="components-compositeid" />.
190+
</para>
191+
</listitem>
192+
</itemizedlist>
168193
</para>
169194
</formalpara>
170195
<para>
@@ -184,29 +209,39 @@
184209
<section id="NHibernate.Caches.SysCache">
185210
<title>SysCache Configuration</title>
186211
<para>
187-
As SysCache relies on <classname>System.Web.Caching.Cache</classname> for the underlying implementation,
188-
the configuration is based on the available options that make sense for NHibernate to utilize.
212+
As SysCache relies on <classname>System.Web.Caching.Cache</classname> for the underlying implementation.
213+
Following NHibernate configuration settings are available:
189214
</para>
190215

191216
<variablelist>
192217
<varlistentry>
193218
<term><literal>expiration</literal></term>
194219
<listitem>
195220
Number of seconds to wait before expiring each item.
221+
Takes precedence over <literal>cache.default_expiration</literal>.
222+
If none of them are defined, defaults to <literal>300</literal>.
223+
</listitem>
224+
</varlistentry>
225+
<varlistentry>
226+
<term><literal>cache.use_sliding_expiration</literal></term>
227+
<listitem>
228+
Should the expiration be sliding? A sliding expiration is reinitialized at each get.
229+
Defaults to <literal>false</literal>.
196230
</listitem>
197231
</varlistentry>
198232
<varlistentry>
199233
<term><literal>priority</literal></term>
200234
<listitem>
201235
A numeric cost of expiring each item, where 1 is a low cost, 5 is the highest, and 3 is normal.
202-
Only values 1 through 5 are valid.
236+
Only values 1 through 5 are valid. 6 is a special value corresponding to
237+
<literal>NotRemovable</literal>, it should not be used.
203238
</listitem>
204239
</varlistentry>
205240
</variablelist>
206241

207242
<para>
208243
SysCache has a config file section handler to allow configuring different expirations and priorities for
209-
different regions. Here's an example:
244+
different regions. Here is an example:
210245
</para>
211246

212247
<example>
@@ -219,7 +254,7 @@
219254
220255
<syscache>
221256
<cache region="foo" expiration="500" priority="4" />
222-
<cache region="bar" expiration="300" priority="3" />
257+
<cache region="bar" expiration="300" priority="3" sliding="true" />
223258
</syscache>
224259
</configuration>]]></programlisting>
225260
</example>
@@ -235,6 +270,28 @@
235270
MSDN documentation.
236271
</para>
237272

273+
<para>
274+
Following NHibernate configuration settings are available:
275+
</para>
276+
277+
<variablelist>
278+
<varlistentry>
279+
<term><literal>expiration</literal></term>
280+
<listitem>
281+
Number of seconds to wait before expiring each item.
282+
Takes precedence over <literal>cache.default_expiration</literal>.
283+
If none of them are defined, defaults to <literal>300</literal>.
284+
</listitem>
285+
</varlistentry>
286+
<varlistentry>
287+
<term><literal>cache.use_sliding_expiration</literal></term>
288+
<listitem>
289+
Should the expiration be sliding? A sliding expiration is reinitialized at each get.
290+
Defaults to <literal>false</literal>.
291+
</listitem>
292+
</varlistentry>
293+
</variablelist>
294+
238295
<para>
239296
To configure cache regions with SqlCacheDependencies a <literal>syscache2</literal> config section must be
240297
defined in the application's configuration file. See the sample below.
@@ -456,7 +513,7 @@
456513
<example>
457514
<title>Relative Expiration</title>
458515

459-
<programlisting><![CDATA[<cacheRegion name="Product" relativeExpiration="300" priority="High" />]]></programlisting>
516+
<programlisting><![CDATA[<cacheRegion name="Product" relativeExpiration="300" priority="High" useSlidingExpiration="true" />]]></programlisting>
460517
</example>
461518

462519
<example>
@@ -472,6 +529,18 @@
472529
<listitem>
473530
<para>
474531
Number of seconds that an individual item will exist in the cache before being removed.
532+
Defaults to <literal>300</literal> if neither <literal>relativeExpiration</literal> nor
533+
<literal>timeOfDayExpiration</literal> are defined, and if no expiration settings are
534+
defined in NHibernate configuration.
535+
</para>
536+
</listitem>
537+
</varlistentry>
538+
<varlistentry>
539+
<term><literal>useSlidingExpiration</literal></term>
540+
<listitem>
541+
<para>
542+
Should the expiration be sliding? A sliding expiration is reinitialized at each get.
543+
Defaults to <literal>false</literal> if not defined in NHibernate configuration.
475544
</para>
476545
</listitem>
477546
</varlistentry>
@@ -498,4 +567,51 @@
498567

499568
</section>
500569

570+
<section id="NHibernate.Caches.RtMemoryCache">
571+
<title>Runtime Memory Configuration</title>
572+
<para>
573+
As runtime memory relies on <classname>System.Runtime.Caching.MemoryCache</classname> for the
574+
underlying implementation.
575+
Following NHibernate configuration settings are available:
576+
</para>
577+
578+
<variablelist>
579+
<varlistentry>
580+
<term><literal>expiration</literal></term>
581+
<listitem>
582+
Number of seconds to wait before expiring each item.
583+
Takes precedence over <literal>cache.default_expiration</literal>.
584+
If none of them are defined, defaults to <literal>300</literal>.
585+
</listitem>
586+
</varlistentry>
587+
<varlistentry>
588+
<term><literal>cache.use_sliding_expiration</literal></term>
589+
<listitem>
590+
Should the expiration be sliding? A sliding expiration is reinitialized at each get.
591+
Defaults to <literal>false</literal>.
592+
</listitem>
593+
</varlistentry>
594+
</variablelist>
595+
596+
<para>
597+
RtMemoryCache has a config file section handler to allow configuring different expirations for
598+
different regions. Here is an example:
599+
</para>
600+
601+
<example>
602+
<programlisting><![CDATA[<?xml version="1.0" encoding="utf-8" ?>
603+
<configuration>
604+
<configSections>
605+
<section name="syscache"
606+
type="NHibernate.Caches.SysCache.SysCacheSectionHandler,NHibernate.Caches.SysCache" />
607+
</configSections>
608+
609+
<syscache>
610+
<cache region="foo" expiration="500" />
611+
<cache region="bar" expiration="300" sliding="true" />
612+
</syscache>
613+
</configuration>]]></programlisting>
614+
</example>
615+
</section>
616+
501617
</chapter>

0 commit comments

Comments
 (0)