Last active
June 14, 2017 07:50
-
-
Save eransharv/0d8c3ade1cabab864191c69db8563caa to your computer and use it in GitHub Desktop.
simple code example on how to connect to redis using StackExchange.redis
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 | |
{ | |
static void Main(string[] args) | |
{ | |
// Connection setup | |
var configurationOptions = new ConfigurationOptions | |
{ | |
EndPoints = { "redis-15482.c14.us-east-1-3.ec2.cloud.redislabs.com:15482" }, | |
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 | |
}; | |
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(configurationOptions); | |
// Standard connection | |
IDatabase db = redis.GetDatabase(); | |
/* | |
//example - assign string | |
if (db.StringSet("foo", "bar")) | |
{ | |
RedisValue val = db.StringGet("foo"); | |
Debug.WriteLine(val); | |
} | |
*/ | |
// get the target server | |
var server = redis.GetServer(configurationOptions.EndPoints[0]); | |
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