Adding warning notification if Redis configuration string is not set

This commit is contained in:
Jasmin Savard
2016-05-26 15:51:59 -04:00
committed by Sébastien Ros
parent b7414e6f42
commit 6198a9d4fb
8 changed files with 148 additions and 10 deletions

View File

@@ -4,6 +4,8 @@ using System.Configuration;
using Orchard.Environment.Configuration; using Orchard.Environment.Configuration;
using Orchard.Logging; using Orchard.Logging;
using StackExchange.Redis; using StackExchange.Redis;
using Orchard.UI.Notify;
using Orchard.Localization;
namespace Orchard.Redis.Configuration { namespace Orchard.Redis.Configuration {
@@ -16,6 +18,8 @@ namespace Orchard.Redis.Configuration {
Logger = NullLogger.Instance; Logger = NullLogger.Instance;
} }
public Localizer T { get; set; }
public ILogger Logger { get; set; } public ILogger Logger { get; set; }
public string GetConnectionString(string service) { public string GetConnectionString(string service) {
@@ -25,7 +29,7 @@ namespace Orchard.Redis.Configuration {
var connectionStringSettings = ConfigurationManager.ConnectionStrings[_tenantSettingsKey] ?? ConfigurationManager.ConnectionStrings[_defaultSettingsKey]; var connectionStringSettings = ConfigurationManager.ConnectionStrings[_tenantSettingsKey] ?? ConfigurationManager.ConnectionStrings[_defaultSettingsKey];
if (connectionStringSettings == null) { if (connectionStringSettings == null) {
throw new ConfigurationErrorsException("A connection string is expected for " + service); return null;
} }
return connectionStringSettings.ConnectionString; return connectionStringSettings.ConnectionString;
@@ -34,7 +38,7 @@ namespace Orchard.Redis.Configuration {
public ConnectionMultiplexer GetConnection(string connectionString) { public ConnectionMultiplexer GetConnection(string connectionString) {
if (String.IsNullOrWhiteSpace(connectionString)) { if (String.IsNullOrWhiteSpace(connectionString)) {
throw new ArgumentNullException("connectionString"); return null;
} }
// when using ConcurrentDictionary, multiple threads can create the value // when using ConcurrentDictionary, multiple threads can create the value
@@ -49,4 +53,4 @@ namespace Orchard.Redis.Configuration {
})).Value; })).Value;
} }
} }
} }

View File

