|
1 |
| -#region License |
2 |
| - |
3 |
| -// |
4 |
| -// SysCache - A cache provider for NHibernate using System.Web.Caching.Cache. |
5 |
| -// |
6 |
| -// This library is free software; you can redistribute it and/or |
7 |
| -// modify it under the terms of the GNU Lesser General Public |
8 |
| -// License as published by the Free Software Foundation; either |
9 |
| -// version 2.1 of the License, or (at your option) any later version. |
10 |
| -// |
11 |
| -// This library is distributed in the hope that it will be useful, |
12 |
| -// but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
| -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
| -// Lesser General Public License for more details. |
15 |
| -// |
16 |
| -// You should have received a copy of the GNU Lesser General Public |
17 |
| -// License along with this library; if not, write to the Free Software |
18 |
| -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
| -// |
20 |
| - |
21 |
| -#endregion |
22 |
| - |
23 |
| -using System.Collections.Generic; |
24 |
| -using System.Configuration; |
25 |
| -using System.Text; |
26 |
| -using NHibernate.Cache; |
27 |
| - |
28 |
| -namespace NHibernate.Caches.SysCache |
29 |
| -{ |
30 |
| - /// <summary> |
31 |
| - /// Cache provider using the System.Web.Caching classes. |
32 |
| - /// </summary> |
33 |
| - public class SysCacheProvider : ICacheProvider |
34 |
| - { |
35 |
| - private static readonly Dictionary<string, IDictionary<string, string>> ConfiguredCachesProperties; |
36 |
| - private static readonly INHibernateLogger Log; |
37 |
| - |
38 |
| - static SysCacheProvider() |
39 |
| - { |
40 |
| - Log = NHibernateLogger.For(typeof(SysCacheProvider)); |
41 |
| - ConfiguredCachesProperties = new Dictionary<string, IDictionary<string, string>>(); |
42 |
| - |
43 |
| - if (!(ConfigurationManager.GetSection("syscache") is CacheConfig[] list)) |
44 |
| - return; |
45 |
| - foreach (var cache in list) |
46 |
| - { |
47 |
| - ConfiguredCachesProperties.Add(cache.Region, cache.Properties); |
48 |
| - } |
49 |
| - } |
50 |
| - |
51 |
| - #region ICacheProvider Members |
52 |
| - |
53 |
| - /// <inheritdoc /> |
54 |
| -#pragma warning disable 618 |
55 |
| - public ICache BuildCache(string regionName, IDictionary<string, string> properties) |
56 |
| -#pragma warning restore 618 |
57 |
| - { |
58 |
| - if (regionName == null) |
59 |
| - { |
60 |
| - regionName = string.Empty; |
61 |
| - } |
62 |
| - |
63 |
| - if (ConfiguredCachesProperties.TryGetValue(regionName, out var configuredProperties) && configuredProperties.Count > 0) |
64 |
| - { |
65 |
| - if (properties != null) |
66 |
| - { |
67 |
| - // Duplicate it for not altering the global configuration |
68 |
| - properties = new Dictionary<string, string>(properties); |
69 |
| - foreach (var prop in configuredProperties) |
70 |
| - { |
71 |
| - properties[prop.Key] = prop.Value; |
72 |
| - } |
73 |
| - } |
74 |
| - else |
75 |
| - { |
76 |
| - properties = configuredProperties; |
77 |
| - } |
78 |
| - } |
79 |
| - |
80 |
| - // create cache |
81 |
| - if (properties == null) |
82 |
| - { |
83 |
| - properties = new Dictionary<string, string>(1); |
84 |
| - } |
85 |
| - |
86 |
| - if (Log.IsDebugEnabled()) |
87 |
| - { |
88 |
| - var sb = new StringBuilder(); |
89 |
| - |
90 |
| - foreach (var de in properties) |
91 |
| - { |
92 |
| - sb.Append("name="); |
93 |
| - sb.Append(de.Key); |
94 |
| - sb.Append("&value="); |
95 |
| - sb.Append(de.Value); |
96 |
| - sb.Append(";"); |
97 |
| - } |
98 |
| - |
99 |
| - Log.Debug("building cache with region: {0}, properties: {1}" , regionName, sb.ToString()); |
100 |
| - } |
101 |
| - return new SysCache(regionName, properties); |
102 |
| - } |
103 |
| - |
104 |
| - /// <inheritdoc /> |
105 |
| - public long NextTimestamp() |
106 |
| - { |
107 |
| - return Timestamper.Next(); |
108 |
| - } |
109 |
| - |
110 |
| - /// <inheritdoc /> |
111 |
| - public void Start(IDictionary<string, string> properties) |
112 |
| - { |
113 |
| - } |
114 |
| - |
115 |
| - /// <inheritdoc /> |
116 |
| - public void Stop() |
117 |
| - { |
118 |
| - } |
119 |
| - |
120 |
| - #endregion |
121 |
| - } |
122 |
| -} |
| 1 | +#region License |
| 2 | + |
| 3 | +// |
| 4 | +// SysCache - A cache provider for NHibernate using System.Web.Caching.Cache. |
| 5 | +// |
| 6 | +// This library is free software; you can redistribute it and/or |
| 7 | +// modify it under the terms of the GNU Lesser General Public |
| 8 | +// License as published by the Free Software Foundation; either |
| 9 | +// version 2.1 of the License, or (at your option) any later version. |
| 10 | +// |
| 11 | +// This library is distributed in the hope that it will be useful, |
| 12 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | +// Lesser General Public License for more details. |
| 15 | +// |
| 16 | +// You should have received a copy of the GNU Lesser General Public |
| 17 | +// License along with this library; if not, write to the Free Software |
| 18 | +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | +// |
| 20 | + |
| 21 | +#endregion |
| 22 | + |
| 23 | +using System.Collections.Generic; |
| 24 | +using System.Configuration; |
| 25 | +using System.Text; |
| 26 | +using NHibernate.Cache; |
| 27 | + |
| 28 | +namespace NHibernate.Caches.SysCache |
| 29 | +{ |
| 30 | + /// <summary> |
| 31 | + /// Cache provider using the System.Web.Caching classes. |
| 32 | + /// </summary> |
| 33 | + public class SysCacheProvider : ICacheProvider |
| 34 | + { |
| 35 | + private static readonly Dictionary<string, IDictionary<string, string>> ConfiguredCachesProperties; |
| 36 | + private static readonly INHibernateLogger Log; |
| 37 | + |
| 38 | + static SysCacheProvider() |
| 39 | + { |
| 40 | + Log = NHibernateLogger.For(typeof(SysCacheProvider)); |
| 41 | + ConfiguredCachesProperties = new Dictionary<string, IDictionary<string, string>>(); |
| 42 | + |
| 43 | + if (!(ConfigurationManager.GetSection("syscache") is CacheConfig[] list)) |
| 44 | + return; |
| 45 | + foreach (var cache in list) |
| 46 | + { |
| 47 | + ConfiguredCachesProperties.Add(cache.Region, cache.Properties); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Set a region configuration. |
| 53 | + /// </summary> |
| 54 | + /// <param name="configuration">The region configuration.</param> |
| 55 | + public static void SetRegionConfiguration(CacheConfig configuration) |
| 56 | + { |
| 57 | + ConfiguredCachesProperties[configuration.Region] = configuration.Properties; |
| 58 | + } |
| 59 | + |
| 60 | + #region ICacheProvider Members |
| 61 | + |
| 62 | + /// <inheritdoc /> |
| 63 | +#pragma warning disable 618 |
| 64 | + public ICache BuildCache(string regionName, IDictionary<string, string> properties) |
| 65 | +#pragma warning restore 618 |
| 66 | + { |
| 67 | + if (regionName == null) |
| 68 | + { |
| 69 | + regionName = string.Empty; |
| 70 | + } |
| 71 | + |
| 72 | + if (ConfiguredCachesProperties.TryGetValue(regionName, out var configuredProperties) && configuredProperties.Count > 0) |
| 73 | + { |
| 74 | + if (properties != null) |
| 75 | + { |
| 76 | + // Duplicate it for not altering the global configuration |
| 77 | + properties = new Dictionary<string, string>(properties); |
| 78 | + foreach (var prop in configuredProperties) |
| 79 | + { |
| 80 | + properties[prop.Key] = prop.Value; |
| 81 | + } |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + properties = configuredProperties; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // create cache |
| 90 | + if (properties == null) |
| 91 | + { |
| 92 | + properties = new Dictionary<string, string>(1); |
| 93 | + } |
| 94 | + |
| 95 | + if (Log.IsDebugEnabled()) |
| 96 | + { |
| 97 | + var sb = new StringBuilder(); |
| 98 | + |
| 99 | + foreach (var de in properties) |
| 100 | + { |
| 101 | + sb.Append("name="); |
| 102 | + sb.Append(de.Key); |
| 103 | + sb.Append("&value="); |
| 104 | + sb.Append(de.Value); |
| 105 | + sb.Append(";"); |
| 106 | + } |
| 107 | + |
| 108 | + Log.Debug("building cache with region: {0}, properties: {1}" , regionName, sb.ToString()); |
| 109 | + } |
| 110 | + return new SysCache(regionName, properties); |
| 111 | + } |
| 112 | + |
| 113 | + /// <inheritdoc /> |
| 114 | + public long NextTimestamp() |
| 115 | + { |
| 116 | + return Timestamper.Next(); |
| 117 | + } |
| 118 | + |
| 119 | + /// <inheritdoc /> |
| 120 | + public void Start(IDictionary<string, string> properties) |
| 121 | + { |
| 122 | + } |
| 123 | + |
| 124 | + /// <inheritdoc /> |
| 125 | + public void Stop() |
| 126 | + { |
| 127 | + } |
| 128 | + |
| 129 | + #endregion |
| 130 | + } |
| 131 | +} |
0 commit comments