打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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); 
}
}

}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Redis操作的封装类 ? 赫洋的Blog
使用ServiceStackRedis链接Redis简介
用Redis实现Session功能,实现单点登录
Redis分布式缓存系列(二)- Redis中的String类型以及使用Redis解决订单秒杀超卖问题
ServiceStack.Redis 之 IRedisTypedClient<第四篇>
Redis快速入门及应用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服