@@ -14,7 +14,7 @@ namespace Orchard.Redis.MessageBus {
public class RedisMessageBusBroker : Component, IMessageBroker { public class RedisMessageBusBroker : Component, IMessageBroker {
private readonly IRedisConnectionProvider _redisConnectionProvider; private readonly IRedisConnectionProvider _redisConnectionProvider;
private readonly ConnectionMultiplexer _connectionMultiplexer;
public const string ConnectionStringKey = "Orchard.Redis.MessageBus"; public const string ConnectionStringKey = "Orchard.Redis.MessageBus";
private readonly string _connectionString; private readonly string _connectionString;
@@ -23,15 +23,19 @@ namespace Orchard.Redis.MessageBus {
public RedisMessageBusBroker(ShellSettings shellSettings, IRedisConnectionProvider redisConnectionProvider) { public RedisMessageBusBroker(ShellSettings shellSettings, IRedisConnectionProvider redisConnectionProvider) {
_redisConnectionProvider = redisConnectionProvider; _redisConnectionProvider = redisConnectionProvider;
_connectionString = _redisConnectionProvider.GetConnectionString(ConnectionStringKey); _connectionString = _redisConnectionProvider.GetConnectionString(ConnectionStringKey);
_connectionMultiplexer = _redisConnectionProvider.GetConnection(_connectionString);
} }
public IDatabase Database { public IDatabase Database {
get { get {
return _redisConnectionProvider.GetConnection(_connectionString).GetDatabase(); return _connectionMultiplexer.GetDatabase();
} }
} }
public void Subscribe(string channel, Action<string, string> handler) { public void Subscribe(string channel, Action<string, string> handler) {
if (_connectionMultiplexer == null) {
return;
}
try { try {
var channelHandlers = _handlers.GetOrAdd(channel, c => { var channelHandlers = _handlers.GetOrAdd(channel, c => {
@@ -68,6 +72,10 @@ namespace Orchard.Redis.MessageBus {
} }
public void Publish(string channel, string message) { public void Publish(string channel, string message) {
if (_connectionMultiplexer == null) {
return;
}
Database.Publish(channel, GetHostName() + "/" + message); Database.Publish(channel, GetHostName() + "/" + message);
} }

View File

@@ -81,6 +81,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="Web.config" /> <Content Include="Web.config" />
<Compile Include="Services\RedisMessageBusNotificationProvider.cs" />
<Compile Include="Services\RedisOutputCacheNotificationProvider.cs" />
<Compile Include="Services\RedisCacheNotificationProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Content Include="Module.txt" /> <Content Include="Module.txt" />
</ItemGroup> </ItemGroup>

View File

@@ -54,7 +54,11 @@ namespace Orchard.Redis.OutputCache {
} }
public void Set(string key, CacheItem cacheItem) { public void Set(string key, CacheItem cacheItem) {
if(cacheItem == null) { if (_connectionMultiplexer == null) {
return;
}
if (cacheItem == null) {
throw new ArgumentNullException("cacheItem"); throw new ArgumentNullException("cacheItem");
} }
@@ -70,14 +74,26 @@ namespace Orchard.Redis.OutputCache {
} }
public void Remove(string key) { public void Remove(string key) {
if (_connectionMultiplexer == null) {
return;
}
Database.KeyDelete(GetLocalizedKey(key)); Database.KeyDelete(GetLocalizedKey(key));
} }
public void RemoveAll() { public void RemoveAll() {
if (_connectionMultiplexer == null) {
return;
}
Database.KeyDeleteWithPrefix(GetLocalizedKey("*")); Database.KeyDeleteWithPrefix(GetLocalizedKey("*"));
} }
public CacheItem GetCacheItem(string key) { public CacheItem GetCacheItem(string key) {
if (_connectionMultiplexer == null) {
return null;
}
var value = Database.StringGet(GetLocalizedKey(key)); var value = Database.StringGet(GetLocalizedKey(key));
if (value.IsNullOrEmpty) { if (value.IsNullOrEmpty) {
@@ -85,11 +101,11 @@ namespace Orchard.Redis.OutputCache {
} }
using (var compressedStream = new MemoryStream(value)) { using (var compressedStream = new MemoryStream(value)) {
if(compressedStream.Length == 0) { if (compressedStream.Length == 0) {
return null; return null;
} }
using(var decompressedStream = Decompress(compressedStream)) { using (var decompressedStream = Decompress(compressedStream)) {
return Deserialize(decompressedStream); return Deserialize(decompressedStream);
} }
} }
@@ -106,6 +122,10 @@ namespace Orchard.Redis.OutputCache {
} }
public int GetCacheItemsCount() { public int GetCacheItemsCount() {
if (_connectionMultiplexer == null) {
return 0;
}
return Database.KeyCount(GetLocalizedKey("*")); return Database.KeyCount(GetLocalizedKey("*"));
} }
@@ -123,6 +143,10 @@ namespace Orchard.Redis.OutputCache {
/// </summary> /// </summary>
/// <returns>The keys for the current tenant.</returns> /// <returns>The keys for the current tenant.</returns>
private IEnumerable<string> GetAllKeys() { private IEnumerable<string> GetAllKeys() {
if (_connectionMultiplexer == null) {
return new string[0];
}
// prevent the same request from computing the list twice (count + list) // prevent the same request from computing the list twice (count + list)
if (_keysCache == null) { if (_keysCache == null) {
_keysCache = new HashSet<string>(); _keysCache = new HashSet<string>();

View File

@@ -28,17 +28,29 @@ namespace Orchard.Redis.OutputCache {
} }
public void Tag(string tag, params string[] keys) { public void Tag(string tag, params string[] keys) {
Database.SetAdd(GetLocalizedKey(tag), Array.ConvertAll(keys, x=> (RedisValue) x)); if (_connectionMultiplexer == null) {
return;
}
Database.SetAdd(GetLocalizedKey(tag), Array.ConvertAll(keys, x => (RedisValue)x));
} }
public IEnumerable<string> GetTaggedItems(string tag) { public IEnumerable<string> GetTaggedItems(string tag) {
if (_connectionMultiplexer == null) {
return new string[0];
}
var values = Database.SetMembers(GetLocalizedKey(tag)); var values = Database.SetMembers(GetLocalizedKey(tag));
if (values == null || values.Length == 0) if (values == null || values.Length == 0)
return Enumerable.Empty<string>(); return Enumerable.Empty<string>();
return Array.ConvertAll(values, x => (string) x); return Array.ConvertAll(values, x => (string)x);
} }
public void RemoveTag(string tag) { public void RemoveTag(string tag) {
if (_connectionMultiplexer == null) {
return;
}
Database.KeyDelete(GetLocalizedKey(tag)); Database.KeyDelete(GetLocalizedKey(tag));
} }

View File

@@ -0,0 +1,29 @@
using Orchard.Environment.Extensions;
using Orchard.Localization;
using Orchard.UI.Admin.Notification;
using Orchard.UI.Notify;
using System.Collections.Generic;
namespace Orchard.Redis.Configuration {
[OrchardFeature("Orchard.Redis.Caching")]
public class RedisCacheNotificationProvider : INotificationProvider {
private readonly RedisConnectionProvider _redisConnectionProvider;
public RedisCacheNotificationProvider(RedisConnectionProvider redisConnectionProvider) {
_redisConnectionProvider = redisConnectionProvider;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public IEnumerable<NotifyEntry> GetNotifications() {
//verify if there is a connection string set in the web.config
string ConnectionStringKey = "Orchard.Redis.Cache";
if (_redisConnectionProvider.GetConnectionString(ConnectionStringKey) == null) {
yield return new NotifyEntry { Message = T("You need to configure Redis Cache connection string."), Type = NotifyType.Warning };
}
}
}
}

View File

@@ -0,0 +1,29 @@
using Orchard.Environment.Extensions;
using Orchard.Localization;
using Orchard.UI.Admin.Notification;
using Orchard.UI.Notify;
using System.Collections.Generic;
namespace Orchard.Redis.Configuration {
[OrchardFeature("Orchard.Redis.MessageBus")]
public class RedisMessageBusNotificationProvider : INotificationProvider {
private readonly RedisConnectionProvider _redisConnectionProvider;
public RedisMessageBusNotificationProvider(RedisConnectionProvider redisConnectionProvider) {
_redisConnectionProvider = redisConnectionProvider;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public IEnumerable<NotifyEntry> GetNotifications() {
//verify if there is a connection string set in the web.config
string ConnectionStringKey = "Orchard.Redis.MessageBus";
if (_redisConnectionProvider.GetConnectionString(ConnectionStringKey) == null) {
yield return new NotifyEntry { Message = T("You need to configure Redis MessageBus connection string."), Type = NotifyType.Warning };
}
}
}
}

View File

@@ -0,0 +1,29 @@
using Orchard.Environment.Extensions;
using Orchard.Localization;
using Orchard.UI.Admin.Notification;
using Orchard.UI.Notify;
using System.Collections.Generic;
namespace Orchard.Redis.Configuration {
[OrchardFeature("Orchard.Redis.OutputCache")]
public class RedisOutputCacheNotificationProvider : INotificationProvider {
private readonly RedisConnectionProvider _redisConnectionProvider;
public RedisOutputCacheNotificationProvider(RedisConnectionProvider redisConnectionProvider) {
_redisConnectionProvider = redisConnectionProvider;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public IEnumerable<NotifyEntry> GetNotifications() {
//verify if there is a connection string set in the web.config
string ConnectionStringKey = "Orchard.Redis.OutputCache";
if (_redisConnectionProvider.GetConnectionString(ConnectionStringKey) == null) {
yield return new NotifyEntry { Message = T("You need to configure Redis OutputCache connection string."), Type = NotifyType.Warning };
}
}
}
}