如何在Windows上使用Servicestack开始使用Redis?

3
我刚开始使用ServiceStack,并在MVC4中创建了我的第一个服务。现在我想通过Redis来持久化我的对象。我无法弄清楚如何在Windows上运行它,或者ServiceStack分发版本是否已包含此功能。我也考虑使用Redis云实现之一,但首先想在本地运行它。
谢谢。
1个回答

7
您需要像Windows上的Redis一样的东西(关于此的博客文章可以在这里这里找到)。您可以使用在GitHub上存储的repo。有了它之后,您就可以在Visual Studio中构建Redis并运行它。
Service Stack还有一个支持页面这里,其中包括一个将Redis作为Windows服务运行的项目的链接runs Redis as a Windows Service编辑。我还找到了一个项目和博客文章,一个月前我也曾用过(巧合的是,这是由Jason从stackexchange编写的)。 LATE UPDATE 好吧,我刚刚发表评论:

do more than "download" and "execute installer to get robust service" like you do with Nuget packages

接着我就找到了这个Redis Nuget,它允许您从命令行运行Redis,由MSOpenTech发布,您可以使用ServiceStack.Redis package与之配合使用。

编辑 这是使用它的方法:

  • Create a console application in Visual Studio
  • Run "manage NuGet packages" in the project console menu on solution explorer
  • Search for, and install "redis-64" and "ServiceStack.Redis" (you may want to do redis-64 by running install-package redis-64 from the Package Manager Console)
  • Start redis from packages\Redis-64.\tools\redis-server.exe by cmd prompt or double click
    • (If asked about windows firewall, just cancel to keep comms on the local machine)
  • run the following code:

    public class Message {
        public long Id { get; set; }
        public string Payload { get; set; }
    }
    
    static void Main(string[] args) {
        List<string> messages = new List<string> {
            "Hi there",
            "Hello world",
            "Many name is",
            "Uh, my name is"
        };
    
        var client = new RedisClient("localhost");
        var msgClient = client.As<Message>();
    
        for (int i = 0; i < messages.Count; i++) {
            Message newItem = new Message { 
                Id = msgClient.GetNextSequence(), 
                Payload = messages[i] };
            msgClient.Store(newItem);
        }
    
        foreach (var item in msgClient.GetAll()) {
            Console.WriteLine("{0} {1}", item.Id, item.Payload);
            msgClient.DeleteById(item.Id);
        }
    
        Console.WriteLine("(All done, press enter to exit)");
        Console.ReadLine();
    }
    

输出:

1 Hi there 
2 Hello world 
3 Many name is 
4 Uh, my name is 
(All done, press enter to exit)

谢谢。我确实尝试了存储在Github上的仓库,但是只是拉取zip文件它没有编译。我将再次尝试使用git。 - Trevor de Koekkoek
@TrevordeKoekkoek。Visual Studio 给我们带来了太多便利,以至于当我需要做更多的事情时,我也承认有些困惑,比如说不像 Nuget 包那样只需“下载”和“执行安装程序”就能获得强大的服务——你只需要稍微调整一下思路,就会没问题的。 - Andy Brown
@TrevordeKoekkoek。哇,我刚刚发了那条评论,然后想“现在Nuget上有什么东西” - 然后发现redis-64是在2013年5月6日星期一添加的。回答已编辑以供我们共同学习。 - Andy Brown
1
@TrevordeKoekkoek。我已经添加了一个在VS2012中测试过的工作示例。 - Andy Brown

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接