Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for 10-Day .Net Aspire Challenge: Day 6 — Redis Cache
Sukhpinder Singh
Sukhpinder Singh

Posted on • Originally published atMedium

     

10-Day .Net Aspire Challenge: Day 6 — Redis Cache

Step-by-step guide on how to use the .Net Aspire Redis Cache component in Visual Studio.

Introduction

.Net Aspire framework is used to develop cloud and production-ready distributed applications. It consists of components to handle cloud-native concerns such as Redis, Postgres etc.

Prerequisites

Objectives

Learn how to create a starter project using .Net Aspire with the Redis Cache.

Github Sample: The solution structure is divided into the following projects

  • DotnetAspireChallenge.ApiService

  • DotnetAspireChallenge.AppHost

  • DotnetAspireChallenge.ServiceDefaults

  • DotnetAspireChallenge.Web

Getting Started

Step 1: Install the following NuGet package

Install the following Nuget package into the subsequent project “DotnetAspireChallenge.AppHost

dotnet add package Aspire.Hosting.Oracle
Enter fullscreen modeExit fullscreen mode

In the above project, register a server database and consume the Oracle connection using the following code.

varcache=builder.AddRedis("cache");builder.AddProject<Projects.DotnetAspireChallenge_Web>("webfrontend").WithExternalHttpEndpoints().WithReference(cache);
Enter fullscreen modeExit fullscreen mode

Step 2: Install another NuGet package

Install the following Nuget package into the subsequent project “DotnetAspireChallenge.ApiService

dotnet add package Aspire.StackExchange.Redis.DistributedCache
Enter fullscreen modeExit fullscreen mode

then register the context into the Program.cs file as follows

builder.AddRedisDistributedCache("cache");
Enter fullscreen modeExit fullscreen mode

To add additional connection string properties using the JSON syntax

{"Aspire":{"StackExchange":{"Redis":{"ConfigurationOptions":{"ConnectTimeout":5000,"ConnectRetry":3}}}}}
Enter fullscreen modeExit fullscreen mode

Congratulations..!! You’ve successfully integrated the Redis Cache component into the .Net Aspire project.

Output Cache

The HTML or any static content can be cached as well in a web app or a Blazer app.

// Add the output cachebuilder.AddRedisOutputCache();// Build the appvarapp=builder.Build();// Add the middlewareapp.UseOutputCache();
Enter fullscreen modeExit fullscreen mode

To cache a razor page use the “OutputCache” attribute as follows

@page"/"@attribute[OutputCache(Duration=10)]
Enter fullscreen modeExit fullscreen mode

If your project requires to cache the response of APIs, you can use the same “OutputCache” attribute as follows

app.MapGet("/products/{ProdId}",(intProdId)=>$"The product ID is{ProdId}.").CacheOutput();app.MapGet("/products/{ProdId}",[OutputCache](intProdId)=>$"The product ID is{ProdId}.");
Enter fullscreen modeExit fullscreen mode

Github Project

GitHub - ssukhpinder/DotnetAspireChallenge: 10 Day .Net Aspire Challenge

More Cheatsheets

Cheat Sheets — .Net

C# Programming🚀

Thank you for being a part of the C# community! Before you leave:

Follow us:Youtube |X |LinkedIn |Dev.to
Visit our other platforms:GitHub
More content atC# Programming

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A questionable humor from a developer who’s convinced his bugs are funnier than yours.
  • Location
    Surrey, Canada
  • Education
    M.Tech & B.Tech Computer Science
  • Pronouns
    he/him
  • Work
    Technical Lead at SourceFuse
  • Joined

More fromSukhpinder Singh

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp