///
/// 插入字符串
///
///
public string Inserttest()
{
//操作数据库0
var db = connect.GetDatabase(0);
return db.StringSet("test", Request.Params["value"]).ToString();
}
///
/// 插入hash
///
///
public string Inserthtest()
{
//操作数据库0
var db = connect.GetDatabase(0);
return db.HashSet("htest", Guid.NewGuid().ToString(), Request.Params["value"]).ToString();
}
///
/// 插入list
///
///
public string Insertltest()
{
//操作数据库0
var db = connect.GetDatabase(0);
return db.ListLeftPush("ltest", Request.Params["value"]).ToString();
}
///
/// 获取hash
///
///
public string Gethtest()
{
//操作数据库0
var db = connect.GetDatabase(0);
var result = db.HashGetAll("htest");
return string.Join("|", result.Select(item => item.Name + ":" + item.Value).ToList<string>());
}
///
/// 插入list
///
///
public string Insertltest()
{
//操作数据库0
var db = connect.GetDatabase(0);
return db.ListLeftPush("ltest", Request.Params["value"]).ToString();
}
///
/// 获取list
///
///
public string Getltest()
{
//操作数据库0
var db = connect.GetDatabase(0);
System.Text.StringBuilder result = new System.Text.StringBuilder("本次读取将清空List:");
for (var i = db.ListLength("ltest"); i > 0; i--)
{
result.Append(db.ListLeftPop("ltest") + ",");
}
return result.ToString();
}
///
/// 插入set
///
///
public string Insertstest()
{
//操作数据库0
var db = connect.GetDatabase(0);
return db.SetAdd("stest", Request.Params["value"]).ToString();
}
///
/// 获取set
///
///
public string Getstest()
{
//操作数据库0
var db = connect.GetDatabase(0);
var result = db.SetMembers("stest");
return string.Join(",", result);
}
///
/// 插入排序set
///
///
public string Insertsstest()
{
//操作数据库0
var db = connect.GetDatabase(0);
return db.SortedSetAdd("sstest", Request.Params["value"], (new Random()).Next(0, 100)).ToString();
}
///
/// 获取排序set
///
///
public string Getsstest()
{
//操作数据库0
var db = connect.GetDatabase(0);
var result = db.SortedSetScan("sstest");
return string.Join("|", result.Select(item => item.Element + ":" + item.Score).ToList<string>());
}
整体来说本demo只对几个类型做了简单的插入与读取操作,在操作过程中发现了Lists是采用的入队列出队列的方式进行操作了,和C#中的List不同,不能遍历数据而不改变数据集(目前了解的是这样,不知是否有其他方式遍历)。
controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StackExchange.Redis;
namespace RedisSession.Controllers
{
public class IndexController : Controller
{
//
// GET: /Index/
public static ConnectionMultiplexer connect = ConnectionMultiplexer.Connect(("192.168.224.134:6379,abortConnect=false,ssl=false,password=123"));
public ActionResult Index()
{
return View();
}
///
/// 插入字符串
///
///
public string Inserttest()
{
//操作数据库0
var db = connect.GetDatabase(0);
return db.StringSet("test", Request.Params["value"]).ToString();
}
///
/// 插入hash
///
///
public string Inserthtest()
{
//操作数据库0
var db = connect.GetDatabase(0);
return db.HashSet("htest", Guid.NewGuid().ToString(), Request.Params["value"]).ToString();
}
///
/// 插入list
///
///
public string Insertltest()
{
//操作数据库0
var db = connect.GetDatabase(0);
return db.ListLeftPush("ltest", Request.Params["value"]).ToString();
}
///
/// 插入set
///
///
public string Insertstest()
{
//操作数据库0
var db = connect.GetDatabase(0);
return db.SetAdd("stest", Request.Params["value"]).ToString();
}
///
/// 插入排序set
///
///
public string Insertsstest()
{
//操作数据库0
var db = connect.GetDatabase(0);
return db.SortedSetAdd("sstest", Request.Params["value"], (new Random()).Next(0, 100)).ToString();
}
///
/// 获取字符串
///
///
public string Gettest()
{
//操作数据库0
var db = connect.GetDatabase(0);
return db.StringGet("test").ToString();
}
///
/// 获取hash
///
///
public string Gethtest()
{
//操作数据库0
var db = connect.GetDatabase(0);
var result = db.HashGetAll("htest");
return string.Join("|", result.Select(item => item.Name + ":" + item.Value).ToList<string>());
}
///
/// 获取list
///
///
public string Getltest()
{
//操作数据库0
var db = connect.GetDatabase(0);
System.Text.StringBuilder result = new System.Text.StringBuilder("本次读取将清空List:");
for (var i = db.ListLength("ltest"); i > 0; i--)
{
result.Append(db.ListLeftPop("ltest") + ",");
}
return result.ToString();
}
///
/// 获取set
///
///
public string Getstest()
{
//操作数据库0
var db = connect.GetDatabase(0);
var result = db.SetMembers("stest");
return string.Join(",", result);
}
///
/// 获取排序set
///
///
public string Getsstest()
{
//操作数据库0
var db = connect.GetDatabase(0);
var result = db.SortedSetScan("sstest");
return string.Join("|", result.Select(item => item.Element + ":" + item.Score).ToList<string>());
}
}
}
html
<style>
table.gridtable {
font-family: verdana,arial,sans-serif;
font-size: 11px;
color: #333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table.gridtable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table.gridtable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
style>
<script src="../../Scripts/jquery-1.7.1.js">script>
<script type="text/javascript">
function insert(method) {
$.post("/Index/Insert" + method, { value: $('#' + method).val() }, function (data) {
alert(data);
}, 'text');
}
function getdata(method) {
$.get("/Index/Get" + method, function (data) {
$('#r' + method).text(data);
}, 'text');
}
script>
<h2>Redis 的几种类型的操作(string,hash,lists,set,sort set)h2>
<table class="gridtable">
<thead>
<tr>
<th>stringth>
<th>hashth>
<th>liststh>
<th>setth>
<th>sort setth>
tr>
thead>
<tr>
<td>Key:testtd>
<td>Key:htesttd>
<td>Key:ltesttd>
<td>Key:stesttd>
<td>Key:sstesttd>
tr>
<tr>
<td>值:<input id="test" style="width: 80px;" /><input type="button" value="入库" onclick="insert('test')" />td>
<td>值:<input id="htest" style="width: 80px;" /><input type="button" value="入库" onclick="insert('htest')" />td>
<td>值:<input id="ltest" style="width: 80px;" /><input type="button" value="入库" onclick="insert('ltest')" />td>
<td>值:<input id="stest" style="width: 80px;" /><input type="button" value="入库" onclick="insert('stest')" />td>
<td>值:<input id="sstest" style="width: 80px;" /><input type="button" value="入库" onclick="insert('sstest')" />td>
tr>
<tr>
<td>结果:<span id="rtest" style="width: 80px;">span><input type="button" value="读取" onclick="getdata('test')" />td>
<td>结果:<span id="rhtest" style="width: 80px;">span><input type="button" value="读取" onclick="getdata('htest')" />td>
<td>结果:<span id="rltest" style="width: 80px;">span><input type="button" value="读取" onclick="getdata('ltest')" />td>
<td>结果:<span id="rstest" style="width: 80px;">span><input type="button" value="读取" onclick="getdata('stest')" />td>
<td>结果:<span id="rsstest" style="width: 80px;">span><input type="button" value="读取" onclick="getdata('sstest')" />td>
tr>
table>