servicestack 收费.redis 什么版本收费

&&文章详情
C#redis使用之ServiceStack
需要注意的是:ServiceStack.Redis&中GetClient()方法,只能拿到Master redis中获取连接,而拿不到slave 的readonly连接。这样 slave起到了冗余备份的作用,读的功能没有发挥出来,如果并发请求太多的话,则Redis的性能会有影响。
    所以,我们需要的写入和读取的时候做一个区分,写入的时候,调用client.GetClient() 来获取writeHosts的Master的redis 链接。读取,则调用client.GetReadOnlyClient()来获取的readonlyHost的 Slave的redis链接。
    或者可以直接使用client.GetCacheClient() 来获取一个连接,他会在写的时候调用GetClient获取连接,读的时候调用GetReadOnlyClient获取连接,这样可以做到读写分离,从而利用redis的主从复制功能。
    1. 配置文件 app.config
  &!-- redis Start&& --&&&& &add key="SessionExpireMinutes" value="180" /&&&& &add key="redis_server_master_session" value="127.0.0.1:6379" /&&&& &add key="redis_server_slave_session" value="127.0.0.1:6380" /&&&& &add key="redis_max_read_pool" value="300" /&&&& &add key="redis_max_write_pool" value="100" /&&&& &!--redis end--&
2. Redis操作的公用类RedisCacheHelper
using Susing System.Collections.Gusing System.Cusing System.Lusing System.Tusing System.Wusing mon.Eusing ServiceStack.Rusing ServiceStack.Lnamespace mon {&&& public class RedisCacheHelper&&& {&&&&&&& private static readonly PooledRedisClientManager pool = null;&&&&&&& private static readonly string[] writeHosts = null;&&&&&&& private static readonly string[] readHosts = null;&&&&&&& public static int RedisMaxReadPool = int.Parse(ConfigurationManager.AppSettings["redis_max_read_pool"]);&&&&&&& public static int RedisMaxWritePool = int.Parse(ConfigurationManager.AppSettings["redis_max_write_pool"]);&&&&&&& static RedisCacheHelper()&&&&&&& {&&&&&&&&&&& var redisMasterHost = ConfigurationManager.AppSettings["redis_server_master_session"];&&&&&&&&&&& var redisSlaveHost = ConfigurationManager.AppSettings["redis_server_slave_session"];&&&&&&&&&&& if (!string.IsNullOrEmpty(redisMasterHost))&&&&&&&&&&& {&&&&&&&&&&&&&&& writeHosts = redisMasterHost.Split(',');&&&&&&&&&&&&&&& readHosts = redisSlaveHost.Split(',');&&&&&&&&&&&&&&& if (readHosts.Length & 0)&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& pool = new PooledRedisClientManager(writeHosts, readHosts,&&&&&&&&&&&&&&&&&&&&&&& new RedisClientManagerConfig()&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&& MaxWritePoolSize = RedisMaxWritePool,&&&&&&&&&&&&&&&&&&&&&&&&&&& MaxReadPoolSize = RedisMaxReadPool,&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&& AutoStart = true&&&&&&&&&&&&&&&&&&&&&&& });&&&&&&&&&&&&&&& }&&&&&&&&&&& }&&&&&&& }&&&&&&& public static void Add&T&(string key, T value, DateTime expiry)&&&&&&& {&&&&&&&&&&& if (value == null)&&&&&&&&&&& {&&&&&&&&&&&&&&& return;&&&&&&&&&&& }&&&&&&&&&&& if (expiry &= DateTime.Now)&&&&&&&&&&& {&&&&&&&&&&&&&&& Remove(key);&&&&&&&&&&&&&&& return;&&&&&&&&&&& }&&&&&&&&&&& try&&&&&&&&&&& {&&&&&&&&&&&&&&& if (pool != null)&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& using (var r = pool.GetClient())&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& if (r != null)&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&& r.SendTimeout = 1000;&&&&&&&&&&&&&&&&&&&&&&&&&&& r.Set(key, value, expiry - DateTime.Now);&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&& }&&&&&&&&&&& }&&&&&&&&&&& catch (Exception ex)&&&&&&&&&&& {&&&&&&&&&&&&&&& string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "存储", key);&&&&&&&&&&& }&&&&&&& }&&&&&&& public static void Add&T&(string key, T value, TimeSpan slidingExpiration)&&&&&&& {&&&&&&&&&&& if (value == null)&&&&&&&&&&& {&&&&&&&&&&&&&&& return;&&&&&&&&&&& }&&&&&&&&&&& if (slidingExpiration.TotalSeconds &= 0)&&&&&&&&&&& {&&&&&&&&&&&&&&& Remove(key);&&&&&&&&&&&&&&& return;&&&&&&&&&&& }&&&&&&&&&&& try&&&&&&&&&&& {&&&&&&&&&&&&&&& if (pool != null)&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& using (var r = pool.GetClient())&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& if (r != null)&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&& r.SendTimeout = 1000;&&&&&&&&&&&&&&&&&&&&&&&&&&& r.Set(key, value, slidingExpiration);&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&& }&&&&&&&&&&& }&&&&&&&&&&& catch (Exception ex)&&&&&&&&&&& {&&&&&&&&&&&&&&& string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "存储", key);&&&&&&&&&&& }&&&&&&& }&&&&&&& public static T Get&T&(string key)&&&&&&& {&&&&&&&&&&& if (string.IsNullOrEmpty(key))&&&&&&&&&&& {&&&&&&&&&&&&&&& return default(T);&&&&&&&&&&& }&&&&&&&&&&& T obj = default(T);&&&&&&&&&&& try&&&&&&&&&&& {&&&&&&&&&&&&&&& if (pool != null)&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& using (var r = pool.GetClient())&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& if (r != null)&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&& r.SendTimeout = 1000;&&&&&&&&&&&&&&&&&&&&&&&&&&& obj = r.Get&T&(key);&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&& }&&&&&&&&&&& }&&&&&&&&&&& catch (Exception ex)&&&&&&&&&&& {&&&&&&&&&&&&&&& string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "获取", key);&&&&&&&&&&& }&&&&&&&&&&& return&&&&&&& }&&&&&&& public static void Remove(string key)&&&&&&& {&&&&&&&&&&& try&&&&&&&&&&& {&&&&&&&&&&&&&&& if (pool != null)&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& using (var r = pool.GetClient())&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& if (r != null)&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&& r.SendTimeout = 1000;&&&&&&&&&&&&&&&&&&&&&&&&&&& r.Remove(key);&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&& }&&&&&&&&&&& }&&&&&&&&&&& catch (Exception ex)&&&&&&&&&&& {&&&&&&&&&&&&&&& string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "删除", key);&&&&&&&&&&& }&&&&&&& }&&&&&&& public static bool Exists(string key)&&&&&&& {&&&&&&&&&&& try&&&&&&&&&&& {&&&&&&&&&&&&&&& if (pool != null)&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& using (var r = pool.GetClient())&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& if (r != null)&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&& r.SendTimeout = 1000;&&&&&&&&&&&&&&&&&&&&&&&&&&& return r.ContainsKey(key);&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&& }&&&&&&&&&&& }&&&&&&&&&&& catch (Exception ex)&&&&&&&&&&& {&&&&&&&&&&&&&&& string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "是否存在", key);&&&&&&&&&&& }&&&&&&&&&&& return false;&&&&&&& }&&&&&&& public static IDictionary&string, T& GetAll&T&(IEnumerable&string& keys) where T : class&&&&&&& {&&&&&&&&&&& if (keys == null)&&&&&&&&&&& {&&&&&&&&&&&&&&& return null;&&&&&&&&&&& }&&&&&&&&&&& keys = keys.Where(k =& !string.IsNullOrWhiteSpace(k));&&&&&&&&&&& if (keys.Count() == 1)&&&&&&&&&&& {&&&&&&&&&&&&&&& T obj = Get&T&(keys.Single());&&&&&&&&&&&&&&& if (obj != null)&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& return new Dictionary&string, T&() { { keys.Single(), obj } };&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&& return null;&&&&&&&&&&& }&&&&&&&&&&& if (!keys.Any())&&&&&&&&&&& {&&&&&&&&&&&&&&& return null;&&&&&&&&&&& }&&&&&&&&&&& IDictionary&string, T& dict = null;&&&&&&&&&&& if (pool != null)&&&&&&&&&&& {&&&&&&&&&&&&&&& keys.Select(s =& new&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& Index = Math.Abs(s.GetHashCode()) % readHosts.Length,&&&&&&&&&&&&&&&&&&& KeyName = s&&&&&&&&&&&&&&& })&&&&&&&&&&&&&&& .GroupBy(p =& p.Index)&&&&&&&&&&&&&&& .Select(g =&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& try&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& using (var r = pool.GetClient(g.Key))&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&& if (r != null)&&&&&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& r.SendTimeout = 1000;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& return r.GetAll&T&(g.Select(p =& p.KeyName));&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& catch (Exception ex)&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "获取", keys.Aggregate((a, b) =& a + "," + b));&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& return null;&&&&&&&&&&&&&&& })&&&&&&&&&&&&&&& .Where(x =& x != null)&&&&&&&&&&&&&&& .ForEach(d =&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& d.ForEach(x =&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& if (dict == null || !dict.Keys.Contains(x.Key))&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&& if (dict == null)&&&&&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& dict = new Dictionary&string, T&();&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&&&&&&&&&& dict.Add(x);&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& });&&&&&&&&&&&&&&& });&&&&&&&&&&& }&&&&&&&&&&& IEnumerable&Tuple&string, T&& result = null;&&&&&&&&&&& if (dict != null)&&&&&&&&&&& {&&&&&&&&&&&&&&& result = dict.Select(d =& new Tuple&string, T&(d.Key, d.Value));&&&&&&&&&&& }&&&&&&&&&&& else&&&&&&&&&&& {&&&&&&&&&&&&&&& result = keys.Select(key =& new Tuple&string, T&(key, Get&T&(key)));&&&&&&&&&&& }&&&&&&&&&&& return result&&&&&&&&&&&&&&& .Select(d =& new Tuple&string[], T&(d.Item1.Split('_'), d.Item2))&&&&&&&&&&&&&&& .Where(d =& d.Item1.Length &= 2)&&&&&&&&&&&&&&& .ToDictionary(x =& x.Item1[1], x =& x.Item2);&&&&&&& }&&& }}<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&Redis在ASP.NET中怎么使用?怎么在VS中操作ServiceStackRedis?_redis吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:690贴子:
Redis在ASP.NET中怎么使用?怎么在VS中操作ServiceStackRedis?收藏
各方面都是初学者,最近在学习Redis,用了ServiceStack.Redis类库,我建好一个SQL数据库,然后将获取到的数据库中的表数据作为List通过IRedisClient.Set存入Redis,然后通过Get取出,现在需要在VS中通过Redis给取出的表的某字段排序,按照ID和者首字母,还有如何通过Redis取list中对象的某字段相同的交集,以上如何通过VS实现?
福利不只是穿多穿少,还要有迷人的微笑!
redis.set&List&T&&(&key&,List&T&);
redis.GET&List&T&&(&key&,List&T&).where(拉姆达表达式);
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或ServiceStack.Redis是Redis官网推荐的C#客户端(),使用的人也很多。最近项目中也用到了,网上查了一下使用这个客户端的方法大概有三种:每次访问新建一个连接,使用连接池和使用长连接()。我一开始使用很简单(我用的版本是3.9.32.0)封装了一个RedisHelper类,内部每次访问new一个RedisClient,并每次用完dispose掉。
public class RedisHelper : IDisposable
public const string DefaultHost = "localhost";
public const int DefaultPort = 6379;
#region Field
private IRedisClient _
private PooledRedisClientManager _
#endregion
public RedisHelper()
public RedisHelper(string host, int port)
_redis = new RedisClient(host, port);
public RedisHelper(string host, int port, int db)
_redis = new RedisClient(host, port);
_redis.Db =
      private bool m_disposed =
      public void Dispose()
      {
        Dispose(true);
        GC.SuppressFinalize(this);
      }
      protected virtual void Dispose(bool disposing)
      {
        if (!m_disposed)
        {
          if (disposing)
          {
            _redis.Dispose();
          }
          m_disposed =
        }
      }
    ~RedisHelper()
      Dispose(false);
