这是ServiceStack的Redis的有效使用? [英] Is this a valid usage of ServiceStack Redis?
问题描述
我是新来的Redis(以托管服务使用它),并希望使用它作为一个示范/沙箱数据存储列表。
I am new to Redis (using it at a hosted service) and want to use it as a demonstration / sandbox data storage for lists.
我用下面的一段代码。它的工作原理 - 我。但它是有效的(并没有完全不好的做法),使用一个小的网站有多个(最多100)并发用户(对于小数据量 - 高达1000列表项)
I use the following piece of code. It works - for me. But is it a valid (and not completely bad practice) usage for a small web site with several (up to 100) concurrent users (for a small amount of data - up to 1000 list items)?
我使用的是静态的连接和一个静态redisclient类型列表是这样的:
I'm using static connection and a static redisclient typed list like this:
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"];
}
}
这样做我有一个非常容易使用的持久性列表的数据,这是我想要的东西,当我建立/证明我的应用程序的基本块。
Doing this I have a very easy to use persistent list of data, which is exactly what I want when I'm building / demonstrating the basic blocks of my application.
foreach (var person in MyApp.Persons) ...
添加一个新的人:
MyApp.Persons.Add(new Person { Id = MyApp.PersonClient.GetNextSequence(), Name = "My Name" });
我担心的是(目前)不是事实,我加载完整的列表到内存在AppStart的,而是我对Redis的主机连接没有遵守好标准的可能性 - 或者说有其他的问题,我不知道的。
My concern is (currently) not the fact that I am loading the complete list into memory at appstart, but rather the possibility that my connection to the redis host is not following good standards - or that there is some other issue that I'm not aware of.
感谢
推荐答案
其实,当你使用 PersonClient.Lists [瓮:名称:目前]
你实际上存储到不是线程安全的一个RedisClient连接的参考。它的确定,如果它在一个GUI或控制台应用程序,但在多线程Web应用程序不理想。在大多数情况下,你想使用一个线程安全的连接工厂即
Actually when you use PersonClient.Lists["urn:names:current"]
you're actually storing a reference to a RedisClient Connection which is not thread safe. It's ok if it's in a GUI or Console app, but not ideal in a multi-threaded web app. In most scenarios you want to be using a thread safe connection factory i.e.
var redisManager = new PooledRedisClientManager("localhost:6379");
其作用非常像一个数据库连接池。所以每当你要访问的RedisClient的工作原理是:
Which acts very much like a database connection pool. So whenever you want to access the RedisClient works like:
using (var redis = redisManager.GetClient())
{
var allItems = redis.As<Person>().Lists["urn:names:current"].GetAll();
}
请注意:。至于< T>
是一个较短的别名 .GetTypedClient< T>
另一个方便的捷径从redisManager执行类型的客户端是:
Note: .As<T>
is a shorter alias for .GetTypedClient<T>
Another convenient short-cut to execute a typed client from a redisManager is:
var allItems = redisManager.ExecAs<Person>(r => r.Lists["urn:names:current"].GetAll());
我通常喜欢绕过 IRedisClientsManager
中我的代码,以便它不持有RedisClient连接,但每当它需要能够访问它。
I usually prefer to pass around IRedisClientsManager
in my code so it doesn't hold a RedisClient connection but can access it whenever it needs to.
这篇关于这是ServiceStack的Redis的有效使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!