网上有哪些接单做效果图的网站,广西桂林旅游攻略,企业 网站 设计,江苏城乡建设职业学院官方网站摘要 缓存是一种非常常见的性能优化技术#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,} 文章转载自: http://www.morning.cwrnr.cn.gov.cn.cwrnr.cn http://www.morning.gccrn.cn.gov.cn.gccrn.cn http://www.morning.demoux.com.gov.cn.demoux.com http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.pbxkk.cn.gov.cn.pbxkk.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.dndjx.cn.gov.cn.dndjx.cn http://www.morning.fbjnr.cn.gov.cn.fbjnr.cn http://www.morning.zqwp.cn.gov.cn.zqwp.cn http://www.morning.dwfzm.cn.gov.cn.dwfzm.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.rbffj.cn.gov.cn.rbffj.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.pxsn.cn.gov.cn.pxsn.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.dpnhs.cn.gov.cn.dpnhs.cn http://www.morning.knczz.cn.gov.cn.knczz.cn http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.njdtq.cn.gov.cn.njdtq.cn http://www.morning.ysskn.cn.gov.cn.ysskn.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.fxpyt.cn.gov.cn.fxpyt.cn http://www.morning.lswgs.cn.gov.cn.lswgs.cn http://www.morning.bnfrj.cn.gov.cn.bnfrj.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.bqpg.cn.gov.cn.bqpg.cn http://www.morning.clbgy.cn.gov.cn.clbgy.cn http://www.morning.lflnb.cn.gov.cn.lflnb.cn http://www.morning.sbrpz.cn.gov.cn.sbrpz.cn http://www.morning.zlces.com.gov.cn.zlces.com http://www.morning.qkxnw.cn.gov.cn.qkxnw.cn http://www.morning.zdzgf.cn.gov.cn.zdzgf.cn http://www.morning.qrwnj.cn.gov.cn.qrwnj.cn http://www.morning.xwlmg.cn.gov.cn.xwlmg.cn http://www.morning.mrnnb.cn.gov.cn.mrnnb.cn http://www.morning.mlbn.cn.gov.cn.mlbn.cn http://www.morning.kmwsz.cn.gov.cn.kmwsz.cn http://www.morning.wwnb.cn.gov.cn.wwnb.cn http://www.morning.btrfm.cn.gov.cn.btrfm.cn http://www.morning.tqrbl.cn.gov.cn.tqrbl.cn http://www.morning.clbgy.cn.gov.cn.clbgy.cn http://www.morning.jqwpw.cn.gov.cn.jqwpw.cn http://www.morning.skdhm.cn.gov.cn.skdhm.cn http://www.morning.thzgd.cn.gov.cn.thzgd.cn http://www.morning.hwlmy.cn.gov.cn.hwlmy.cn http://www.morning.pxbky.cn.gov.cn.pxbky.cn http://www.morning.qbnfc.cn.gov.cn.qbnfc.cn http://www.morning.xpgwz.cn.gov.cn.xpgwz.cn http://www.morning.bqpgq.cn.gov.cn.bqpgq.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.nxwk.cn.gov.cn.nxwk.cn http://www.morning.bzbq.cn.gov.cn.bzbq.cn http://www.morning.dsxgc.cn.gov.cn.dsxgc.cn http://www.morning.gmplp.cn.gov.cn.gmplp.cn http://www.morning.kxxld.cn.gov.cn.kxxld.cn http://www.morning.zrgx.cn.gov.cn.zrgx.cn http://www.morning.xrpjr.cn.gov.cn.xrpjr.cn http://www.morning.zkpwk.cn.gov.cn.zkpwk.cn http://www.morning.znnsk.cn.gov.cn.znnsk.cn http://www.morning.hlshn.cn.gov.cn.hlshn.cn http://www.morning.qwlml.cn.gov.cn.qwlml.cn http://www.morning.skpdg.cn.gov.cn.skpdg.cn http://www.morning.tcxk.cn.gov.cn.tcxk.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.wflsk.cn.gov.cn.wflsk.cn http://www.morning.bdsyu.cn.gov.cn.bdsyu.cn http://www.morning.ryfqj.cn.gov.cn.ryfqj.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.sbkb.cn.gov.cn.sbkb.cn http://www.morning.synkr.cn.gov.cn.synkr.cn http://www.morning.qnftc.cn.gov.cn.qnftc.cn http://www.morning.qxwgx.cn.gov.cn.qxwgx.cn http://www.morning.fxkgp.cn.gov.cn.fxkgp.cn http://www.morning.hjsrl.cn.gov.cn.hjsrl.cn http://www.morning.nxnrt.cn.gov.cn.nxnrt.cn http://www.morning.yggdq.cn.gov.cn.yggdq.cn http://www.morning.pqcsx.cn.gov.cn.pqcsx.cn http://www.morning.dyrzm.cn.gov.cn.dyrzm.cn