forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheFactory.cs
122 lines (107 loc) · 4.45 KB
/
CacheFactory.cs
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
using System;
using System.Collections.Generic;
using NHibernate.Cfg;
using NHibernate.Util;
namespace NHibernate.Cache
{
/// <summary>
/// Factory class for creating an <see cref="ICacheConcurrencyStrategy"/>.
/// </summary>
public static class CacheFactory
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(CacheFactory));
public const string ReadOnly = "read-only";
public const string ReadWrite = "read-write";
public const string NonstrictReadWrite = "nonstrict-read-write";
/// <remarks>
/// No providers implement transactional caching currently,
/// it was ported from Hibernate just for the sake of completeness.
/// </remarks>
public const string Transactional = "transactional";
/// <summary>
/// Never interact with second level cache or UpdateTimestampsCache.
/// </summary>
public const string Never = "never";
/// <summary>
/// Creates an <see cref="ICacheConcurrencyStrategy"/> from the parameters.
/// </summary>
/// <param name="usage">The name of the strategy that <see cref="ICacheProvider"/> should use for the class.</param>
/// <param name="name">The name of the class the strategy is being created for.</param>
/// <param name="mutable"><see langword="true" /> if the object being stored in the cache is mutable.</param>
/// <param name="settings">Used to retrieve the global cache region prefix.</param>
/// <param name="properties">Properties the cache provider can use to configure the cache.</param>
/// <returns>An <see cref="ICacheConcurrencyStrategy"/> to use for this object in the <see cref="ICache"/>.</returns>
// Since v5.3
[Obsolete("Please use overload with a CacheBase parameter.")]
public static ICacheConcurrencyStrategy CreateCache(
string usage,
string name,
bool mutable,
Settings settings,
IDictionary<string, string> properties)
{
if (usage == null || !settings.IsSecondLevelCacheEnabled) return null;
var cache = BuildCacheBase(name, settings, properties);
var ccs = CreateCache(usage, cache, settings);
if (mutable && usage == ReadOnly)
log.Warn("read-only cache configured for mutable: {0}", name);
return ccs;
}
/// <summary>
/// Creates an <see cref="ICacheConcurrencyStrategy"/> from the parameters.
/// </summary>
/// <param name="usage">The name of the strategy that <see cref="ICacheProvider"/> should use for the class.</param>
/// <param name="cache">The <see cref="CacheBase"/> used for this strategy.</param>
/// <returns>An <see cref="ICacheConcurrencyStrategy"/> to use for this object in the <see cref="ICache"/>.</returns>
// TODO: Since v5.4
//[Obsolete("Please use overload with a CacheBase and Settings parameters.")]
public static ICacheConcurrencyStrategy CreateCache(string usage, CacheBase cache)
{
return CreateCache(usage, cache, null);
}
/// <summary>
/// Creates an <see cref="ICacheConcurrencyStrategy"/> from the parameters.
/// </summary>
/// <param name="usage">The name of the strategy that <see cref="ICacheProvider"/> should use for the class.</param>
/// <param name="cache">The <see cref="CacheBase"/> used for this strategy.</param>
/// <param name="settings">NHibernate settings</param>
/// <returns>An <see cref="ICacheConcurrencyStrategy"/> to use for this object in the <see cref="ICache"/>.</returns>
public static ICacheConcurrencyStrategy CreateCache(string usage, CacheBase cache, Settings settings)
{
if (log.IsDebugEnabled())
log.Debug("cache for: {0} usage strategy: {1}", cache.RegionName, usage);
ICacheConcurrencyStrategy ccs;
switch (usage)
{
case ReadOnly:
ccs = new ReadOnlyCache();
break;
case ReadWrite:
ccs = new ReadWriteCache(settings == null ? new AsyncReaderWriterLock() : settings.CacheReadWriteLockFactory.Create());
break;
case NonstrictReadWrite:
ccs = new NonstrictReadWriteCache();
break;
//case CacheFactory.Transactional:
// ccs = new TransactionalCache();
// break;
default:
throw new MappingException(
"cache usage attribute should be read-write, read-only or nonstrict-read-write");
}
ccs.Cache = cache;
return ccs;
}
internal static CacheBase BuildCacheBase(string name, Settings settings, IDictionary<string, string> properties)
{
try
{
return settings.CacheProvider.BuildCache(name, properties).AsCacheBase();
}
catch (CacheException e)
{
throw new HibernateException("Could not instantiate cache implementation", e);
}
}
}
}