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" }    //只读服务器
);


/// <summary>
/// 创建Redis连接池管理对象
/// </summary>

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

/// <summary>
/// 添加数据
/// </summary>

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


/// <summary>
/// 读取数据
/// </summary>

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

/// <summary>
/// 删除数据
/// </summary>

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

}







评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值