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

View File

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

View File

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

View File

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

View File

@@ -28,17 +28,29 @@ namespace Orchard.Redis.OutputCache {
}
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) {
if (_connectionMultiplexer == null) {
return new string[0];
}
var values = Database.SetMembers(GetLocalizedKey(tag));
if (values == null || values.Length == 0)
return Enumerable.Empty<string>();
return Array.ConvertAll(values, x => (string) x);
return Array.ConvertAll(values, x => (string)x);
}
public void RemoveTag(string tag) {
if (_connectionMultiplexer == null) {
return;
}
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 };
}
}
}
}