Sam
Sam

Reputation: 30330

Performing a search in Redis cached objects

I'm working on an application where we want to make sure scalability and performance.

I have a List that I'm thinking about caching through Redis. These position are typical corporate positions such as "Accounting Clerk", "Network Administrator", "HR Director", etc. Over time, there will be thousands of these positions.

I also want to implement an auto-complete function where when the user types the a few characters of "Director", I want to start making suggestions. By the way, the position does NOT have to start with the word "Director". In other words, "Director" can be in the middle or at the end as in the case of "HR Director".

My positions are stored in a SQL Server database. I feel if I were to handle this auto-complete function against data in SQL Server, I'll run into performance issues.

I feel caching ALL of my positions in Redis and performing my auto-complete against cached data in Redis should give me the best performance. However, I've never run searches against Redis data.

Could someone please give me some pointers about how to perform such text searches against Redis cached data? Please keep in mind, I want to be able to find the word wherever it may be, not just in the beginning of the phrase.

P.S. My app is an ASP.NET MVC app written in C# and hosted on Azure.

Upvotes: 2

Views: 2471

Answers (1)

thepirat000
thepirat000

Reputation: 13114

You can have a Sorted Set as a lexicographically sorted set of strings, just by inserting the same score for all the elements.

See Lexicographical scores on Redis documentation.

Then you can do the search by iterating on elements matching a given glob-style pattern with the ZSCAN command.

For example with StackExchange.Redis:

var cnn = ConnectionMultiplexer.Connect("localhost");
var db = cnn.GetDatabase();
var key = "positions";
db.SortedSetAdd(key, "Accounting Clerk", 0);
db.SortedSetAdd(key, "Network Administrator", 0);
db.SortedSetAdd(key, "HR Director", 0);
var entries = db.SortedSetScan(key, "*Director*");

Also with CachingFramework.Redis is a little bit shorter:

var ctx = new Context("localhost");
var lexSet = ctx.Collections.GetRedisLexicographicSet("positions");
lexSet.AddRange(new [] { "Accounting Clerk", "Network Administrator", "HR Director" });
var entries = lexSet.Match("*Director*");

Upvotes: 1

Related Questions