728x90
반응형
728x170
■ 캐싱을 사용하면 빠른 액세스를 위해 데이터를 메모리에 저장할 수 있다. 데이터에 다시 액세스하면 애플리케이션은 원래 소스에서 데이터를 검색하는 대신 캐시에서 데이터를 가져올 수 있다. 이를 통해 성능과 확장성을 향상시킬 수 있다. 또한 캐싱은 데이터 원본을 일시적으로 사용할 수 없을 때 데이터를 사용할 수 있도록 한다.
▶ MainWindow.xaml
<Window x:Class="TestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800"
Height="600"
Title="TestProject"
FontFamily="나눔고딕코딩"
FontSize="16">
<Grid>
<Button Name="getContentButton"
Width="120"
Height="30"
Content="Get content" />
</Grid>
</Window>
▶ MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Caching;
using System.Windows;
namespace TestProject
{
/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 소스 파일 경로
/// </summary>
private string sourceFilePath = "cache.txt";
/// <summary>
/// 캐시 디렉토리 경로
/// </summary>
private string cacheDirectoryPath = "c:\\cache";
/// <summary>
/// 캐시 파일 경로
/// </summary>
private string cacheFilePath = "c:\\cache\\cache.txt";
/// <summary>
/// 캐시 키
/// </summary>
private string cacheKey = "fileData";
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
InitializeComponent();
if(!Directory.Exists(this.cacheDirectoryPath))
{
Directory.CreateDirectory(this.cacheDirectoryPath);
}
if(!File.Exists(this.cacheFilePath))
{
File.Copy(this.sourceFilePath, this.cacheFilePath);
}
this.getContentButton.Click += getContentButton_Click;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Get content 버튼 클릭시 처리하기 - getContentButton_Click(sender, e)
/// <summary>
/// Get content 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void getContentButton_Click(object sender, RoutedEventArgs e)
{
ObjectCache objectCache = MemoryCache.Default;
string fileData = objectCache[this.cacheKey] as string;
if(fileData == null)
{
CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
cacheItemPolicy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(10d);
List<string> filePathList = new List<string>();
filePathList.Add(this.cacheFilePath);
cacheItemPolicy.ChangeMonitors.Add(new HostFileChangeMonitor(filePathList));
fileData = File.ReadAllText(this.cacheFilePath) + "\n" + DateTime.Now.ToString();
objectCache.Set(this.cacheKey, fileData, cacheItemPolicy);
}
MessageBox.Show(fileData);
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] ControlTemplate 엘리먼트 : 버튼 배경색을 그라데이션 처리한 Button 엘리먼트 정의하기 (0) | 2023.01.08 |
---|---|
[C#/WPF] Frame 클래스 : Navigated 이벤트를 사용해 스크립트 오류 억제하기 (0) | 2023.01.08 |
[C#/WPF] Dispatcher 클래스 : Run 정적 메소드를 사용해 다중 윈도우 다중 스레드 사용하기 (0) | 2023.01.08 |
[C#/WPF] 백그라운드 스레드로 차단 작업 처리하기 (0) | 2023.01.08 |
[C#/WPF] DispatcherObject 클래스 : Dispatcher 속성을 사용해 장기 실행 계산이 포함된 단일 스레드 애플리케이션 만들기 (0) | 2023.01.07 |
[C#/WPF] RenderOptions 클래스 : SetCachingHint 정적 메소드를 사용해 캐싱 힌트 옵션 설정하기 (0) | 2023.01.04 |
[C#/WPF] HwndHost 클래스 : Win32 컨트롤 호스트하기 (0) | 2023.01.04 |
[C#/WPF] WindowsFormsHost 클래스 : PropertyMap 속성을 사용해 WinForm 컨트롤 속성 매핑 설정하기 (0) | 2023.01.02 |
[C#/WPF] WindowsFormsHost 엘리먼트 : 하이브리드 애플리케이션에서 데이터 바인딩하기 (0) | 2022.12.31 |
[C#/WPF] WindowsFormsHost 엘리먼트 : WinForm 레이아웃 컨트롤 사용하기 (0) | 2022.12.29 |
댓글을 달아 주세요