参考学习链接:https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/advanced/walkthrough-caching-application-data-in-a-wpf-application?redirectedfrom=MSDN

  • 创建缓存类的实例,即,将实例化新的 MemoryCache 对象。

  • 指定缓存使用 HostFileChangeMonitor 对象来监视文本文件中的更改。

  • 读取文本文件并将其内容缓存为缓存条目。

  • 显示缓存的文本文件的内容

创建WPFDemo 程序WpfApp2如下,验证使用 MemoryCache 缓存数据及缓存过期情况。

<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Width="100" Height="60" Content="Cache" Click="Button_Click"></Button>
<Button Content="Button" HorizontalAlignment="Left" Margin="117,145,0,0" VerticalAlignment="Top" Width="124" Height="74" Click="Button_Click_1"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Caching;
using System.Threading;
using System.Windows; namespace WpfApp2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void Button_Click(object sender, RoutedEventArgs e)
{
ObjectCache cache = MemoryCache.Default;
string fileContents = cache["filecontents"] as string; if (fileContents == null)
{
CacheItemPolicy policy = new CacheItemPolicy();
policy.AbsoluteExpiration =
DateTimeOffset.Now.AddSeconds(1000.0); List<string> filePaths = new List<string>();
filePaths.Add(@"C:\Users\wr\Desktop\cacheText.txt"); policy.ChangeMonitors.Add(new
HostFileChangeMonitor(filePaths)); // Fetch the file contents.
fileContents = File.ReadAllText(@"C:\Users\wr\Desktop\cacheText.txt") + "\n" + DateTime.Now.ToString(); cache.Set("filecontents", fileContents, policy);
}
MessageBox.Show(fileContents);
} private void Button_Click_1(object sender, RoutedEventArgs e)
{
ObjectCache cache = MemoryCache.Default;
string fileContents = cache["filecontents"] as string;
cache.Set(cache.GetCacheItem("filecontents"), new CacheItemPolicy() { AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(5.0) });
MessageBox.Show(cache["filecontents"].ToString());
Thread.Sleep(5000);
MessageBox.Show(cache["filecontents"] == null ? "缓存已清空" :cache["filecontents"].ToString());
}
}
}

在 WPF 应用程序中缓存应用程序数据的更多相关文章

  1. C#程序中从数据库取数据时需注意数据类型之间的对应,int16\int32\int64

    private void btn2_Click(object sender, RoutedEventArgs e)         {             using (SqlConnection ...

  2. MO拆分计划行程序中写入PRODUCTIONORDERS表数据出现重复导致报错(BUG)20180502

    错误提示:ORA-00001: 违反唯一约束条件 (ABPPMGR.C0248833319_6192)ORA-06512: 在 "STG.FP_MO_SPLIT", line 19 ...

  3. ABAP 在被访问的程序中获取访问程序的全局变量

    前些日子接到过一个看起来比较普通的需求: 存在一个系统标准函数组FG01,内含函数模块FM00,FM01……等等.在系统程序中,FM00会调用FM01,通过FM01获取获取某些数据. 需求要求,复制一 ...

  4. 在 Java 应用程序中绑定 Bean 和数据

    本指南介绍了 NetBeans IDE 对 Java 应用程序中 Bean 绑定和数据绑定的支持. 要学完本教程,您需要具备以下软件和资源. 软件或资源 要求的版本 NetBeans IDE 版本 7 ...

  5. 当数据库某张表数据发生变化时,更新c#程序中缓存的用法

    参考:http://www.webkaka.com/tutorial/asp.net/2012/111912/(SqlDependency和SqlCacheDependency缓存的用法及具体步骤) ...

  6. TCP程序中发送和接收数据

    这里我们来探讨一下在网络编程过程中,有关read/write 或者send/recv的使用细节.这里有关常用的阻塞/非阻塞的解释在网上有很多很好的例子,这里就不说了,还有errno ==EAGAIN ...

  7. qt 程序中执行额外程序和脚本

    1.最简单的,我们可以通过system直接启动一个应用程序或者脚本:(但是要调用 #include <stdlib.h>) system("./helloworld") ...

  8. (转)C#.NET WINFORM应用程序中控制应用程序只启动一次

    原文地址 :http://www.cnblogs.com/emanlee/archive/2009/08/31/1557379.html using System; using System.Thre ...

  9. Android程序中Acticity间传递数据

    在Android开发过程中,在不同的Acitivity之间传递数据的情况是非常常见的.我花费了一点时间来总结Acitivity之间的数据传递,记录下来. 1.简单传递键值对 这种传递方式非常简单,只需 ...

  10. 如何在WPF的DiagramControl中绘制一个类型数据关系图的方法

    https://www.devexpress.com/Support/Center/Question/Details/T418156 虽然是在wpf中,但是在win中也可以调用wpf控件,这个太棒了, ...

随机推荐

  1. shp文件及附属

    主文件 .shp - 用于存储要素几何的主文件,之前分享过该文件的结构详解:必需文件. .shx - 用于存储要素几何索引的索引文件:必需文件. .dbf - 用于存储要素属性信息的 dBASE 表, ...

  2. docker安装cas

    直接docker pull apereo/cas ,docker run的时候各种报错: standard_init_linux.go:178: exec user process caused &q ...

  3. tomcat部署cas6并配置自己的ssl证书

    ​配置并安装tomcat,详见我的文章:windows安装tomcat10 安装必备的软件:(在<windows安装tomcat10>中已详细配置) apache-tomcat-10.1. ...

  4. 深入聊聊async&Promise

    正文 最近在学习JavaScript里的async.await异步,对于其中的Promise状态以及背后的Js引擎实际运行状态不大理解且很感兴趣,因此花了一点时间仔细研究了一下. 从Example说起 ...

  5. k8s单节点改为高可用和更新证书

    master单节点添加master节点 apiServer添加域名更新证书 更新kubenertes证书有效期 环境 kubernetes v1.22.12 使用kubeadm安装的集群 #添加节点 ...

  6. docker-compose安装mongo

    创建目录 [root@localhost tools]# mkdir -p /root/tools/mongo/{data,conf,init} 创建初始化用户脚本 [root@localhost m ...

  7. Mac netstat 查看端口报错 netstat: option requires an argument -- p 解决

    netstat -anvp |grep 10001 查询端口的时候报错提示 意思是缺少协议. 解决方案在Mac上正确使用的方法是:即-f需要加上地址族,-p需要加上协议TCP或者UDP等 a)如果需要 ...

  8. PostgreSQL 的特点

    title: PostgreSQL 的特点 date: 2024/12/24 updated: 2024/12/24 author: cmdragon excerpt: PostgreSQL 是当今最 ...

  9. ESLint is disabled since its execution has not been approved or denied yet

    VS Code 装好ESLint 插件报黄线的问题,具体解决方法如下所示: ESLint is disabled since its execution has not been approved o ...

  10. AICA第6期-学习笔记汇总

    AICA第6期-学习笔记汇总 AICA第六期|预科班课程 1.<跨上AI的战车> 2.<产业中NLP任务的技术选型与落地> 3.<计算机视觉产业落地挑战与应对> 4 ...