This small tool will give you the power to query redis key value data with Linq.
This project is inspired by UniSpyServer.
The example is shown below.
- You need to create your class inherent from RedisKeyValueObject
- Define some properties with [RedisKey] attribute
- Create your own class which inherent from RedisClient<>, remember to specify the database
- Use (1) GetValue method (2) index access (3) Linq to query from redis
- The linq query buildin only support ==,&&,|| operation, you can not query more complex expression
- The properties with [RedisKey] attribute are the key used to search on redis, the raw redis query string is build to query from redis
- Use LinqToRedis to query simple condition, get the data from redis, then use linq for List<> or Dictionary<> to query more complex data
public record UserInfo : RedisKeyValueObject
{
[RedisKey]
public Guid? ServerID { get; set; }
[RedisKey]
public int? Cookie { get; set; }
public string UserName { get; set; }
public string RemoteEndPoint { get; set; }
}
internal class RedisClient : UniSpyServer.LinqToRedis.RedisClient<UserInfo>
{
public RedisClient() : base("127.0.0.1:6789", 0)
{
}
}
Then you can use the general way to query from redis.
var key = new UserInfo{ Cookie=1 };
var client = new RedisClient();
var results1 = client.GetValue(key);
var results2 = client[key];
var results3 = client.Where(x => x.Cookie == 1).ToList();