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; }
}
- ASP.NET上运行Redis简单示例
- .NET跨平台:在CentOS上编译dnx并运行ASP.NET 5示例程序
- .NET跨平台:在CentOS上编译dnx并运行ASP.NET 5示例程序
- .NET跨平台:在CentOS上编译dnx并运行ASP.NET 5示例程序
- .NET跨平台:在CentOS上编译dnx并运行ASP.NET 5示例程序
- .NET跨平台:在CentOS上编译dnx并运行ASP.NET 5示例程序
- .NET跨平台:在CentOS上编译dnx并运行ASP.NET 5示例程序
- .NET跨平台:在CentOS上编译dnx并运行ASP.NET 5示例程序
- .NET跨平台:在CentOS上编译dnx并运行ASP.NET 5示例程序
- .NET跨平台:在CentOS上编译dnx并运行ASP.NET 5示例程序
- ASP.NET使用Redis共享缓存示例
- asp.net AJAX 一个简单示例
- ASP.NET报表简单使用示例
- ASP.NET Spring.Net 入门1 简单入门示例
- 在传统.NET Framework 上运行ASP.NET Core项目
- Redis简单示例
- Asp.net简单ORM示例源码详细讲解三
- Asp.net简单ORM示例源码详细讲解二
- 黑马程序员--HTML小结(上)
- ArcGIS API for Silverlight/开发入门 环境搭建
- 开源软件新闻 2012-07,来源openopen
- 《多线程》笔记 (修正版)
- Android入门:隐式Intent
- ASP.NET上运行Redis简单示例
- android信息-摘要校验信息
- 黑马程序员--HTML小结(下)
- 解决HP笔记本安装XP SP3后出现系统蓝屏0x0000007E错误的方法
- 一种3D引擎的多线程设计方案
- Request.Form("cardno").Item(y) 的count总是为0
- 静态数组实现栈
- 中缀、前缀、后缀表达式之间的转换
- Android中Message,MessageQueue,Looper,Handler详解+实例