をViewComponent
表示するためにを作成しました。List<Product>
リストはREST API
サービスから取得したデータを評価したものです。これは私のクラスの実装です。
public class ProductsViewComponent : ViewComponent
{
private readonly HttpClient _client;
public ProductsViewComponent(HttpClient client)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
}
public async Task<IViewComponentResult> InvokeAsync(string date)
{
using (var response = await _client.GetAsync($"/"product/get_products/{date}"))
{
response.EnsureSuccessStatusCode();
var products = await response.Content.ReadAsAsync<List<Product>>();
return View(products);
}
}
}
次のComponents
フォルダ内にあるhtmlテーブル内にリストをロードしますViews\Shared\Components\Products\Default.cshtml
。
私がしView
たProducts
ことを表示する必要があるそれぞれで:
@await Component.InvokeAsync("Products", new { date = myDate })
REST API
使用して呼び出されるHttpClient
ように構成にしStartup.cs
、次のとおりです。
services.AddHttpClient<ProductsViewComponent>(c =>
{
c.BaseAddress = new Uri('https://api.myservice.com');
});
これはうまく機能しますが、主な問題は、ユーザーがページをリロードするか、製品のリストを表示する必要がある別のビューに入るたびに、アプリが別のAPI
呼び出しを行うことです。
リストをキャッシュのようなものに保存しAPI
、日付が選択した前の日付と等しい場合に再度呼び出すのを防ぐことは可能ですか?
私は学んでいるASP.NET Core
ので、この議論についてはあまり専門家ではありません。
助けてくれてありがとう。
マイクロソフトのドキュメントによるとhttps://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-2.1
IMemoryCache
データのキャッシュに使用できます
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app)
{
app.UseMvcWithDefaultRoute();
}
}
のインスタンスを作成しますIMemoryCache
。これは、Microsoftのドキュメントの例です。これをすべて一緒に処理する別のクラスを作成できます。以下の例では、これはDateTimeを保存しているだけです。ただし、任意のオブジェクトをキャッシュに保存でき、キャッシュからその値を読み取ろうとすると、そのオブジェクトをTypeにキャストする必要があります。
上記のドキュメントを確認することを強くお勧めします。
public class HomeController : Controller
{
private IMemoryCache _cache;
public HomeController(IMemoryCache memoryCache)
{
_cache = memoryCache;
}
public IActionResult CacheTryGetValueSet()
{
DateTime cacheEntry;
// Look for cache key.
if (!_cache.TryGetValue(CacheKeys.Entry, out cacheEntry))
{
// Key not in cache, so get data.
cacheEntry = DateTime.Now;
// Set cache options.
var cacheEntryOptions = new MemoryCacheEntryOptions()
// Keep in cache for this time, reset time if accessed.
.SetSlidingExpiration(TimeSpan.FromSeconds(3));
// Save data in cache.
_cache.Set(CacheKeys.Entry, cacheEntry, cacheEntryOptions);
}
return View("Cache", cacheEntry);
}
}
更新:CacheKeys.Entry
すべてのキーが定義されている静的クラスです。(コーディング標準のみ)。上記のドキュメントリンクを確認してください。
public static class CacheKeys
{
public static string Entry { get { return "_Entry"; } }
public static string CallbackEntry { get { return "_Callback"; } }
public static string CallbackMessage { get { return "_CallbackMessage"; } }
public static string Parent { get { return "_Parent"; } }
public static string Child { get { return "_Child"; } }
public static string DependentMessage { get { return "_DependentMessage";} }
public static string DependentCTS { get { return "_DependentCTS"; } }
public static string Ticks { get { return "_Ticks"; } }
public static string CancelMsg { get { return "_CancelMsg"; } }
public static string CancelTokenSource { get { return "_CancelTokenSource";} }
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加