ASP.NET上运行Redis简单示例

来源:互联网 发布:怎么卸载mac上的office 编辑:程序博客网 时间:2024/05/22 17:30

方法一:单服务器 

代码: 

RedisClient redisClient = new RedisClient("127.0.0.1"); 

using (var redisUsers = redisClient.GetTypedClient<User>()) 

      using (var redisBlogs = redisClient.GetTypedClient<Blog>()) 

      {         

        //Create the user, getting a unique User Id from the User sequence. 

        /*var mythz = new User { Id = redisUsers.GetNextSequence(), Name = "Demis Bellot" }; 

        //create some blogs using unique Ids from the Blog sequence. Also adding references 

        for (int i = 0; i < 1000; i++) 

        { 

          var mythzBlogs = new List<Blog>{

new Blog 

Id = redisBlogs.GetNextSequence(), 

UserId = mythz.Id, 

UserName = mythz.Name,

Tags = new List<string> { "百度Architecture", ".NET", "Redis" }, 

}, 

new Blog 

Id = redisBlogs.GetNextSequence(), 

UserId = mythz.Id, 

UserName = mythz.Name, 

Tags = new List<string> { "Music", "Twitter", "Life" }, 

}, 

}; 

          //Add the blog references 

          mythzBlogs.ForEach(x => mythz.BlogIds.Add(x.Id)); 

          //Store the user and their blogs 

          redisUsers.Store(mythz); 

          redisBlogs.StoreAll(mythzBlogs); 

        } 

        */ 

        //retrieve all blogs 

        var blogs = redisBlogs.GetAll();  

        //Recursively print the values of the POCO (For T.Dump() Extension method see: http://www.servicestack.net/mythz_blog/?p=202)

        //Response.Write(blogs.OrderBy(p=>p.Id).Dump());

        //Response.Write(redisBlogs.Db.ToJson());

       // if(redisBlogs.GetById(1) != null)

       // redisBlogs.Delete(redisBlogs.GetById(1));

        //redisBlogs.Store(new Blog

        //   {

        //     Id = redisBlogs.GetNextSequence(),

        //     UserId = redisUsers.GetById(1).Id,

        //     UserName = redisUsers.GetById(1).Name,

        //     Tags = new List<string> { "百度Architecture", ".NET", "Redis" },

        //   });

       //redisUsers.Save();

        //redisBlogs.Save();

        //Response.Write(blogs.ToList());

        foreach (Blog model in blogs.OrderBy(p=>p.Id).ToList())

        {

          Response.Write(model.Id + "" + model.Tags.ToJson());

          Response.Write("<br/>========================================================<br/>");

        }

        DateTime end = DateTime.Now;

        Response.Write(end.ToString() + "<br/>");

        TimeSpan ts = end - start;

        Response.Write(ts.TotalSeconds);

      }

 

方法二:使用连接池:

参考Discuz!NT的连接池管理代码,好像还没有开源的,只是参考了他的连接池

    #region 连接池

    private static PooledRedisClientManager prcm;

    /// <summary>

    /// 创建链接池管理对象

    /// </summary>

    private static void CreateManager()

    {

      string[] writeServerList = { "127.0.0.1" };

      string[] readServerList = { "127.0.0.1" };

      prcm = new PooledRedisClientManager(readServerList, writeServerList,

                       new RedisClientManagerConfig

                       {

                         MaxWritePoolSize = 60,

                         MaxReadPoolSize = 60,

                         AutoStart = true,

                       });

    }

 

    /// <summary>

    /// 客户端缓存操作对象

    /// </summary>

    public static IRedisClient GetClient()

    {

      if (prcm == null)

        CreateManager();

      return prcm.GetClient();

    }

    #endregion

 

调用 方法:

      using (var Redis = GetClient().GetTypedClient<Blog>())

      {

        Redis.Store(new Blog

           {

             Id = Redis.GetNextSequence(),

             UserId = 1,

             UserName = "sdfsdf",

             Tags = new List<string> { "黄建杰", ".NET", "Redis" },

           });

        var blogs = Redis.GetAll();

        Response.Write(blogs.ToList().ToJson());

        //Response.Write(Redis.Dump());

      }

使用的实体类:

    public class User

    {

      public User()

      {

        this.BlogIds = new List<long>();

      } 

      public long Id { get; set; }

      public string Name { get; set; }

      public List<long> BlogIds { get; set; }

    } 

    public class Blog

    {

      public Blog()

      {

        this.Tags = new List<string>();

        this.BlogPostIds = new List<long>();

      } 

      public long Id { get; set; }

      public long UserId { get; set; }

      public string UserName { get; set; }

      public List<string> Tags { get; set; }

      public List<long> BlogPostIds { get; set; }

    }

    public class BlogPost

    {

      public BlogPost()

      {

        this.Categories = new List<string>();

        this.Tags = new List<string>();

        this.Comments = new List<BlogPostComment>();

      } 

      public long Id { get; set; }

      public long BlogId { get; set; }

      public string Title { get; set; }

      public string Content { get; set; }

      public List<string> Categories { get; set; }

      public List<string> Tags { get; set; }

      public List<BlogPostComment> Comments { get; set; }

    } 

 

    public class BlogPostComment

    {

      public string Content { get; set; }

 

      public DateTime CreatedDate { get; set; }

    }