.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. 代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...
随机推荐
- 扩增子统计绘图1箱线图:Alpha多样性
绘制Alpha多样性线箱图 绘图和统计全部为R语言,建议复制代码,在Rstuido中运行,并设置工作目录为存储之前分析结果文件的result目录 # 运行前,请在Rstudio中菜单栏选择“Sessi ...
- Java变量及数据类型
变量及数据类型 变量 变量定义格式:数据类型 变量名 = 初始化值; 基本数据类型 整形数据 package com.ahabest.demo; //输出整形数据的最小值,默认值,最大值,二进制位数 ...
- 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的错误"
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的错误" One of the configured ...
- 洛谷——P2420 让我们异或吧
P2420 让我们异或吧 题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中…xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B ...
- 微信小程序 setData动态修改数据数组的值
1.问题说明 有一组数据,用来存储图片路径,动态修改图片的路径来上传图片,而小程序JS只能通过事件获取时机和setData方法修改数据来改变view. 而用这样写的方式明显是错误的 2.解决办法 字符 ...
- Linux学习笔记记录(三)
压缩: 例如将/etc 目录压缩为压缩包tar -cjvf /aaa.tar.bz2 /etc tar -czvf /aaa.tar.gz /etc 解压: tar -xjv ...
- software collection
software software Table of Contents 1. Privacy 2. GFW 2.1. google search 2.2. 修改 DNS 服务器 2.2.1. 修改ip ...
- atCoder Ants on a Circle(又是蚂蚁问题。。。)
atCoder Ants on a Circle(又是蚂蚁问题...) 传送门 题意:一个圈,蚂蚁在上面以相同的速度和不同的方向走,问t秒后它们各自的位置. 解法:和经典的蚂蚁问题一致,把相撞的情况看 ...
- juruo的刷题&博文祭
Nothing--- 祭我bzoj过66题,博文240篇(.弱.) 自娱(愚)自乐下-
- Python基础(六) 基础文件操作
今天学习python下对文件的基础操作,主要从open函数.File对象的属性.文件定位.简单操作.举例说明几个步骤开始学习,下面开始进入今天的主题: 一.open函数介绍 open函数主要是打开一个 ...