Object mapping, and more, for Redis and .NET
Redis OM .NET makes it easy to model Redis data in your .NET Applications.
Redis OM .NET | Redis OM Node.js | Redis OM Spring | Redis OM Python
Table of contents
Redis OM provides high-level abstractions for using Redis in .NET, making it easy to model and query your Redis domain objects.
This preview release contains the following features:
- Declarative object mapping for Redis objects
- Declarative secondary-index generation
- Fluent APIs for querying Redis
- Fluent APIs for performing Redis aggregations
Using the dotnet cli, run:
dotnet add package Redis.OM
Before writing any code you'll need a Redis instance with the appropriate Redis modules! The quickest way to get this is with Docker:
docker run -p 6379:6379 redislabs/redismod:preview
With Redis OM, you can model your data and declare indexes with minimal code. For example, here's how we might model a customer object:
[Document]
public class Customer
{
[Indexed(Sortable = true)] public string FirstName { get; set; }
[Indexed(Aggregatable = true)] public string LastName { get; set; }
public string Email { get; set; }
[Indexed(Sortable = true)] public int Age { get; set; }
}
Notice that we've applied the Document
attribute to this class. We've also specified that certain fields should be Indexed
.
Now we need to create the Redis index. So we'll connect to Redis and then call CreateIndex
on an IRedisConnection
:
var provider = new RedisConnectionProvider("redis://localhost:6379");
provider.Connection.CreateIndex(typeof(Customer));
Once the index is created, we can:
- Insert Customer objects into Redis
- Get a Customer object by ID from Redis
- Query Customers from Redis
- Run aggregations on Customers in Redis
Let's see how!
We can query our domain using expressions in LINQ, like so:
var customers = provider.RedisCollection<Customer>();
// Insert customer
customers.Insert(new Customer()
{
FirstName = "James",
LastName = "Bond",
Age = 68,
Email = "[email protected]"
});
// Find all customers whose last name is "Bond"
customers.Where(x => x.LastName == "Bond");
// Find all customers whose last name is Bond OR whose age is greater than 65
customers.Where(x => x.LastName == "Bond" || x.Age > 65);
// Find all customers whose last name is Bond AND whose first name is James
customers.Where(x => x.LastName == "Bond" && x.FirstName == "James");
We can also run aggregations on the customer object, again using expressions in LINQ:
// Get our average customer age
customerAggregations.Average(x => x.RecordShell.Age);
// Format customer full names
customerAggregations.Apply(x => string.Format("{0} {1}", x.RecordShell.FirstName, x.RecordShell.LastName),
"FullName");
// Get each customer's distance from the Mall of America
customerAggregations.Apply(x => ApplyFunctions.GeoDistance(x.RecordShell.Home, -93.241786, 44.853816),
"DistanceToMall");
This README just scratches the surface. You can find a full tutorial on the Redis Developer Site. All the summary docs for this library can be found on the repo's github page.
If you run into trouble or have any questions, we're here to help!
First, check the FAQ. If you don't find the answer there, hit us up on the Redis Discord Server.
Redis OM can be used with regular Redis for Object mapping and getting objects by their IDs. For more advanced features like indexing, querying, and aggregation, Redis OM is dependeant on the Source Available RedisJSON module.
Without RedisJSON, you can still use Redis OM to create declarative models backed by Redis.
We'll store your model data in Redis as Hashes, and you can retrieve models using their primary keys.
So, what won't work without these modules?
- You won't be able to nest models inside each other.
- You won't be able to use our expressive queries to find object -- you'll only be able to query by primary key.
You can use RedisJSON with your self-hosted Redis deployment. Just follow the instructions on installing the binary version of the module in its Quick Start Guides.
NOTE: The quick start guide has instructions on how to run the module in Redis with Docker.
Don't want to run Redis yourself? RedisJSON is also available on Redis Cloud. Get started here.
We'd love your contributions! If you want to contribute please read our Contributing document.
- @slorello89
- @banker
- @simonprickett
- @BenShapira
- @satish860