这是 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["urn:names:current"]
时,您实际上是在存储对 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();
}
注意:.As
是 .GetTypedClient
的较短别名从 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屋!