.net - Redis:我如何 INCR 和插入?

标签 .net redis booksleeve

我打开了 CLI 和一个 .NET 虚拟解决方案。我想创建一个用户并一次性插入一些数据。我是如何在 CLI/REPL 中写这个的

redis 127.0.0.1:6379> incr userCount
(integer) 1
redis 127.0.0.1:6379> set user:1:name acid
OK
redis 127.0.0.1:6379> set user:1:pw zombie
OK

这一切都很好,但我如何一次做到这一点?如果我能在操作结束时获得用户 ID 就好了,但对于这个例子我真的不需要它,我想一​​次流水线化这 3 个命令。我怎么做?也许我应该在交易中这样做?另外,我可能应该在插入之前检查该名称是否存在,但我还没有做到这一点。

using BookSleeve;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RedisEx
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var conn = new RedisConnection("localhost"))
            {
                conn.Open();
                var userC = conn.Strings.Increment(1, "userCount");
                var userid = conn.Wait(userC);
                if (userid > 1)
                {
                    var sz = conn.Wait(conn.Strings.GetString(1, string.Format("user:{0}:name", userid - 1)));
                    Console.WriteLine(sz);
                }
                conn.Strings.Set(1, string.Format("user:{0}:name", userid), "acid");
                conn.Strings.Set(1, string.Format("user:{0}:pw", userid), "zombie");
            }
        }
    }
}

最佳答案

请看这个 C# 例子:

它说明了将“shippers”记录添加到 Redis“列表”中。只需将“托运人”替换为“用户”,并去掉“增量”。您的“计数”简单地变成了列表中的 #/items :)

例子中使用的是ServiceStack而不是Booksleeve,但是原理应该是完全一样的。

'希望有帮助.. PSM

关于.net - Redis:我如何 INCR 和插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12206914/

相关文章:

.net - LinQ 到对象 GroupBy() 按对象和 Sum() 按数量

redis - 如何扩展 Redis?

redis - 如何从 BookSleeve 调用 BGSAVE?

javascript - CSV 文件作为单个字符串返回

c# - 有没有更好的方法来为队列实现 Remove 方法?

java - 如何在线程安全模式下以特定编号在Redis中启动计数器?

node.js - 带有单独处理器脚本的 Google App Engine

c# - 使用 BookSleeve 的 ConnectionUtils.Connect() 将 SignalR 与 Redis 消息总线故障转移结合使用

c# - 在 Windows Process Activation Services (WAS) 和 IIS 7 之间进行选择