开发者

Start using Redis with ASP.NET

How do I start using Redis database with ASP开发者_JAVA百科.NET?

What I should install and what I should download?

I'm using Visual Studio 2008 with C#.


FYI, both the:

  • RedisStackOverflow with C# Source Code and
  • RedisAdminUI with C# Source Code

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*.


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


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.


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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