Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 这是ServiceStack Redis的有效用法吗?_C#_Redis_<img Src="//i.stack.imgur.com/WM7S8.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">servicestack - Fatal编程技术网 servicestack,C#,Redis,servicestack" /> servicestack,C#,Redis,servicestack" />

C# 这是ServiceStack Redis的有效用法吗?

C# 这是ServiceStack Redis的有效用法吗?,c#,redis,servicestack,C#,Redis,servicestack,我不熟悉Redis(在托管服务中使用它),希望将其用作列表的演示/沙盒数据存储 我使用下面的代码。对我来说很有效。但是,对于一个拥有多个(最多100个)并发用户的小型网站(对于少量数据-最多1000个列表项),这是一种有效的(而不是完全不好的做法)用法吗 我使用的是静态连接和静态客户端类型列表,如下所示: public class MyApp { private static ServiceStack.Redis.RedisClient redisClient; publ

我不熟悉Redis(在托管服务中使用它),希望将其用作列表的演示/沙盒数据存储

我使用下面的代码。对我来说很有效。但是,对于一个拥有多个(最多100个)并发用户的小型网站(对于少量数据-最多1000个列表项),这是一种有效的(而不是完全不好的做法)用法吗

我使用的是静态连接和静态客户端类型列表,如下所示:

public class MyApp
{   
    private static ServiceStack.Redis.RedisClient redisClient;

    public static IList<Person> Persons;
    public static IRedisTypedClient<Person> PersonClient;

    static MyApp()
    {
        redisClient = new RedisClient("nnn.redistogo.com", nnn) { Password = "nnn" };
        PersonClient = redisClient.GetTypedClient<Person>();
        Persons = PersonClient.Lists["urn:names:current"];
    }
}
添加新人员:

MyApp.Persons.Add(new Person { Id = MyApp.PersonClient.GetNextSequence(), Name = "My Name" });
我(目前)担心的不是我在appstart将完整列表加载到内存中的事实,而是我与redis主机的连接可能没有遵循良好的标准,或者存在一些我不知道的其他问题


感谢您在使用PersonClient.List[“urn:names:current”]

时实际存储了对RedisClient连接的引用,但该连接不是线程安全的。如果它在GUI或控制台应用程序中也可以,但在多线程web应用程序中并不理想。在大多数情况下,您希望使用线程安全连接工厂,即

var redisManager = new PooledRedisClientManager("localhost:6379");
它的作用非常类似于数据库连接池。因此,无论何时您想要访问RedisClient,其工作原理如下:

using (var redis = redisManager.GetClient())
{
    var allItems = redis.As<Person>().Lists["urn:names:current"].GetAll();
}
使用(var redis=redisManager.GetClient())
{
var allItems=redis.As().Lists[“urn:names:current”].GetAll();
}
注意:
.As
的较短别名。GetTypedClient
从redisManager执行类型化客户端的另一个便捷方法是:

var allItems = redisManager.ExecAs<Person>(r => r.Lists["urn:names:current"].GetAll());
var allItems=redisManager.ExecAs(r=>r.Lists[“urn:names:current”].GetAll());

我通常更喜欢在我的代码中传递
IRedisClientsManager
,这样它就不会拥有RedisClient连接,而是可以随时访问它。

我看到了一个示例项目。有一件事我不太明白,那就是如何处理从C#List到Redis存储之间的更新。我发现开放连接非常方便,我的应用程序中总是有完整的列表,甚至不需要显式地处理更新。使用您的方法,您每次需要时都会再次获取列表?另一个问题:如何在ClientsManager的帮助下传递密码?@joeriks如果它是GUI应用程序,可以保留并使用单个连接,Redis中的连接非常便宜。使用redisManager,您需要强制转换到(RedisNativeClient)以设置密码并重新连接()。这些Redis应该只能从内部网访问,而需要密码的情况并不常见。您不需要每次都从经理那里获得完整的列表(我只是给您展示了一个示例调用)。您仍然可以使用
redis.As().Lists[“key”][3]=newValue
SetItemInList
更新单个项目,有关完整的api,请参阅:好的,谢谢。是的,密码,我正在尝试在托管服务(redis to go)上使用redis快速启动运行,但我意识到,对于真正的解决方案,自托管是最好的。@mythz我们如何为新的PooledRedisClientsManager()设置密码
var allItems = redisManager.ExecAs<Person>(r => r.Lists["urn:names:current"].GetAll());