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

商务网站设计制作好的工具和方法西安小程序开发费用

商务网站设计制作好的工具和方法,西安小程序开发费用,桂林工程建设信息网站,那样的网站18年摘要 缓存是一种非常常见的性能优化技术,在开发过程中经常会用到。.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.yayakq.cn/news/887405/

相关文章:

  • 网页模板免费下载网站wordpress多用户多模板
  • 东莞企业年检哪个网站做临沂网站备案公司
  • 狮岭做包包的网站怎样注册网站卖东西
  • 南昌营销网站公司哪家好拓展公司网站建设
  • 建设网站能自学吗wordpress笔记
  • 泰州网站建设公司哪家专业网站怎么做单页
  • 上海巨型网站建设wordpress 电影 插件
  • 怎么样制作网站教程wordpress开发者中心
  • 旅游网站的后台管理系统怎么做网站建设通知
  • 网站不备案会怎样外贸网站建站系统
  • 做网站 中介做网站 做应用
  • 江门网站制作费用网站建设的策划文案
  • 网站建设岗位任职资格视频制作网站推荐
  • 美食网站php源码合肥公司网站搭建服务商
  • 职业学校查询网站网站必须做可信认证
  • 网站建设与管理课程实训ui素材
  • 域名网站免费建站购卡链接网站怎么做
  • 更换网站域名 推广网络结构形成的系统解决什么问题
  • 微信公众平台制作网站购买域名后怎么建网站
  • 无障碍浏览网站怎么做国外有什么网站做游戏吗
  • 装修网站合作平台有哪些成都有实力的网站建设
  • 辽宁建设厅网站首页响应式网站例子
  • 网站排名怎么做 知乎wordpress菜单的意思
  • 网站开发网上宠物店管理系统美工是做什么的难学吗
  • 电影新网站如何做seo优化怎么制作网站视频教程
  • 咸阳免费做网站公司桂林做网站哪家公司好
  • 网站开发项目描述范文我的主页制作代码
  • 高端网站设计技术分析wordpress使用QQ头像
  • 网站建设源码开发网站建设相关文章
  • 做网站是怎么挣钱的上海外贸网站优化