做网站注册商标哪一类,网站建设方案的策划书,建立网站可以赚钱吗,广州制作网站seo摘要 缓存是一种非常常见的性能优化技术#xff0c;在开发过程中经常会用到。.NET提供了内置的内存缓存类 MemoryCache#xff0c;它可以很方便地存储数据并在后续的请求中快速读取#xff0c;从而提高应用程序的响应速度。
正文 通过使用 Microsoft.Extensions.Caching.Me…摘要 缓存是一种非常常见的性能优化技术在开发过程中经常会用到。.NET提供了内置的内存缓存类 MemoryCache它可以很方便地存储数据并在后续的请求中快速读取从而提高应用程序的响应速度。
正文 通过使用 Microsoft.Extensions.Caching.Memory我们可以在 .NET Core 中轻松实现内存缓存功能从而提高应用程序的性能和响应速度。在实际应用中你可以根据具体需求来设置缓存的有效期和其他选项。
nuget 安装依赖 Microsoft.Extensions.Caching.Memory 一个简单例子
public partial class Form1 : Form{ // 创建 MemoryCache 实例 MemoryCache cache new MemoryCache(new MemoryCacheOptions());System.Timers.Timer timer new System.Timers.Timer(); int idx 0; public Form1() { InitializeComponent(); timer.Interval 1000; timer.Elapsed (o, e) { this.Invoke(new Action(() { lblTime.Text idx.ToString(); idx; })); }; }private void btnCreateCache_Click(object sender, EventArgs e) { // 添加数据到缓存 string key hi; string value Hello, World!; var cacheEntryOptions new MemoryCacheEntryOptions { AbsoluteExpiration DateTimeOffset.Now.AddMinutes(1) // 缓存有效期为 1 分钟 }; cache.Set(key, value, cacheEntryOptions); timer.Start(); }private void btnGetCache_Click(object sender, EventArgs e) { // 从缓存中获取数据 if (cache.TryGetValue(hi, out string cachedValue)) { MessageBox.Show(cachedValue); } else { MessageBox.Show(没有找到cache); } }} /// summary/// 删除cache/// /summary/// param namesender/param/// param namee/paramprivate void btnDeleteCache_Click(object sender, EventArgs e){ cache.Remove(hi);} 缓存一个对象
public class Person{ public string Name { get; set; } public int Age { get; set; }public override string ToString() { return this.Name this.Age.ToString(); }}
public partial class Form1 : Form{ // 创建 MemoryCache 实例 MemoryCache cache new MemoryCache(new MemoryCacheOptions());System.Timers.Timer timer new System.Timers.Timer(); int idx 0; public Form1() { InitializeComponent(); timer.Interval 1000; timer.Elapsed (o, e) { this.Invoke(new Action(() { lblTime.Text idx.ToString(); idx; })); }; }private void btnCreateCache_Click(object sender, EventArgs e) { Person person new Person() { NameRick, Age99 };// 添加对像数据到缓存 var cacheEntryOptions new MemoryCacheEntryOptions { AbsoluteExpiration DateTimeOffset.Now.AddMinutes(1) // 缓存有效期为 1 分钟 }; cache.SetPerson(p1, person); timer.Start(); }private void btnGetCache_Click(object sender, EventArgs e) { // 从缓存中获取数据 if (cache.TryGetValuePerson(p1, out Person cachedValue)) { MessageBox.Show(cachedValue.ToString()); } else { MessageBox.Show(没有找到cache); } }/// summary /// 删除cache /// /summary /// param namesender/param /// param namee/param private void btnDeleteCache_Click(object sender, EventArgs e) { cache.Remove(p1); }} 侦听几个事件使用PostEvictionCallbacks这个回调
private void btnCreateCache_Click(object sender, EventArgs e){ Person person new Person() { NameRick, Age99 };// 添加对像数据到缓存 var cacheEntryOptions new MemoryCacheEntryOptions { AbsoluteExpiration DateTimeOffset.Now.AddMinutes(1), // 缓存有效期为 1 分钟 PostEvictionCallbacks { new PostEvictionCallbackRegistration { EvictionCallbackCache_EntryRemoved, State this } } }; cache.SetPerson(p1, person, cacheEntryOptions); timer.Start();}
private static void Cache_EntryRemoved(object key, object value, EvictionReason reason, object state){ // 在 PostEvictionCallback 中处理逻辑 switch (reason.ToString()) { case Delete: MessageBox.Show(删除缓存了); break; default: break; }} 注意Reason这里能知道是什么操作
public enum EvictionReason{ None,/// summary /// Manually /// /summary Removed,/// summary /// Overwritten /// /summary Replaced,/// summary /// Timed out /// /summary Expired,/// summary /// Event /// /summary TokenExpired,/// summary /// Overflow /// /summary Capacity,}