Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

c# - Is this a valid usage of ServiceStack 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.

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)?

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) ...

Adding a new person:

MyApp.Persons.Add(new Person { Id = MyApp.PersonClient.GetNextSequence(), Name = "My Name" });

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.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");

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();
}

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());

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...