Reputation: 571
I'm somehow beginner in redis and I know it is easy in redis if you want to cache list or object or something like that but I don't know how can I store my web pages in redis? notice that I'm using servicestack as my redis client and for saving data using service stack to my redis I'm using such code:
IRedisClient redisClient = new RedisClient();
var rc = redisClient.As<person>();
rc.Store(new person()
{
Id = 1,
Name = "foo"
});
rc.Store(new person()
{
Id = 2,
Name = "bar"
});
var result = rc.GetAll();
as I told you before I have a big question in my mind that it is
How can I cache my .html or .cshtml pages in .net core web application with using Redis?
Upvotes: 3
Views: 2234
Reputation: 143369
If you're using ServiceStack Razor or ServiceStack Templates to generate your Views you can use the [CachedResponse] attribute to cache the Output responses of your Services, e.g:
[CacheResponse(Duration = 60)]
public class CachedServices : Service
{
public object Any(GetCustomer request) { ... }
public object Any(GetCustomerOrders request) { ... }
}
Annotating your Service will cache the output response of all Services, otherwise you can add them on service implementation methods to cache them adhoc, e.g:
public class CachedServices : Service
{
public object Any(GetCustomer request) { ... }
[CacheResponse(Duration = 60)]
public object Any(GetCustomerOrders request) { ... }
}
Upvotes: 0
Reputation: 239430
The response caching middleware uses whatever distributed cache is configured. Therefore, you need to add the Redis distributed cache provider, and then add the response caching middleware:
services.AddDistributedRedisCache(options =>
{
options.Configuration = "localhost";
options.InstanceName = "SampleInstance";
});
services.AddResponseCaching();
FWIW, you should also change your existing code to utilize an injected instance of IDistributedCache
, rather than working with RedisClient
directly. The end result will be the same (assuming you've configured Redis as your distributed cache provider), but you'll abstract the implementation out of your code.
Upvotes: 3