1:下载redis for windows or linux安装并开启服务,并在vs的⼯具菜单下安装nuget(本⽂采⽤windows版本)
2:建⽴ mvc4项⽬(internet app)
4:添加引⽤:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ServiceStack.Redis;
using System.Collections;
using ServiceStack.Common;
4:替换HomeController类中的Index⽅法:
public ActionResult Index()
{
RedisClientManagerConfig RedisConfig = new RedisClientManagerConfig();
RedisConfig.AutoStart = true;
RedisConfig.MaxReadPoolSize = 60;
RedisConfig.MaxWritePoolSize = 60;
PooledRedisClientManager prcm = new PooledRedisClientManager(new List<string>() { "127.0.0.1" }, new List<string>() { "127.0.0.1" }, RedisConfig);
using (IRedisClient RClient = prcm.GetClient())
{
RClient.Add("p2", "Hello world,");
}
using (IRedisClient RClient = prcm.GetClient())
{
RClient.Get<string>("p2");
}
using (IRedisClient RClient = prcm.GetClient())
{
var t = RClient.Sets["Set"];
t.Add("bill");
t.Add("bob");
t.Add("jeremey");
var otherSet = RClient.Sets["OtherSet"];
otherSet.Add("bill");
otherSet.Add("jeb");
otherSet.Add("kermin");
List<string> keys = RClient.GetAllKeys();
IEnumerator myie = t.GetEnumerator();
myie.Ret();
string text = string.Empty;//重置
while (myie.MoveNext())
{
text += myie.Current.ToString() + ", ";
}
var hash = RClient.Hashes["myhash"];
hash.Add("key1", "value1");
hash.Add("key2", "value2");
hash.Add("key3", "value3");
IEnumerator ie = hash.GetEnumerator();
string hashtext = string.Empty;
while (ie.MoveNext())
{
hashtext += ie.Current.ToString() + ", ";
}
}
var sortt=RClient.SortedSets["mysortedt1"];
sortt.Add("hello");
sortt.Add("mello");
sortt.Add("aello");
RClient.Add("mylf1",sortt);
dynamic result = RClient.GetValue("mylf1");
var child = result[0];
Respon.Write(hashtext);
return Content("");
/
* ⼤家对⽐下细节看看呢,上⾯操作的是副本,即便再次增加,内存中的内容也不会更新,⽽注释掉的就不⼀样了.⽽且,明显看书,是排序过的
RClient.Remove("mylf1");
var sortt=RClient.SortedSets["mysortedt1"];
sortt.Add("hello");
sortt.Add("mello");
sortt.Add("aello");
sortt.Add("123");
RClient.Add("mylf1",sortt);
dynamic result = RClient.GetValue("mylf1").GetEnumerator();
string resulttext = string.Empty;
result.Ret();
while (result.MoveNext())
{
resulttext += result.Current.ToString() + ", ";
}
//下⾯是发布订阅模型⽰例和命令⾏的⼏个命令⽰例
using (RedisClient client = new RedisClient("127.0.0.1:6379"))
{
RedisClient client1 = new RedisClient("127.0.0.1:6379");
client1.Subscribe("channel");
client.PublishMessage("channel", "message");
//每⼀个客户端连接后会发送⼀条请求同步的命令,服务器于是给他⼀个快照同步,并继续缓存收到的命令,
//稍后再⼀起把后续的操作打包给客户端
//最终实现完全同步
client.ZAdd("sortedt1", 45, new byte[] {1,2,3,4,5,6,7,4,3 });
var a= client.ZCard("sortedt1");
var b = client.ZRange("sortedt1", 0, 1);
}
Respon.Write(hashtext);/*
}
//测试list的⽅法,随意放在控制器中,空视图即可
public string RedisList()
{
RedisClient client = new RedisClient("127.0.0.1:6379");
byte[] mylist = new byte[] { 1, 2, 3, 43, 21, 54, 32 };
client.LPush("listid", mylist);
byte[][] result = TestList();
return result.ToString();
}
private byte[][] TestList()
{ RedisClient clientt = new RedisClient("127.0.0.1:6379");
return clientt.LRange("listid", 0, 2);
}
//要和官⽅⼿册上的命令⾏⼀⼀对应,可以这样创建链接对象:RedisClient client=new RedisClient("ho
st:port");//此处有五个重载,mget,t,mt,gett,ht,hget,sadd,sinter ,sinterstore等等都可以⽤了,亲测通过
//⼀个更复杂的例⼦:参照它可以实现和数据库的同步,⽽且,更重要的是,⽆需转化,只需组合封装,极为⽅便
public string testComplexDadaTable()
{
using (RedisClient clientt = new RedisClient("127.0.0.1:6379"))
{
DataTable dt = GetDataTable();
var columns = dt.Columns;
var rows = dt.Rows;
List<DateTime> mydate = new List<DateTime>();
for (int i = 0; i < rows.Count; i++)
{
DateTime test = (rows[i].Field<DateTime>(3));
mydate.Add(test);
}
clientt.Set<List<DateTime>>("mydate", mydate);
}
RedisClient rdc = new RedisClient("127.0.0.1:6379");
var valueComplex = rdc.Get<List<DateTime>>("mydate");
return null; //
clientt.LPush("redis:mysql:10086", null);
}
private DataTable GetDataTable() {
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
// Here we add five DataRows.
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now); table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
return table; }
//为什么redis存储本质是流,如下⽰例:
public string testComplexDadaTable()
{
public string testComplexDadaTable()
{
string key = "key";
byte[] bytekey = System.Text.Encoding.Default.GetBytes(key);
using (RedisClient clientt = new RedisClient("127.0.0.1:6379"))
{
DataTable dt = GetDataTable();
var columns = dt.Columns;
var rows = dt.Rows;
List<DateTime> mydate = new List<DateTime>();
List<DataRow> datarow = new List<DataRow>();
Hashtable hashtable = new Hashtable();
for (int i = 0; i < rows.Count; i++)
{ DateTime test = (rows[i].Field<DateTime>(3));
mydate.Add(test);
string testby = test.ToString();
byte[] arrtext = System.Text.Encoding.Default.GetBytes(testby);
clientt.HSet("hashkey", bytekey, arrtext);
if(!hashtable.ContainsKey("key"))
{
hashtable.Add("key", test);
}
} clientt.Get<DataRow>("row");
clientt.Set<List<DateTime>>("mydate", mydate);
}
RedisClient rdc = new RedisClient("127.0.0.1:6379");
var valueComplex = rdc.Get<List<DateTime>>("mydate");
var byteresult = rdc.HGet("hashkey", bytekey);
string constructedString = System.Text.Encoding.Default.GetString(byteresult); return null;
// clientt.LPush("redis:mysql:10086", null);
}
private DataTable GetDataTable()
{
DataTable table = new DataTable();