当前位置: 首页 > news >正文

专门做音箱的网站秦皇岛seo优化

专门做音箱的网站,秦皇岛seo优化,专门做活动的网站,php网站开发文档怎么写摘要 缓存是一种非常常见的性能优化技术,在开发过程中经常会用到。.NET提供了内置的内存缓存类 MemoryCache,它可以很方便地存储数据并在后续的请求中快速读取,从而提高应用程序的响应速度。 正文 通过使用 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 name="sender"></param>/// <param name="e"></param>private 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()        {            Name="Rick",            Age=99        };
        // 添加对像数据到缓存        var cacheEntryOptions = new MemoryCacheEntryOptions        {            AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(1) // 缓存有效期为 1 分钟        };        cache.Set<Person>("p1", person);        timer.Start();    }
    private void btnGetCache_Click(object sender, EventArgs e)    {        // 从缓存中获取数据        if (cache.TryGetValue<Person>("p1", out Person cachedValue))        {            MessageBox.Show(cachedValue.ToString());        }        else        {            MessageBox.Show("没有找到cache");        }    }
    /// <summary>    /// 删除cache    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></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()    {        Name="Rick",        Age=99    };
    // 添加对像数据到缓存    var cacheEntryOptions = new MemoryCacheEntryOptions    {        AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(1), // 缓存有效期为 1 分钟        PostEvictionCallbacks =        {            new PostEvictionCallbackRegistration            {                EvictionCallback=Cache_EntryRemoved,                State =this            }        }    };    cache.Set<Person>("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.hkea.cn/news/706269/

相关文章:

  • web网站开发作品关键词歌词图片
  • 汕头行业网站seo培训公司
  • 网站背景图片优化关键词歌曲免费听
  • 郑州做网站哪家专业我要发布信息
  • 西安做网站优化的公司石家庄seo按天扣费
  • 2022年西安封城通知自动app优化下载
  • 无锡做网站哪家公司好一个公司可以做几个百度推广
  • 专题网站建设工作关键词林俊杰无损下载
  • adobe 网站开发软件软文写作兼职
  • 英文网站建设 淮安免费培训网站
  • 隔离需要多少钱湖南网站seo找行者seo
  • wordpress简单企业站seo怎么刷排名
  • 网站建设与运维泉州全网推广
  • 网站建站哪个公司好一点营销咨询服务
  • 值得玩的网页游戏北京seo营销培训
  • 中国建设银行网站分期通百度推广登录平台网址
  • 公司内部网站源码新闻软文推广案例
  • vf建设银行网站谷歌seo排名
  • 如何申请商业服务器武汉seo工厂
  • 祥云平台英文网站微博指数查询入口
  • 公司网站建设准备资料今日重大财经新闻
  • 发布网站后备案免费网站建站页面
  • 浙江建设职业技术学院迎新网站做一个网站要多少钱
  • axure做网站好不好手机百度问一问
  • 开发微信小程序的流程广州seo优化电话
  • 小企业网站建设和管理全能搜
  • 无棣县建设局网站游戏优化大师下载安装
  • 小额贷款 网站模板品牌推广软文
  • 网站建设开发成本天津百度搜索网站排名
  • 做的好的营销型网站有哪些内容外贸网站外链平台