forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnhibernate_caches.xml
1363 lines (1254 loc) · 57 KB
/
nhibernate_caches.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!-- <!DOCTYPE chapter SYSTEM "../docbook-xml/docbookx.dtd"> -->
<chapter id="caches">
<title>NHibernate.Caches</title>
<abstract id="NHibernate.Caches-abstract">
<title>What is NHibernate.Caches?</title>
<formalpara>
<title>NHibernate.Caches namespace contains several second-level cache providers for NHibernate.</title>
<para>
A cache is a place where entities are kept after being loaded from the database; once cached, they can be
retrieved without going to the database. This means that they are faster to (re)load.
</para>
</formalpara>
<para>
An NHibernate session has an internal (first-level) cache where it keeps its entities. There is no sharing
between these caches - a first-level cache belongs to a given session and is destroyed with it. NHibernate
provides a <emphasis>second-level cache</emphasis> system; it works at the session factory level.
A second-level cache is shared by all sessions created by the same session factory.
</para>
<para>
An important point is that the second-level cache <emphasis>does not</emphasis> cache instances of the object
type being cached; instead it caches the individual values of the properties of that object. This provides two
benefits. One, NHibernate doesn't have to worry that your client code will manipulate the objects in a way that
will disrupt the cache. Two, the relationships and associations do not become stale, and are easy to keep
up-to-date because they are simply identifiers. The cache is not a tree of objects but rather a map of arrays.
</para>
<para>
With the <emphasis>session-per-request</emphasis> model, a high number of sessions can concurrently access
the same entity without hitting the database each time; hence the performance gain.
</para>
<para>
Depending on the chosen cache provider, the second level cache may be actually shared between different session
factories. If you need to avoid this for some session factories, configure each of them with a different
<literal>cache.region_prefix</literal>. See <xref linkend="configuration-optional" />.
</para>
<para>
Several cache providers have been contributed by NHibernate users:
<variablelist>
<varlistentry>
<term><classname>NHibernate.Caches.Prevalence</classname></term>
<listitem>
<para>
Uses <classname>Bamboo.Prevalence</classname> as the cache provider. Open the
file <filename>Bamboo.Prevalence.license.txt</filename> for more information about its license;
you can also visit its <ulink url="http://bbooprevalence.sourceforge.net/">website</ulink>. This
provider is available for the .Net Framework only. Also see <xref linkend="NHibernate.Caches.Prevalence" />.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><classname>NHibernate.Caches.SysCache</classname></term>
<listitem>
<para>
Uses <classname>System.Web.Caching.Cache</classname> as the cache provider. This means that you can
rely on ASP.NET caching feature to understand how it works. For more information, read (on the MSDN):
<ulink url="https://msdn.microsoft.com/en-us/library/6hbbsfk6.aspx">Caching Application Data</ulink>.
This provider is available for the .Net Framework only. Also see <xref linkend="NHibernate.Caches.SysCache" />.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><classname>NHibernate.Caches.SysCache2</classname></term>
<listitem>
<para>
Similar to <classname>NHibernate.Caches.SysCache</classname>, uses ASP.NET cache. This provider also supports
SQL dependency-based expiration, meaning that it is possible to configure certain cache regions to automatically
expire when the relevant data in the database changes.
</para>
<para>
SysCache2 requires Microsoft SQL Server 2000 or higher. This provider is available for the .Net Framework only.
</para>
<para>
See <xref linkend="NHibernate.Caches.SysCache2" />.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><classname>NHibernate.Caches.EnyimMemcached</classname></term>
<listitem>
<para>
Uses <literal>Memcached</literal>. See <ulink url="https://memcached.org/">memcached homepage</ulink>
for more information on Memcached. This provider is available for the .Net Framework only. Also see
<xref linkend="NHibernate.Caches.EnyimMemcached" />.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><classname>NCache provider for NHibernate</classname></term>
<listitem>
<para>
Uses <literal>NCache</literal>. NCache is a commercial distributed caching system with a provider for NHibernate.
The NCache Express version is free for use, see
<ulink url="http://www.alachisoft.com/ncache/">NCache Express homepage</ulink>
for more information.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><classname>NHibernate.Caches.RtMemoryCache</classname></term>
<listitem>
<para>
Uses <classname>System.Runtime.Caching.MemoryCache.Default</classname> as the cache provider. This provider
is available as a .Net Standard NuGet package. See <xref linkend="NHibernate.Caches.RtMemoryCache" />.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><classname>NHibernate.Caches.StackExchangeRedis</classname></term>
<listitem>
<para>
Uses <classname>StackExchange.Redis</classname>. This provider is available as a .Net Standard
NuGet package. It can batch together puts and reads, reducing incurred IOs.
See <xref linkend="NHibernate.Caches.StackExchangeRedis" />.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><classname>NHibernate.Caches.CoreMemoryCache</classname></term>
<listitem>
<para>
Uses <classname>Microsoft.Extensions.Caching.Memory.MemoryCache</classname> as the cache provider. This provider
is available as a .Net Standard NuGet package. See <xref linkend="NHibernate.Caches.CoreMemoryCache" />.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><classname>NHibernate.Caches.CoreDistributedCache</classname></term>
<listitem>
<para>
Uses <classname>Microsoft.Extensions.Caching.Abstractions.IDistributedCache</classname> implementations as the cache
provider. The implementation has to be provided through an <literal>IDistributedCacheFactory</literal>.
Distributed cache factories for <literal>Memcached</literal>, <literal>Redis</literal>, <literal>SqlServer</literal>
and <literal>Memory</literal> caches are available through their own package, prefixed by
<literal>NHibernate.Caches.CoreDistributedCache.</literal>.
</para>
<para>
This provider is available as a .Net Standard NuGet package. See <xref linkend="NHibernate.Caches.CoreDistributedCache" />.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</abstract>
<section id="NHibernate.Caches-howto">
<title>How to use a cache?</title>
<para>
Here are the steps to follow to enable the second-level cache in NHibernate:
<itemizedlist>
<listitem>
<para>
Choose the cache provider you want to use and copy its assembly in your assemblies directory.
(For example, <filename>NHibernate.Caches.Prevalence.dll</filename> or
<filename>NHibernate.Caches.SysCache.dll</filename>.)
</para>
</listitem>
<listitem>
<para>
To tell NHibernate which cache provider to use, add in your NHibernate configuration file
(can be <filename>YourAssembly.exe.config</filename> or <filename>web.config</filename> or a
<filename>.cfg.xml</filename> file):
<programlisting>
<property name="cache.provider_class"><literal>XXX</literal></property><co id="hibernate.cache.provider_class-co" linkends="hibernate.cache.provider_class"/>
<property name="cache.default_expiration"><literal>120</literal></property><co id="nhcaches-expiration-co" linkends="nhcaches-expiration"/>
<property name="cache.use_sliding_expiration"><literal>true</literal></property><co id="nhcaches-sliding-co" linkends="nhcaches-sliding"/>
</programlisting>
<calloutlist>
<callout arearefs="hibernate.cache.provider_class-co" id="hibernate.cache.provider_class">
<para>
"<literal>XXX</literal>" is the assembly-qualified class name of a class
implementing <classname>ICacheProvider</classname>, eg.
"<classname>NHibernate.Caches.SysCache.SysCacheProvider,
NHibernate.Caches.SysCache</classname>".
</para>
</callout>
<callout arearefs="nhcaches-expiration-co" id="nhcaches-expiration">
<para>
The <literal>expiration</literal> value is the number of seconds you wish
to cache each entry (here two minutes). Not all providers support this setting, it may
be ignored. Check their respective documentation.
</para>
</callout>
<callout arearefs="nhcaches-sliding-co" id="nhcaches-sliding">
<para>
The <literal>use_sliding_expiration</literal> value is whether you wish to use a
sliding expiration or not. Defaults to <literal>false</literal>.
Not all providers support this setting, it may be ignored. Check their respective documentation.
</para>
</callout>
</calloutlist>
</para>
</listitem>
<listitem>
<para>
Add <literal><cache usage="read-write|nonstrict-read-write|read-only"/></literal> (just
after <literal><class></literal>) in the mapping of the entities you want to cache. It
also works for collections (bag, list, map, set, ...).
</para>
</listitem>
</itemizedlist>
</para>
<formalpara>
<title>Be careful</title>
<para>
<itemizedlist>
<listitem>
<para>
Most caches are never aware of changes made to the persistent store by another process (though they may be
configured to regularly expire cached data). As the caches are created at the session factory level,
they are destroyed with the SessionFactory instance; so you must keep them alive as long as you need
them.
</para>
</listitem>
<listitem>
<para>
The second level cache requires the use of transactions, be it through transaction scopes or NHibernate
transactions. Interacting with the data store without an explicit transaction is discouraged, and will
not allow the second level cache to work as intended.
</para>
</listitem>
<listitem>
<para>
To avoid issues with composite ids and some cache providers, <literal>ToString()</literal> needs to
be overridden on composite id classes. It should yield an unique string representing the id. If the
composite id is mapped as a component, overriding the component <literal>ToString()</literal>
is enough. See <xref linkend="components-compositeid" />.
</para>
</listitem>
</itemizedlist>
</para>
</formalpara>
<para>
See also <xref linkend="performance-cache" />.
</para>
</section>
<section id="NHibernate.Caches.ConfigurationProvider">
<title>Using a custom configuration provider</title>
<para>
Most caches have their own configuration section. By default, the cache attempts to read its configuration section
through the .Net <literal>ConfigurationManager</literal>. Some environments do not support it, especially when
running under .Net Core. The .Net Core compatible caches provide a way to set a custom configuration provider, through
their <literal>NHibernate.Caches. cache specific namespace .ConfigurationProvider</literal> class.
</para>
<para>
To disable the configuration provider, in case you configure the cache entirely programmatically,
set its <literal>ConfigurationProvider.Current</literal> property to <literal>null</literal>.
</para>
<programlisting><![CDATA[ConfigurationProvider.Current = null;]]></programlisting>
<para>
To provide directly the <literal>System.Configuration.Configuration</literal> instance to use, call
the cache <literal>ConfigurationProvider.SetConfiguration</literal> method.
</para>
<programlisting><![CDATA[ConfigurationProvider.SetConfiguration(yourConfig);]]></programlisting>
<para>
You may also derive a custom provider from the <literal>ConfigurationProvider</literal> of the cache,
implements its abstract method <literal>GetConfiguration</literal>, and assign an instance of your
custom provider to the cache <literal>ConfigurationProvider.Current</literal> property.
</para>
<para>
Changes of the <literal>ConfigurationProvider.Current</literal> property value are to be done very
early in the application lifecycle, before building any session factory using the cache provider or
before any other call on the cache API. Otherwise they will not be taken into account.
</para>
</section>
<section id="NHibernate.Caches.Prevalence">
<title>Prevalence Cache Configuration</title>
<para>
There is only one configurable parameter: <literal>prevalenceBase</literal>. This is the directory on the
file system where the Prevalence engine will save data. It can be relative to the current directory or a
full path. If the directory doesn't exist, it will be created.
</para>
<para>
The <literal>prevalenceBase</literal> setting can only be set programmatically through the NHibernate
configuration object, by example with <literal>Configuration.SetProperty</literal>.
</para>
</section>
<section id="NHibernate.Caches.SysCache">
<title>SysCache Configuration</title>
<para>
SysCache relies on <classname>System.Web.Caching.Cache</classname> for the underlying implementation.
This is a .Net Framework only library, since <literal>System.Web.Caching</literal> is available neither
in the .Net Standard nor in .Net Core.
</para>
<para>
The following NHibernate configuration settings are available:
</para>
<variablelist>
<varlistentry>
<term><literal>cache.default_expiration</literal></term>
<listitem>
Number of seconds to wait before expiring each item.
Defaults to <literal>300</literal>. It can also be set programmatically on the NHibernate
configuration object under the name <literal>expiration</literal>, which then takes precedence
over <literal>cache.default_expiration</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.use_sliding_expiration</literal></term>
<listitem>
Should the expiration be sliding? A sliding expiration is reinitialized at each get.
Defaults to <literal>false</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>priority</literal></term>
<listitem>
A numeric cost of expiring each item, where 1 is a low cost, 5 is the highest, and 3 is normal.
Only values 1 through 6 are valid. 6 is a special value corresponding to
<literal>NotRemovable</literal>. This setting can only be set programmatically through on the
NHibernate configuration object, by example with <literal>Configuration.SetProperty</literal>.
</listitem>
</varlistentry>
</variablelist>
<para>
SysCache has a config file section handler to allow configuring different expirations and priorities for
different regions. Here is an example:
</para>
<example>
<programlisting><![CDATA[<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="syscache"
type="NHibernate.Caches.SysCache.SysCacheSectionHandler,NHibernate.Caches.SysCache" />
</configSections>
<syscache>
<cache region="foo" expiration="500" priority="4" />
<cache region="bar" expiration="300" priority="3" sliding="true" />
</syscache>
</configuration>]]></programlisting>
</example>
</section>
<section id="NHibernate.Caches.SysCache2">
<title>SysCache2 Configuration</title>
<para>
SysCache2 uses <literal>System.Web.Caching</literal> like SysCache, but can use <literal>SqlCacheDependencies</literal>
to invalidate cache regions when data in an underlying SQL Server
table or query changes. Query dependencies are only available for SQL Server 2005 or higher. To use the cache
provider, the application must be setup and configured to support SQL notifications as described in the
MSDN documentation.
</para>
<para>
This is a .Net Framework only library, since <literal>System.Web.Caching</literal> is available neither
in the .Net Standard nor in .Net Core.
</para>
<para>
The following NHibernate configuration settings are available:
</para>
<variablelist>
<varlistentry>
<term><literal>cache.default_expiration</literal></term>
<listitem>
Number of seconds to wait before expiring each item.
Defaults to <literal>300</literal>. It can also be set programmatically on the NHibernate
configuration object under the name <literal>expiration</literal>, which then takes precedence
over <literal>cache.default_expiration</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.use_sliding_expiration</literal></term>
<listitem>
Should the expiration be sliding? A sliding expiration is reinitialized at each get.
Defaults to <literal>false</literal>.
</listitem>
</varlistentry>
</variablelist>
<para>
To configure cache regions with SqlCacheDependencies a <literal>syscache2</literal> config section must be
defined in the application's configuration file. See the sample below.
</para>
<example>
<programlisting><![CDATA[
<configSections>
<section name="syscache2"
type="NHibernate.Caches.SysCache2.SysCacheSection, NHibernate.Caches.SysCache2"/>
</configSections>]]></programlisting>
</example>
<section id="NHibernate.Caches.SysCache2-Table">
<title>Table-based Dependency</title>
<para>
A table-based dependency will monitor the data in a database table for changes. Table-based
dependencies are generally used for a SQL Server 2000 database but will work with SQL Server 2005 or superior as
well. Before you can use SQL Server cache invalidation with table based dependencies, you need to
enable notifications for the database. This task is performed with the <command>aspnet_regsql</command>
command. With table-based notifications, the application will poll the database for changes at a
predefined interval. A cache region will not be invalidated immediately when data in the table changes.
The cache will be invalidated the next time the application polls the database for changes.
</para>
<para>
To configure the data in a cache region to be invalidated when data in an underlying table is changed,
a cache region must be configured in the application's configuration file. See the sample below.
</para>
<example>
<programlisting><![CDATA[<syscache2>
<cacheRegion name="Product">
<dependencies>
<tables>
<add name="price"
databaseEntryName="Default"
tableName="VideoTitle" />
</tables>
</dependencies>
</cacheRegion>
</syscache2>]]></programlisting>
</example>
</section>
<variablelist>
<title>Table-based Dependency Configuration Properties</title>
<varlistentry>
<term><literal>name</literal></term>
<listitem>Unique name for the dependency</listitem>
</varlistentry>
<varlistentry>
<term><literal>tableName</literal></term>
<listitem>
The name of the database table that the dependency is associated with. The table must be enabled
for notification support with the <literal>AspNet_SqlCacheRegisterTableStoredProcedure</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>databaseEntryName</literal></term>
<listitem>
The name of a database defined in the <literal>databases</literal> element for
<literal>sqlCacheDependency</literal> for caching (ASP.NET Settings Schema) element of the
application's <literal>Web.config</literal> file.
</listitem>
</varlistentry>
</variablelist>
<section id="NHibernate.Caches.SysCache2-Command">
<title>Command-Based Dependencies</title>
<para>
A command-based dependency will use a SQL command to identify records to monitor for data changes.
Command-based dependencies work only with SQL Server 2005.
</para>
<para>
Before you can use SQL Server cache invalidation with command-based dependencies, you need to enable
the Service Broker for query notifications. The application must also start the listener for receiving
change notifications from SQL Server. With command based notifications, SQL Server will notify the
application when the data of a record returned in the results of a SQL query has changed. Note that a
change will be indicated if the data in any of the columns of a record change, not just the columns
returned by a query. The query is a way to limit the number of records monitored for changes, not the
columns. As soon as data in one of the records is modified, the data in the cache region will be
invalidated immediately.
</para>
<para>
To configure the data in a cache region to be invalidated based on a SQL command, a cache region must
be configured in the application's configuration file. See the samples below.
</para>
<example>
<title>Stored Procedure</title>
<programlisting><![CDATA[<cacheRegion name="Product" priority="High" >
<dependencies>
<commands>
<add name="price"
command="ActiveProductsStoredProcedure"
isStoredProcedure="true"/>
</commands>
</dependencies>
</cacheRegion>]]></programlisting>
</example>
<example>
<title>SELECT Statement</title>
<programlisting><![CDATA[<cacheRegion name="Product" priority="High">
<dependencies>
<commands>
<add name="price"
command="Select VideoTitleId from dbo.VideoTitle where Active = 1"
connectionName="default"
connectionStringProviderType="NHibernate.Caches.SysCache2.ConfigConnectionStringProvider, NHibernate.Caches.SysCache2"/>
</commands>
</dependencies>
</cacheRegion>]]></programlisting>
</example>
<variablelist>
<title>Command Configuration Properties</title>
<varlistentry>
<term><literal>name</literal></term>
<listitem>Unique name for the dependency</listitem>
</varlistentry>
<varlistentry>
<term><literal>command</literal> (required)</term>
<listitem>SQL command that returns results which should be monitored for data changes</listitem>
</varlistentry>
<varlistentry>
<term><literal>isStoredProcedure</literal> (optional)</term>
<listitem>
Indicates if command is a stored procedure. The default is <literal>false</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>connectionName</literal> (optional)</term>
<listitem>
The name of the connection in the applications configuration file to use for registering the
cache dependency for change notifications. If no value is supplied for <literal>connectionName
</literal> or <literal>connectionStringProviderType</literal>, the connection properties from
the NHibernate configuration will be used.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>connectionStringProviderType</literal> (optional)</term>
<listitem>
<classname>IConnectionStringProvider</classname> to use for retrieving the connection string to
use for registering the cache dependency for change notifications. If no value is supplied for
<literal>connectionName</literal>, the unnamed connection supplied by the provider will be
used.
</listitem>
</varlistentry>
</variablelist>
</section>
<section id="NHibernate.Caches.SysCache2-Aggregate">
<title>Aggregate Dependencies</title>
<para>
Multiple cache dependencies can be specified. If any of the dependencies triggers a change
notification, the data in the cache region will be invalidated. See the samples below.
</para>
<example>
<title>Multiple commands</title>
<programlisting><![CDATA[<cacheRegion name="Product">
<dependencies>
<commands>
<add name="price"
command="ActiveProductsStoredProcedure"
isStoredProcedure="true"/>
<add name="quantity"
command="Select quantityAvailable from dbo.VideoAvailability"/>
</commands>
</dependencies>
</cacheRegion>]]></programlisting>
</example>
<example>
<title>Mixed</title>
<programlisting><![CDATA[<cacheRegion name="Product">
<dependencies>
<commands>
<add name="price"
command="ActiveProductsStoredProcedure"
isStoredProcedure="true"/>
</commands>
<tables>
<add name="quantity"
databaseEntryName="Default"
tableName=" VideoAvailability" />
</tables>
</dependencies>
</cacheRegion>]]></programlisting>
</example>
</section>
<section id="NHibernate.Caches.SysCache2-AdditionalSettings">
<title>Additional Settings</title>
<para>
In addition to data dependencies for the cache regions, time based expiration policies can be specified
for each item added to the cache. Time based expiration policies will not invalidate the data
dependencies for the whole cache region, but serve as a way to remove items from the cache after they
have been in the cache for a specified amount of time. See the samples below.
</para>
<example>
<title>Relative Expiration</title>
<programlisting><![CDATA[<cacheRegion name="Product" relativeExpiration="300" priority="High" useSlidingExpiration="true" />]]></programlisting>
</example>
<example>
<title>Time of Day Expiration</title>
<programlisting><![CDATA[<cacheRegion name="Product" timeOfDayExpiration="2:00:00" priority="High" />]]></programlisting>
</example>
<variablelist>
<title>Additional Configuration Properties</title>
<varlistentry>
<term><literal>relativeExpiration</literal></term>
<listitem>
<para>
Number of seconds that an individual item will exist in the cache before being removed.
Defaults to <literal>300</literal> if neither <literal>relativeExpiration</literal> nor
<literal>timeOfDayExpiration</literal> are defined, and if no expiration settings are
defined in NHibernate configuration.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>useSlidingExpiration</literal></term>
<listitem>
<para>
Should the expiration be sliding? A sliding expiration is reinitialized at each get.
Defaults to <literal>false</literal> if not defined in NHibernate configuration.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>timeOfDayExpiration</literal></term>
<listitem>
<para>
24 hour based time of day that an item will exist in the cache until. 12am is specified as
00:00:00; 10pm is specified as 22:00:00. Only valid if relativeExpiration is not specified.
Time of Day Expiration is useful for scenarios where items should be expired from the cache
after a daily process completes.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>priority</literal></term>
<listitem>
<classname>System.Web.Caching.CacheItemPriority</classname> that identifies the relative
priority of items stored in the cache.
</listitem>
</varlistentry>
</variablelist>
</section>
</section>
<section id="NHibernate.Caches.EnyimMemcached">
<title>EnyimMemcached Configuration</title>
<para>
Its configuration relies on the EnyimMemcached library own configuration, through its
<literal>enyim.com/memcached</literal> configuration section. See
<ulink url="https://github.com/enyim/EnyimMemcached">project site</ulink>.
This is a .Net Framework only library.
</para>
</section>
<section id="NHibernate.Caches.RtMemoryCache">
<title>RtMemoryCache Configuration</title>
<para>
RtMemoryCache relies on <classname>System.Runtime.Caching.MemoryCache</classname> for the
underlying implementation.
</para>
<para>
The following NHibernate configuration settings are available:
</para>
<variablelist>
<varlistentry>
<term><literal>cache.default_expiration</literal></term>
<listitem>
Number of seconds to wait before expiring each item.
Defaults to <literal>300</literal>. It can also be set programmatically on the NHibernate
configuration object under the name <literal>expiration</literal>, which then takes precedence
over <literal>cache.default_expiration</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.use_sliding_expiration</literal></term>
<listitem>
Should the expiration be sliding? A sliding expiration is reinitialized at each get.
Defaults to <literal>false</literal>.
</listitem>
</varlistentry>
</variablelist>
<para>
RtMemoryCache has a config file section handler to allow configuring different expirations for
different regions. Here is an example:
</para>
<example>
<programlisting><![CDATA[<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="rtmemorycache"
type="NHibernate.Caches.RtMemoryCache.RtMemoryCacheSectionHandler,NHibernate.Caches.RtMemoryCache" />
</configSections>
<rtmemorycache>
<cache region="foo" expiration="500" />
<cache region="bar" expiration="300" sliding="true" />
</rtmemorycache>
</configuration>]]></programlisting>
</example>
<para>
The loading of this section can be customized with the
<literal>NHibernate.Caches.RtMemoryCache.ConfigurationProvider</literal> class. See
<xref linkend="NHibernate.Caches.ConfigurationProvider" />.
</para>
</section>
<section id="NHibernate.Caches.StackExchangeRedis">
<title>NHibernate.Caches.StackExchangeRedis Configuration</title>
<para>
NHibernate.Caches.StackExchangeRedis relies on <classname>StackExchange.Redis</classname> for the
underlying implementation.
</para>
<para>
The following NHibernate configuration settings are available (also defined in
<literal>NHibernate.Caches.StackExchangeRedis.RedisEnvironment</literal>):
</para>
<variablelist>
<varlistentry>
<term><literal>cache.default_expiration</literal></term>
<listitem>
Number of seconds to wait before expiring each item.
Defaults to <literal>300</literal>. It can also be set programmatically on the NHibernate
configuration object under the name <literal>expiration</literal>, which then takes precedence
over <literal>cache.default_expiration</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.use_sliding_expiration</literal></term>
<listitem>
Should the expiration be sliding? A sliding expiration is reinitialized at each get. Can be overriden for each region by using
<literal>sliding</literal> attribute.
Defaults to <literal>false</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.database</literal></term>
<listitem>
The default Redis database index, that can be overriden for each region by using <literal>database</literal> attribute.
Defaults to <literal>-1</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.strategy</literal></term>
<listitem>
The assembly qualified name of the region strategy, that can be overriden for each region by using <literal>strategy</literal> attribute.
<literal>NHibernate.Caches.StackExchangeRedis</literal> provides the following strategies:
<itemizedlist>
<listitem>
<literal>NHibernate.Caches.StackExchangeRedis.DefaultRegionStrategy</literal>
<para>
Uses a special key that contains the region current version number which is appended after the region prefix.
Each time a clear operation is performed the version number is increased and an event is send to all clients
so that they can update their local versions. Even if the event was not sent to all clients, each operation has a
version check in order to prevent working with stale data. This strategy has additional settings:
<varlistentry>
<term><literal>cache.region_strategy.default.max_allowed_version</literal></term>
<listitem>
The max allowed version number. When the max value is reached, the next value will be reset to zero.
Defaults to <literal>10000</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.region_strategy.default.use_pubsub</literal></term>
<listitem>
Whether to use Redis pub/sub mechanism in order to notify other cache instances when the clear operation was performed.
Defaults to <literal>true</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.region_strategy.default.retry_times</literal></term>
<listitem>
Total retry times for read and lock operations, when concurrent clear operations are performed.
Defaults to <literal>1</literal>.
</listitem>
</varlistentry>
</para>
</listitem>
<listitem>
<literal>NHibernate.Caches.StackExchangeRedis.FastRegionStrategy</literal>
<para>
Uses very simple read/write operations but does not support <literal>ICache.Clear</literal> operation.
</para>
</listitem>
<listitem>
<literal>NHibernate.Caches.StackExchangeRedis.TwoLayerCacheRegionStrategy</literal>
<para>
Extends <literal>NHibernate.Caches.StackExchangeRedis.DefaultRegionStrategy</literal> and uses
an additional local memory cache for faster readings. The local caches are invalidated by using Redis pub/sub mechanism.
This strategy should be used only for regions that have few write operations and a high expiration time.
This strategy inherits additional settings from <literal>DefaultRegionStrategy</literal> and also has its own settings:
<varlistentry>
<term><literal>cache.region_strategy.two_layer_cache.use_pipelining</literal></term>
<listitem>
Whether to use <literal>StackExchange.Redis</literal> pipelining feature.
Defaults to <literal>false</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.region_strategy.two_layer_cache.client_id</literal></term>
<listitem>
The client id used for cache invalidation.
Defaults to a random number.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.region_strategy.two_layer_cache.max_synchronization_time</literal></term>
<listitem>
The max synchronization time between caches in seconds.
Defaults to <literal>10</literal>.
</listitem>
</varlistentry>
</para>
</listitem>
<listitem>
<literal>NHibernate.Caches.StackExchangeRedis.FastTwoLayerCacheRegionStrategy</literal>
<para>
Extends <literal>NHibernate.Caches.StackExchangeRedis.FastRegionStrategy</literal> and uses
an additional local memory cache for faster readings. The local caches are invalidated by using Redis pub/sub mechanism.
This strategy does not support <literal>ICache.Clear</literal> operation and should be used only for regions that have
few write operations and a high expiration time. This strategy has additional settings:
<varlistentry>
<term><literal>cache.region_strategy.fast_two_layer_cache.use_pipelining</literal></term>
<listitem>
Whether to use <literal>StackExchange.Redis</literal> pipelining feature.
Defaults to <literal>false</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.region_strategy.fast_two_layer_cache.client_id</literal></term>
<listitem>
The client id used for cache invalidation.
Defaults to a random number.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.region_strategy.fast_two_layer_cache.max_synchronization_time</literal></term>
<listitem>
The max synchronization time between caches in seconds.
Defaults to <literal>10</literal>.
</listitem>
</varlistentry>
</para>
</listitem>
<listitem>
<literal>NHibernate.Caches.StackExchangeRedis.DistributedLocalCacheRegionStrategy</literal>
<para>
Uses only a memory cache to store the values and uses Redis pub/sub mechanism to synchronize data between other local caches.
The synchronization between caches is done by comparing the UTC <literal>DateTime.Ticks</literal>, which represent when the
operation was performed. When two operations have the same <literal>DateTime.Ticks</literal>, then the client with the highest
id wins. This strategy should be used only for regions that have few write operations and a high expiration time. It is recommended
to use <literal>NHibernate.Caches.StackExchangeRedis.TwoLayerCacheRegionStrategy</literal>, when the instances where the strategy
would run are often restarted/recycled. In order to use this strategy a custom <literal>ICacheRegionStrategyFactory</literal>
has to be provided (see <literal>cache.region_strategy_factory</literal> setting), where the strategy is created with a custom
<literal>RegionMemoryCacheBase</literal> implementation. This strategy has additional settings:
<varlistentry>
<term><literal>cache.region_strategy.distributed_local_cache.use_pipelining</literal></term>
<listitem>
Whether to use <literal>StackExchange.Redis</literal> pipelining feature.
Defaults to <literal>false</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.region_strategy.distributed_local_cache.client_id</literal></term>
<listitem>
The client id used for cache invalidation.
Defaults to a random number.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.region_strategy.distributed_local_cache.max_synchronization_time</literal></term>
<listitem>
The max synchronization time between caches in seconds.
Defaults to <literal>10</literal>.
</listitem>
</varlistentry>
</para>
</listitem>
</itemizedlist>
Defaults to <literal>NHibernate.Caches.StackExchangeRedis.DefaultRegionStrategy</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.append_hashcode</literal></term>
<listitem>
Whether the hash code of the key should be added to the cache key. Can be overriden for each region by using <literal>append-hashcode</literal> attribute.
Defaults to <literal>false</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.key_prefix</literal></term>
<listitem>
The prefix that will be prepended before each cache key in order to avoid having collisions when multiple clients uses the same Redis database.
Defaults to <literal>NHibernate-Cache:</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.environment_name</literal></term>
<listitem>
The name of the environment that will be prepended before each cache key in order to allow having multiple environments on the same Redis database.
Defaults to <literal>null</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.serializer</literal></term>
<listitem>
The assembly qualified name of the serializer that is used to serialize/deserialize the key values. Optionally, a faster json serializer can be
used by installing <literal>NHibernate.Caches.Util.JsonSerializer</literal> package and setting
<literal>NHibernate.Caches.Util.JsonSerializer.JsonCacheSerializer, NHibernate.Caches.Util.JsonSerializer</literal> value instead.
Defaults to <literal>NHibernate.Caches.Common.BinaryCacheSerializer, NHibernate.Caches.Common</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.region_strategy_factory</literal></term>
<listitem>
The assembly qualified name of the region strategy factory.
Defaults to <literal>NHibernate.Caches.StackExchangeRedis.DefaultCacheRegionStrategyFactory</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.connection_multiplexer_provider</literal></term>
<listitem>
The assembly qualified name of the connection multiplexer provider.
Defaults to <literal>NHibernate.Caches.StackExchangeRedis.DefaultConnectionMultiplexerProvider</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.database_provider</literal></term>
<listitem>
The assembly qualified name of the database provider.
Defaults to <literal>NHibernate.Caches.StackExchangeRedis.DefaultDatabaseProvider</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.lock.key_timeout</literal></term>
<listitem>
The timeout for a lock key to expire in seconds.
Defaults to <literal>5</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.lock.acquire_timeout</literal></term>
<listitem>
The time limit to acquire the lock in seconds.
Defaults to <literal>5</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.lock.retry_times</literal></term>
<listitem>
The number of retries for acquiring the lock.
Defaults to <literal>3</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.lock.max_retry_delay</literal></term>
<listitem>
The maximum delay before retrying to acquire the lock in milliseconds.
Defaults to <literal>400</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.lock.min_retry_delay</literal></term>
<listitem>
The minimum delay before retrying to acquire the lock in milliseconds.
Defaults to <literal>10</literal>.
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cache.lock.value_provider</literal></term>