但是在实际测试过程中500左右的并发就会出现&& 的错误,很奇怪我每次用完RedisClient都dispose掉了,按理说应该是释放掉了连接了。看源码在RedisNativeClient.cs中的下下面几行代码:
protected virtual void Dispose(bool disposing)
if (ClientManager != null)
ClientManager.DisposeClient(this);
if (disposing)
//dispose un managed resources
DisposeConnection();
internal void DisposeConnection()
if (IsDisposed)
IsDisposed =
if (socket == null)
catch (Exception ex)
log.Error("Error when trying to Quit()", ex);
SafeConnectionClose();
&我最终调用的应该是先Quit向Redis发送一个quit命令,然后执行SafeConnectionClose()方法:
private void SafeConnectionClose()
// workaround for a .net bug: /kb/821625
if (Bstream != null)
Bstream.Close();
if (socket != null)
socket.Close();
&这个方法是先关闭Bstream(BufferedStream继承自Stream),这里说为了解决.net的一个bug()TcpClient关闭方法不会关闭基础TCP连接,然后关闭socket。正常来讲不会出现我遇到的问题,这时我怀疑是不是就是因为.net那个bug导致的而ServiceStack.Redis并没有解决这个bug或者某些环境下没有彻底解决,关于那个bug有时间得好好研究下。
不管怎么样既然使用上面的那种方式不行我就考虑换种方式,这次为了简单起见和验证(我怀疑500压力太大,其实一点也不大)我使用了连接池:
private static PooledRedisClientManager CreateManager(RedisConfig config)
//192.168.32.13:6379;db=1
//PooledRedisClientManager prcm = new PooledRedisClientManager(new List&string& { "192.168.71.64:6380" }, new List&string& { "192.168.71.64:6380" },
new RedisClientManagerConfig
MaxWritePoolSize = 150,
MaxReadPoolSize = 150,
AutoStart = true
PooledRedisClientManager prcm = new PooledRedisClientManager(config.Db, config.Host);
prcm.ConnectTimeout = config.ConnectT
prcm.PoolTimeOut = config.PoolTimeO
prcm.SocketSendTimeout = config.SocketSendT
prcm.SocketReceiveTimeout = config.SocketReceiveT
public static PooledRedisClientManager GetPooledRedisClientManager(string connectionString)
lock (_syncObj)
if (!pools.ContainsKey(connectionString))
pools.Add(connectionString, CreateManager(GetRedisConfig(connectionString)));
else if (pools[connectionString] == null)
pools[connectionString] = CreateManager(GetRedisConfig(connectionString));
return pools[connectionString];
&上面的&&没出现但是却出现了另外一个奇怪的问题:&no more data&,我用工具测试发现连续不断的访问redis并不存在问题,但是稍微间隔几秒在访问就会包no more data的错误,跟踪源码:
int c = SafeReadByte();
if (c == -1)
throw CreateResponseError("No more data");
private int SafeReadByte()
return Bstream.ReadByte();
这个错误意思貌似就是从stream里读取不到数据()。但实在想不通为什么。因为要上线,所以没有多深究,接下来尝试了第三种方法长连接:
protected static RedisClient Redis = new RedisClient("10.0.4.227", 6379);
但是报的错更是千奇百怪(确实没有了这个错误)当时太匆忙错误信息没记录下来,貌似记得有&强迫关闭连接&的问题 ,我开始怀疑这玩意了网上也有人抱怨Redis()。但看了看这位兄弟的问题跟我的应该还是不一样,再说redis这么&牛&,怀疑别人之前先怀疑自己,于是我怀疑是不是自己代码有bug但是反复检查了几遍却没有头绪,最后在同事的提示下我把重装了一下Redis(之前是测试人员装的),结果用上面长连接的方式又测了一下但是仍然有问题,貌似是连接太多没有释放的问题(太马虎了没记录啊),难道是loadRunner测试脚本有问题??时间太紧我抱着试试看看的心里又采用了上面的连接池的方法试了一下,奇迹般的竟然正常了。坑爹啊~来来回回头都搞大了结果就不明不白的好了,时间太匆忙也怪我太马虎中间的信息都没做保留而且影响因素太多没好好的控制,有时间一定要在重现一下弄个究竟。
阅读(...) 评论()

我要回帖

更多关于 stackoverflow redis 的文章

 

随机推荐