.NET平台下Redis使用(三)【ServiceStack.Redis学习】
MVC4项目下对redis进行增删该查
Models文件下实体类:
public class Book
{
public string BookName {get;set;}
public string Author {get;set;}
public string Edition {get;set;}
public string Publisher {get;set;}
public string Summary { get; set; }
public long Id { get; set; }
public int InStock { get; set; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
BookService.cs代码:
public class BookService : Service
{
public IRepository Repository { get; set; }
public object Post(AddBook request)
{
var id = Repository.AddBook(request.ISBN, request.BookName, request.Author, request.Edition, request.Publisher, request.Summary);
return new AddBookResponse { ISBN = id };
}
public object Get(Books request)
{
return new BooksResponse{ books = Repository.GetBooks()};
}
}
public class BooksResponse
{
public IEnumerable<Book> books { get; set; }
}
[Route("/books", "GET")]
public class Books
{
}
[Route("/books", "POST")]
public class AddBook
{
public long ISBN { get; set; }
public string BookName { get; set; }
public string Author { get; set; }
public string Edition { get; set; }
public string Publisher { get; set; }
public string Summary { get; set; }
public int InStock { get; set; }
}
public class AddBookResponse
{
public long ISBN { get; set; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
Repository.cs代码:
public interface IRepository
{
long AddBook(long ISBN, string BookName, string Author, string Edition, string Publisher, string Summary);
IEnumerable<Book> GetBooks();
Book GetBooks(long isbn);
void UpdateStock(Book book);
}
public class Repository : IRepository
{
IRedisClientsManager RedisManager { get; set; }
public Repository(IRedisClientsManager redisManager)
{
RedisManager = redisManager;
}
public IEnumerable<Book> GetBooks()
{
using (var redisClient = RedisManager.GetClient())
{
var redisUsers = redisClient.As<Book>();
return redisUsers.GetAll();
}
}
public Book GetBooks(long isbn)
{
using (var redisClient = RedisManager.GetClient())
{
var redisUsers = redisClient.As<Book>();
return redisUsers.GetById(isbn);
}
}
public long AddBook(long isbn, string bookName, string author, string edition, string publisher, string summary)
{
using (var redisClient = RedisManager.GetClient())
{
var redisUsers = redisClient.As<Book>();
if(redisUsers.GetById(isbn) !=null)
{
var book = GetBooks(isbn);
book.InStock++;
UpdateStock(book);
return isbn;
}
else
{
var book = new Book() { Id = isbn, BookName = bookName, Author = author, Edition = edition, Publisher = publisher, Summary = summary, InStock = 1 };
redisUsers.Store(book);
return isbn;
}
}
}
public void UpdateStock(Book book)
{
using (var redisClient = RedisManager.GetClient())
{
var redisUsers = redisClient.As<Book>();
redisUsers.Store(book);
};
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
HomeController.cs代码:
public class HomeController : Controller
{
//
// GET: /Home/
public ViewResult Index(int? page)
{
using (var redisClient = new RedisClient("127.0.0.1", 6379, "123456", 1))
{
var pageIndex = page ?? 1;
var pageSize = 20;
var redisUsers = redisClient.As<Book>();
var books = redisUsers.GetAll().OrderByDescending(a => a.Id).ToPagedList(pageIndex, pageSize);
ViewBag.pageOfBooks = books;
return View();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
AdminController.cs代码:
public class AdminController : Controller
{
private readonly static string getBookInfoUri = "http://isbndb.com/api/v2/json/KWC08NFB/book/";
//http://isbndb.com/api/v2/json/[your-api-key]/book/9780849303159
// GET: /Admin/
public ActionResult Index()
{
using (var redisClient = new RedisClient("127.0.0.1",6379,"123456",1))
{
var redisUsers = redisClient.As<Book>();
ViewBag.pageOfBooks = redisUsers.GetAll();
return View();
}
}
public ActionResult PersonList()
{
using (var redisClient = new RedisClient("127.0.0.1", 6379, "123456", 1))
{
var redisPerson = redisClient.As<Person>();
ViewBag.pageOfPersons = redisPerson.GetAll();
return View();
}
}
//[HttpPost]
public ActionResult CreateFromId(string isbn)
{
string fullUri = getBookInfoUri + isbn;
HttpWebRequest webRequest = GetWebRequest(fullUri);
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
string jsonResponse = string.Empty;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
jsonResponse = sr.ReadToEnd();
}
JObject o = JObject.Parse(jsonResponse);
Book newBook = new Book();
newBook.Id = long.Parse(isbn);
newBook.BookName = (string)o["data"][0]["title"];
newBook.Author = (string)o["data"][0]["author_data"][0]["name"];
newBook.Edition = (string)o["data"][0]["edition_info"];
newBook.Publisher = (string)o["data"][0]["publisher_text"];
newBook.Summary = (string)o["data"][0]["summary"];
using (var redisClient = new RedisClient("127.0.0.1", 6379, "123456", 1))
{
var redisUsers = redisClient.As<Book>();
//添加单条数据
redisUsers.Store(newBook);
//添加多条数据
//redisUsers.StoreAll(ListBook);
//查询
//Linq支持
ViewBag.pageOfBooks = redisUsers.GetAll();
//return View();
}
return View("Index");
}
public ActionResult CreatePerson()
{
Person p1 = new Person() { Id = 1, Name = "刘备" };
Person p2 = new Person() { Id = 2, Name = "关羽" };
Person p3 = new Person() { Id = 3, Name = "张飞" };
Person p4 = new Person() { Id = 4, Name = "曹操" };
Person p5 = new Person() { Id = 5, Name = "典韦" };
Person p6 = new Person() { Id = 6, Name = "郭嘉" };
List<Person> ListPerson = new List<Person>() { p2, p3, p4, p5, p6 };
using (IRedisClient RClient = new RedisClient("127.0.0.1", 6379, "123456", 1))
{
IRedisTypedClient<Person> IRPerson = RClient.As<Person>();
IRPerson.DeleteAll();
//------------------------------------------添加--------------------------------------------
//添加单条数据
IRPerson.Store(p1);
//添加多条数据
IRPerson.StoreAll(ListPerson);
//------------------------------------------查询--------------------------------------------
//Linq支持
Response.Write(IRPerson.GetAll().Where(m => m.Id == 1).First().Name); //刘备
//注意,用IRedisTypedClient的对象IRPerson的Srore()添加的才能用IRPerson()方法读取
Response.Write(IRPerson.GetAll().First(m => m.Id == 2).Name); //关羽
//------------------------------------------删除--------------------------------------------
/*
IRPerson.Delete(p1); //删除 刘备
Response.Write(IRPerson.GetAll().Count()); //5
IRPerson.DeleteById(2); //删除 关羽
Response.Write(IRPerson.GetAll().Count()); //4
IRPerson.DeleteByIds(new List<int> { 3, 4 }); //删除张飞 曹操
Response.Write(IRPerson.GetAll().Count()); //2
IRPerson.DeleteAll(); //全部删除
Response.Write(IRPerson.GetAll().Count()); //0
*/
}
return Content("");
}
[HttpPost]
public ActionResult Create(Book bookInfo)
{
return RedirectToAction("Index");
}
private static HttpWebRequest GetWebRequest(string formattedUri)
{
Uri serviceUri = new Uri(formattedUri, UriKind.Absolute);
return (HttpWebRequest)System.Net.WebRequest.Create(serviceUri);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
Admin视图文件夹:Index.cshtml内容:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Admin</h2>
<table>
<tr>
<th>
ISBN
</th>
<th>
Book Name
</th>
<th>
Author
</th>
<th>
Edition
</th>
<th>
Number In Stock
</th>
<th></th>
</tr>
@foreach (var item in ViewBag.pageOfBooks)
{
<tr>
<td>
@item.Id
</td>
<td>
@item.BookName
</td>
<td>
@item.Author
</td>
<td>
@item.Edition
</td>
<td>
@item.InStock
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
PersonList.cshtml内容:
@model dynamic
@{
ViewBag.Title = "Person";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Admin</h2>
<table>
<tr>
<th>
ID
</th>
<th>
名字
</th>
<th>
功能
</th>
</tr>
@foreach (var item in ViewBag.pageOfPersons)
{
<tr>
<td>
@item.Id
</td>
<td>
@item.Name
</td>
<td>
@*@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })*@
</td>
</tr>
}
</table>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
Home视图文件夹:Index.cshtml内容:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using PagedList.Mvc;
@using PagedList;
@foreach (var item in ViewBag.pageOfBooks)
{
<div class ="bookSmall">
<h3>@item.BookName</h3>
<p>@item.Author</p>
<p>@item.Edition</p>
<p>@item.InStock</p>
@Html.ActionLink("More Details", "Details", new { Id = item.Id})
</div>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
运行结果如图:
.NET平台下Redis使用(三)【ServiceStack.Redis学习】的更多相关文章
- Redis(三)Redis基本命令操作与API
一Redis 连接 Redis 连接命令主要是用于连接 redis 服务. 实例 以下实例演示了客户端如何通过密码验证连接到 redis 服务,并检测服务是否在运行: redis 127.0.0.1: ...
- 【Redis】三、Redis安装及简单示例
(四)Redis安装及使用 Redis的安装比较简单,仍然和大多数的Apache开源软件一样,只需要下载,解压,配置环境变量即可.具体安装过程参考:菜鸟教程Redis安装. 安装完成后,通过r ...
- Redis系列(三):Redis的持久化机制(RDB、AOF)
本篇博客是Redis系列的第3篇,主要讲解下Redis的2种持久化机制:RDB和AOF. 本系列的前2篇可以点击以下链接查看: Redis系列(一):Redis简介及环境安装. Redis系列(二): ...
- Redis(三)--- Redis的五大数据类型的底层实现
1.简介 Redis的五大数据类型也称五大数据对象:前面介绍过6大数据结构,Redis并没有直接使用这些结构来实现键值对数据库,而是使用这些结构构建了一个对象系统redisObject:这个对象系统包 ...
- Redis(三)Redis附加功能
一.慢查询分析 许多存储系统(例如MySql)提供慢查询日志帮助开发和运维人员定位系统存在的慢操作. 所谓慢查询日志就是系统在命令执行前后计算每条命令的执行时间,当超过预设阈值,就将这条命令的相关信息 ...
- Redis系列三(redis配置文件分析)
在第一篇文章中有提到过redis.conf这个文件,这个文件就是redis-server的具体配置了.要使用好redis,一定要搞清楚redis的配置文件,这样才能最大的发挥redis的性能. # B ...
- Redis分布式锁(ServiceStack.Redis实现)
1.设计思路 由于Redis是单线程模型,命令操作原子性,所以利用这个特性可以很容易的实现分布式锁.A用户端在Resdis写入1个KEY,其他的用户无法写入这个KEY,实现锁的效果.A用户使用完成后释 ...
- redis(三):Redis 命令(python)
import redis from redis import StrictRedis redis=StrictRedis(host='localhost',port=6379,db=0,passwor ...
- Redis探索之路(三):Redis的五种数据类型String和Hash
一:String 存储二进制数据,可以图片,序列化对象 GET,SET SETNX(not exist) setnx age 33 返回 0,1 SETEX设置有效期 SETEX COLOR 2 ...
- ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现
ASP.NET MVC 学习笔记-2.Razor语法 1. 表达式 表达式必须跟在“@”符号之后, 2. 代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...
随机推荐
- 梦想3D控件 2018.7.26更新
下载地址: http://www.mxdraw.com/ndetail_108.html 1. 编写所有接口函数使用的CHM文档 2. 增加交互绘制功能 3. 增加案例弧形窗建模案例 4. 增 ...
- freemarker使用map替换ftl中相关值
ftl文件demo01.ftl <html> <head> <title>Welcome!</title> </head> <body ...
- java.lang.NoSuchFieldError: DEFAULT_INCOMPATIBLE_IMPROVEMENTS
解决方案: 启动类上加@EnableAutoConfiguration(exclude = { FreeMarkerAutoConfiguration.class }) 或者在配置文件添加spring ...
- 如何使用Dilworth定理
相关例题:NOIP 1999导弹拦截 遇到这题不会去网上搜Dilworth定理,太难受了,看不懂证明 但是,我知道怎么使用了,管那么多,会用就完事了 学习自这篇文章 -1.为什么我不想学证明这个定理 ...
- 爬虫之Selenium库
官方文档:https://selenium-python.readthedocs.io/ Selenium:自动化测试工具,支持多种浏览器.爬虫中主要用来解决JavaScript渲染的问题. 一.开始 ...
- 如何用纯 CSS 创作一个按钮文字滑动特效
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. 在线预览 https://codepen.io/zhang-ou/pen/GdpPLE 可交互视频教 ...
- LINUX-JPS工具
JPS工具 jps(Java Virtual Machine Process Status Tool)是JDK 1.5提供的一个显示当前所有java进程pid的命令,简单实用,非常适合在linux/u ...
- 【Codeforces 375B】Maximum Submatrix 2
[链接] 我是链接,点我呀:) [题意] 如果任意行之间可以重新排序. 问你最大的全是1的子矩阵中1的个数 [题解] 设cnt[i][j] 表示(i,j)这个点往右连续的1的个数 我们枚举列j 然后对 ...
- [K/3Cloud] 首页增加一个自定义页签及页面内容
在K3Cloud登录后的门户首页增加一个页签,如增加一个[BBS论坛] 2013-7-30 11:18:51 上传 下载附件 (84.81 KB) 增加页签 可以这么来做: 进入BOS IDE ,找 ...
- 小朋友的数字(codevs 3293)
题目描述 Description 有n个小朋友排成一列.每个小朋友手上都有一个数字,这个数字可正可负.规定每个小朋友的特征值等于排在他前面(包括他本人)的小朋友中连续若干个(最少有一个)小朋友手上的数 ...