Last active
July 9, 2020 09:13
-
-
Save eransharv/d528c974b22cc036884e72d51ed42beb to your computer and use it in GitHub Desktop.
how to connect to redis using stackexchange.redis with singelton object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using StackExchange.Redis; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace HelloRedis | |
{ | |
//For better modulation of Redis it is recommended to store ConnectionMultiplexer as a static singleton in your application. | |
public class RedisStore | |
{ | |
private static readonly Lazy<ConnectionMultiplexer> LazyConnection; | |
static RedisStore() | |
{ | |
// Connection setup | |
var configurationOptions = new ConfigurationOptions | |
{ | |
EndPoints = { "pub-redis-12261.us-east-1.1.azure.garantiadata.com:12261" }, | |
KeepAlive = 10, | |
AbortOnConnectFail = false, | |
ConfigurationChannel = "", | |
TieBreaker = "", | |
ConfigCheckSeconds = 0, | |
CommandMap = CommandMap.Create(new HashSet<string> | |
{ // EXCLUDE a few commands | |
"SUBSCRIBE", "UNSUBSCRIBE", "CLUSTER" | |
}, available: false), | |
Password = "1234", //will ingnore in case of no password is set | |
AllowAdmin = true | |
}; | |
LazyConnection = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(configurationOptions)); | |
} | |
public static ConnectionMultiplexer Connection => LazyConnection.Value; | |
public static IDatabase GetDB => Connection.GetDatabase(); | |
public static IServer GetServer => Connection.GetServer("pub-redis-12261.us-east-1.1.azure.garantiadata.com:12261"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using StackExchange.Redis; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Security.Cryptography.X509Certificates; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace HelloRedis | |
{ | |
//For better modulation of Redis it is recommended to store ConnectionMultiplexer as a static singleton in your application. | |
public class RedisStoreSSL | |
{ | |
private static readonly Lazy<ConnectionMultiplexer> LazyConnection; | |
static RedisStoreSSL() | |
{ | |
// Connection setup | |
var configurationOptions = new ConfigurationOptions | |
{ | |
EndPoints = { "pub-redis-12261.us-east-1.1.azure.garantiadata.com:12261" }, | |
KeepAlive = 10, | |
AbortOnConnectFail = false, | |
ConfigurationChannel = "", | |
TieBreaker = "", | |
ConfigCheckSeconds = 0, | |
CommandMap = CommandMap.Create(new HashSet<string> | |
{ // EXCLUDE a few commands | |
"SUBSCRIBE", "UNSUBSCRIBE", "CLUSTER" | |
}, available: false), | |
Password = "1234", //will ingnore in case of no password is set | |
AllowAdmin = true, | |
Ssl = true | |
}; | |
configurationOptions.CertificateSelection += delegate { | |
return new X509Certificate2("C:/Users/Eran Sharvit/Downloads/garantia_credentials/certificate.pfx", "changeit"); | |
}; | |
LazyConnection = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(configurationOptions)); | |
} | |
public static ConnectionMultiplexer Connection => LazyConnection.Value; | |
public static IDatabase GetDB => Connection.GetDatabase(); | |
public static IServer GetServer => Connection.GetServer("pub-redis-12261.us-east-1.1.azure.garantiadata.com:12261"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using StackExchange.Redis; | |
using System.Diagnostics; | |
namespace HelloRedis | |
{ | |
class stackexchange_example_singelton | |
{ | |
static void Main(string[] args) | |
{ | |
// get the target DB | |
var redis = RedisStoreSSL.GetDB; | |
// get the target server | |
var server = RedisStoreSSL.GetServer; | |
ClientInfo[] clients = server.ClientList(); | |
//display client list | |
foreach (var client in clients) | |
{ | |
Debug.WriteLine(client); | |
} | |
var keys = server.Keys(); | |
foreach(var key in keys) | |
{ | |
Debug.WriteLine(key); | |
} | |
var info = server.Info(); | |
foreach (var stats in info) | |
{ | |
Debug.WriteLine(stats.Key); | |
Debug.WriteLine("------------"); | |
foreach(var param in stats) | |
{ | |
Debug.WriteLine(param); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment