示例#1
0
        public static string GetKeyType(string host,
                                        [SqlParameter(DefaultValue = "6379")] int port,
                                        [SqlParameter(DefaultValue = typeof(DBNull))] string password,
                                        [SqlParameter(DefaultValue = typeof(DBNull))] int?dbId,
                                        string key)
        {
            using (var redis = RedisConnection.GetConnection(host, port, password, dbId))
            {
                var redisKeyType = redis.TypeOf(key);
                switch (redisKeyType)
                {
                case Redis.KeyType.None:
                    return(KeyType.NotExisting.ToString());

                case Redis.KeyType.String:
                    return(KeyType.String.ToString());

                case Redis.KeyType.List:
                    if (RedisqlLists.GetListItemAtIndex(host, port, password, dbId, key, 0).Equals(RedisqlRowsets.RowsetMagic, StringComparison.OrdinalIgnoreCase))
                    {
                        return(KeyType.Rowset.ToString());
                    }
                    return(KeyType.List.ToString());

                case Redis.KeyType.Set:
                    return(KeyType.Set.ToString());

                case Redis.KeyType.ZSet:
                    return(KeyType.Set.ToString());

                case Redis.KeyType.Hash:
                    return(KeyType.Hash.ToString());

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
示例#2
0
        public static void StoreQueryResultsData(string host,
                                                 [SqlParameter(DefaultValue = "6379")] int port,
                                                 [SqlParameter(DefaultValue = typeof(DBNull))] string password,
                                                 [SqlParameter(DefaultValue = typeof(DBNull))] int?dbId,
                                                 string key,
                                                 string query,
                                                 [SqlParameter(DefaultValue = typeof(DBNull))] TimeSpan?expiration,
                                                 [SqlParameter(DefaultValue = false)] bool replaceExisting)
        {
            var rowsXmls = ExecuteQueryAndGetResultList(query);

            using (var redis = RedisConnection.GetConnection(host, port, password, dbId))
            {
                if (redis.ContainsKey(key))
                {
                    if (replaceExisting)
                    {
                        redis.Remove(key);
                    }
                    else
                    {
                        throw new Exception("key with the same name already exists, and replace flag not enabled");
                    }
                }
                List <string> valuesToAdd = new List <string>(rowsXmls.Count + 1)
                {
                    RowsetMagic
                };
                valuesToAdd.AddRange(rowsXmls);
                valuesToAdd.ForEach(val => RedisqlLists.AddToList(host, port, password, dbId, key, val, true, null));
                if (expiration != null)
                {
                    redis.Expire(key, (int)expiration.Value.TotalSeconds);
                }
            }
        }