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

照片做视频的软件 模板下载网站好如何做网络营销?

照片做视频的软件 模板下载网站好,如何做网络营销?,建设网站需要服务器吗,福州鼓楼区网站建设摘要 缓存是一种非常常见的性能优化技术,在开发过程中经常会用到。.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.dt0577.cn/news/37672.html

相关文章:

  • 重庆网站优化排名推广软文营销经典案例
  • 在天津做网站的公司网络营销中的四种方法
  • 自己能不能做个网站高端网站建设深圳
  • 简约 时尚 高端 网站建设头条搜索站长平台
  • 做网站页面一般设置多大尺寸链接地址
  • 国外汽车配件网站模板最新域名查询ip
  • 烟台汽车租赁网站建设聊石家庄seo
  • 广西网站建设运营费用短视频seo营销系统
  • php网站的特点中国人民银行网站
  • 郑州市汉狮做网站知识营销
  • 网站建设自学网站404页面怎么做
  • 做域名跳转非法网站负什么责任贵阳seo网站推广
  • 杭州萧山区专业做网站的公司域名收录
  • 杭州做网站下沙提供seo顾问服务适合的对象是
  • 寻找昆明网站建设如何做seo
  • 邯郸市做网站友情链接交换源码
  • 厦门市建设区网站网站如何注册
  • 做番号网站犯法吗昆明百度关键词优化
  • 网站群建设方案网络营销运营推广
  • 杭州app定制开发seo流量是什么
  • 域名哪里注册seo中国是什么
  • 网站推广全过程百度一下电脑版
  • wordpress 腾讯云优化设计高中
  • 盘锦网络推广东莞网站优化公司哪家好
  • 如何免费申请网站域名seo排名优化推广报价
  • 网站快速上排名方法市场调研方法有哪些
  • 青海网站建设怎么建设长沙seo咨询
  • 门户网站建设的书籍百度网页翻译
  • 找图纸的网站网上广告宣传怎么做
  • 广州建设学校淘宝seo具体优化方法