在 WPF 应用程序中缓存应用程序数据
创建缓存类的实例,即,将实例化新的 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 应用程序中缓存应用程序数据的更多相关文章
- C#程序中从数据库取数据时需注意数据类型之间的对应,int16\int32\int64
private void btn2_Click(object sender, RoutedEventArgs e) { using (SqlConnection ...
- MO拆分计划行程序中写入PRODUCTIONORDERS表数据出现重复导致报错(BUG)20180502
错误提示:ORA-00001: 违反唯一约束条件 (ABPPMGR.C0248833319_6192)ORA-06512: 在 "STG.FP_MO_SPLIT", line 19 ...
- ABAP 在被访问的程序中获取访问程序的全局变量
前些日子接到过一个看起来比较普通的需求: 存在一个系统标准函数组FG01,内含函数模块FM00,FM01……等等.在系统程序中,FM00会调用FM01,通过FM01获取获取某些数据. 需求要求,复制一 ...
- 在 Java 应用程序中绑定 Bean 和数据
本指南介绍了 NetBeans IDE 对 Java 应用程序中 Bean 绑定和数据绑定的支持. 要学完本教程,您需要具备以下软件和资源. 软件或资源 要求的版本 NetBeans IDE 版本 7 ...
- 当数据库某张表数据发生变化时,更新c#程序中缓存的用法
参考:http://www.webkaka.com/tutorial/asp.net/2012/111912/(SqlDependency和SqlCacheDependency缓存的用法及具体步骤) ...
- TCP程序中发送和接收数据
这里我们来探讨一下在网络编程过程中,有关read/write 或者send/recv的使用细节.这里有关常用的阻塞/非阻塞的解释在网上有很多很好的例子,这里就不说了,还有errno ==EAGAIN ...
- qt 程序中执行额外程序和脚本
1.最简单的,我们可以通过system直接启动一个应用程序或者脚本:(但是要调用 #include <stdlib.h>) system("./helloworld") ...
- (转)C#.NET WINFORM应用程序中控制应用程序只启动一次
原文地址 :http://www.cnblogs.com/emanlee/archive/2009/08/31/1557379.html using System; using System.Thre ...
- Android程序中Acticity间传递数据
在Android开发过程中,在不同的Acitivity之间传递数据的情况是非常常见的.我花费了一点时间来总结Acitivity之间的数据传递,记录下来. 1.简单传递键值对 这种传递方式非常简单,只需 ...
- 如何在WPF的DiagramControl中绘制一个类型数据关系图的方法
https://www.devexpress.com/Support/Center/Question/Details/T418156 虽然是在wpf中,但是在win中也可以调用wpf控件,这个太棒了, ...
随机推荐
- Grid 网格布局备忘录
概述 网格布局(Grid)是最强大的 CSS 布局方案. 它将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局. Grid 布局与 Flex 布局有一定的相似性,都可以指定容器内部多个 ...
- VLC web(http)控制 (3) 播放控制
VLC web(http) 播放控制: 1.如果已经在打开视频 播放和暂停都是:http://127.0.0.1:8080/requests/status.xml?command=pl_pause ...
- 【读书笔记】 深入理解JVM第三版 JVM 运行时数据区
JVM 内存管理 堆 (Heap)线程共享 方法区 (Method Area)线程共享 虚拟机栈(VM Stack) 线程私有 本地方法栈 (Native Method Stack)线程私有 程序计数 ...
- 【Amadeus原创】idea实现java前后端代码自动化调试
代码结构: 1,meeting-server 后端 springBoot maven 2,metting-ui 前端 nodejs 3,两个文件夹都在early-meeting文件夹中 如何在 ...
- C/C++实例汇集(1)
1.用代码判断一个系统是16位系统还是32位系统? 以下是几种常见编程语言中判断系统是 16 位还是 32 位的代码示例 C语言: #include <stdio.h> int main( ...
- Slate文档编辑器-TS类型扩展与节点类型检查
Slate文档编辑器-TS类型扩展与节点类型检查 在之前我们基于slate实现的文档编辑器探讨了WrapNode数据结构与操作变换,主要是对于嵌套类型的数据结构类型需要关注的Normalize与Tra ...
- PostgreSQL 的历史
title: PostgreSQL 的历史 date: 2024/12/23 updated: 2024/12/23 author: cmdragon excerpt: PostgreSQL 是一款功 ...
- Kubernetes 服务发现 监控Endpoints
监控 Pod之前的apiserver 实际上就是一种特殊的 Endpoints,现在我们同样来配置一个任务用来专门发现普通类型的 Endpoint,其实就是 Service 关联的 Pod 列表,由于 ...
- Debian 12 (bookworm) 安装方法及其国内软件源
摘要: Debian 12 (bookworm) 已经于2023年6月份正式发布.以下为原来信息: Debian 12 (bookworm) 预计于2023年中期发布,但是目前(2023年4月)已经处 ...
- 《Kubernetes故障篇:calico/node is not ready: BIRD is not ready》
文章目录一.背景信息二.解决方法总结:整理不易,如果对你有帮助,可否点赞关注一下? 一.背景信息k8s集群部署后发现calico的pod未通过健康检查,如下所示: 通过命令kubectl descri ...