22

How do I start using Redis database with ASP.NET?

What I should install and what I should download?

I'm using Visual Studio 2008 with C#.

4 Answers 4

30

FYI, both the:

are open source ASP.NET web applications that only use the ServiceStack.Redis C# client.

Here is an example of how you would use an Inversion of control (IoC) container to register a Redis client connection pool and its accompanying IRepository with an IoC:

//Register any dependencies you want injected into your services
container.Register<IRedisClientsManager>(c => new PooledRedisClientManager());
container.Register<IRepository>(c => new Repository(c.Resolve<IRedisClientsManager>()));

Note: if you're just starting out with the client, I recommend you go through the C# Client Wiki, Especially the Designing a Simple Blog application with Redis tutorial*.

Sign up to request clarification or add additional context in comments.

3 Comments

Would it be recommended that IRedisClientsManager be registered in singleton scope if the IoC container supports it (i.e. ReusedWithin(ReuseScope.Container) in Funq or InSingletonScope() with Ninject)?
Yes it maintains the connection pool on the instance. To be useful the client connections should be managed from the same pool. Note: Funq is singleton by default, no need to be explicit.
Ah, the bit about Funq being singleton by default is what I missed. This explains why it's not explicit in any example I've seen. (I'm used to Ninject where transient scope is the default.) Thanks for pointing this out.
16

You can access a Redis instance from C# using the servicestack driver. You can download the code from its GitHub repository.

3 Comments

It's worth noting that the ServiceStack driver has a limit of 6,000 requests per day unless you pay $$$.
Where can I read about these limitations and payment options?
5

Recommend You StackExchage.Redis Client Library for ASP.net. Recommended By Microsoft as you see in this MSDN article. it is free and opensource. Also Look at complete List of Redis Clients available: http://redis.io/clients

And for Installing The Redis and using Client in Windows Based Platforms Download And Install Redis Service (Server and Client Tools With Documentaion) Wrote By Microsoft.

Comments

2

Taken from Integrating Redis in your Asp.Net MVC Project:
The first thing to do is to install Redis on your machine. It is created for Linux but has a simple installation for Windows. In fact, Microsoft has a open source implementation where you can download the installation from this GitHub page.

Install StackExchange.Redis from Nuget.
Then you can use it like this:

public class RedisCache : ICache
{
private readonly ConnectionMultiplexer redisConnections;

public RedisCache()
{
    this.redisConnections = ConnectionMultiplexer.Connect("localhost");
}
public void Set<T>(string key, T objectToCache) where T : class
{
    var db = this.redisConnections.GetDatabase();
    db.StringSet(key, JsonConvert.SerializeObject(objectToCache
                , Formatting.Indented
                , new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
                    PreserveReferencesHandling = PreserveReferencesHandling.Objects
                }));
}


public T Get<T>(string key) where T :class
{
    var db = this.redisConnections.GetDatabase();
    var redisObject = db.StringGet(key);
    if (redisObject.HasValue)
    {
        return JsonConvert.DeserializeObject<T>(redisObject
                , new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
                    PreserveReferencesHandling = PreserveReferencesHandling.Objects
                });
    }
    else
    {
        return (T)null;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.