Redis的使用

1、下载最新版redis
http://redis.googlecode.com/files/redis-2.0.0.tar.gz 


2、下载Windows版客户端
ServiceStack.Redis ★  https://github.com/ServiceStack/ServiceStack.Redis 
Booksleeve ★ http://code.google.com/p/booksleeve/


3、解压redis到C盘,制作自动执行文件和自动隐藏cmd文件
------------- redis-run.vbs --------------  
set ws=wscript.createobject("wscript.shell")
ws.run "redis-run-start.bat /start",0

------------- redis-run-start.bat -------
redis-server.exe redis.conf


4、双击redis-run.vbs启动redis服务
关闭时需要到“任务管理器”中自行删除进程


5、引用ServiceStack的类,并在cs文件中应用其名称空间
using ServiceStack.Common.Extensions;
using ServiceStack.Redis;
using ServiceStack.Redis.Generic;
using ServiceStack.Text;
using ServiceStack.Redis.Support;


6、建立redis管理对象 RedisManage.cs
public  class  Redis
{
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static PooledRedisClientManager prcm = Redis.CreateRedisManager(
   new string[] { "127.0.0.1:6379" },   //读写服务器
   new string[] { "127.0.0.1:6379" }    //只读服务器
);


///


/// 创建Redis连接池管理对象
///

public static PooledRedisClientManager CreateRedisManager(string[] readWriteHosts, string[] readOnlyHosts)
{
   //支持读写分离,均衡负载
   return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
   {
       MaxWritePoolSize = 5, //“写”链接池数
       MaxReadPoolSize = 5, //“读”链接池数
       AutoStart = true,
   });
}

///
/// 添加数据
///

public static bool Set(string key, T val)
{
using (IRedisClient rds = prcm.GetClient())
{
return rds.Set(key, val);
}
}


///
/// 读取数据
///

public static T Get(string key)
{
using (IRedisClient rds = prcm.GetReadOnlyClient())
{
return rds.Get(key);
}
}

///
/// 删除数据
///

public static bool Remove(string key)
{
using (IRedisClient rds = prcm.GetClient())
{
return rds.Remove(key); 
}
}

}







你可能感兴趣的:(C#,/,ASP.Net)