🖋️
StackExchange Redis Extensions
  • Home
  • Setup
  • Dependency Injection
  • Configuration
    • C# Configuration
    • Json Configuration
    • XML Configuration
  • Serializers
    • Newtonsoft Json.Net
    • MsgPack
    • System.Text.Json
  • Packages
  • Usage
    • Add, retrieve and remove complex object
    • Replace an object
    • Work with multiple items
    • Custom serializer
  • ASP.NET Core
    • Expose redis information
  • Helpful link
  • License
  • Work with the code
    • Unit tests
  • Usages
Powered by GitBook
On this page

Was this helpful?

  1. Usage

Add, retrieve and remove complex object

Create your instance:

var user = new User()
{
	Username = "imperugo",
	Firstname = "Ugo",
	Lastname = "Lattanzi",
	Twitter = "@imperugo"
	Blog = "http://tostring.it",
	Company = new Company 
	{
		Name = "My Super Company",
		Vat = "IT12345678911",
		Address = "somewhere road 12"
	}
}

Add object to Redis

bool added = await cacheClient.Db0.AddAsync("my cache key", user, DateTimeOffset.Now.AddMinutes(10));

Retrieve the object:

var userFromCache = await cacheClient.Db0.GetAsync<User>("my cache key");

Note that is possible to change the expires time of the item also when you are retrieving it. In fact the method GetAsync offers two overloads that accept DateTimeOffset or TimeSpan in order to change the expiration time of the item without submit it again.

Remove and object

bool removed = await cacheClient.Db0.RemoveAsync<User>("my cache key");

PreviousUsageNextReplace an object

Last updated 5 years ago

Was this helpful?