Skip to content

Commit dbdfcda

Browse files
maca88fredericDelaporte
authored andcommitted
Add static region configuration (#68)
1 parent 049d69b commit dbdfcda

File tree

6 files changed

+289
-234
lines changed

6 files changed

+289
-234
lines changed

CoreDistributedCache/NHibernate.Caches.CoreDistributedCache/CoreDistributedCacheProvider.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ public class CoreDistributedCacheProvider : ICacheProvider
7171
/// </remarks>
7272
public static bool AppendHashcodeToKey { get; set; }
7373

74+
/// <summary>
75+
/// Set a region configuration.
76+
/// </summary>
77+
/// <param name="configuration">The region configuration.</param>
78+
public static void SetRegionConfiguration(RegionConfig configuration)
79+
{
80+
ConfiguredCachesProperties[configuration.Region] = configuration.Properties;
81+
}
82+
7483
static CoreDistributedCacheProvider()
7584
{
7685
Log = NHibernateLogger.For(typeof(CoreDistributedCacheProvider));

CoreMemoryCache/NHibernate.Caches.CoreMemoryCache/CoreMemoryCacheProvider.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ public class CoreMemoryCacheProvider : ICacheProvider
5656
/// </remarks>
5757
public static TimeSpan? ExpirationScanFrequency { get; set; }
5858

59+
/// <summary>
60+
/// Set a region configuration.
61+
/// </summary>
62+
/// <param name="configuration">The region configuration.</param>
63+
public static void SetRegionConfiguration(RegionConfig configuration)
64+
{
65+
ConfiguredCachesProperties[configuration.Region] = configuration.Properties;
66+
}
67+
5968
static CoreMemoryCacheProvider()
6069
{
6170
Log = NHibernateLogger.For(typeof(CoreMemoryCacheProvider));

RtMemoryCache/NHibernate.Caches.RtMemoryCache/RtMemoryCacheProvider.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ public class RtMemoryCacheProvider : ICacheProvider
3535
private static readonly Dictionary<string, IDictionary<string, string>> ConfiguredCachesProperties;
3636
private static readonly INHibernateLogger Log;
3737

38+
/// <summary>
39+
/// Set a region configuration.
40+
/// </summary>
41+
/// <param name="configuration">The region configuration.</param>
42+
public static void SetRegionConfiguration(CacheConfig configuration)
43+
{
44+
ConfiguredCachesProperties[configuration.Region] = configuration.Properties;
45+
}
46+
3847
static RtMemoryCacheProvider()
3948
{
4049
Log = NHibernateLogger.For(typeof(RtMemoryCacheProvider));

StackExchangeRedis/NHibernate.Caches.StackExchangeRedis/RedisCacheProvider.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ public static RedisCacheConfiguration DefaultCacheConfiguration
2626
set => _defaultCacheConfiguration = value ?? new RedisCacheConfiguration();
2727
}
2828

29+
/// <summary>
30+
/// Set a region configuration.
31+
/// </summary>
32+
/// <param name="regionName">The region name.</param>
33+
/// <param name="configuration">The region configuration.</param>
34+
public static void SetRegionConfiguration(string regionName, RegionConfig configuration)
35+
{
36+
ConfiguredCacheRegions[regionName] = configuration;
37+
}
38+
2939
static RedisCacheProvider()
3040
{
3141
Log = NHibernateLogger.For(typeof(RedisCacheProvider));
Lines changed: 131 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,131 @@
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

Comments
 (0)