分享

Redis的使用

 修行的嘟嘟 2014-10-20
1、下载最新版redis
http://redis./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); 
}
}

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多