첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ 캐싱을 사용하면 빠른 액세스를 위해 데이터를 메모리에 저장할 수 있다. 데이터에 다시 액세스하면 애플리케이션은 원래 소스에서 데이터를 검색하는 대신 캐시에서 데이터를 가져올 수 있다. 이를 통해 성능과 확장성을 향상시킬 수 있다. 또한 캐싱은 데이터 원본을 일시적으로 사용할 수 없을 때 데이터를 사용할 수 있도록 한다.

TestProject.zip
0.01MB

▶ 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
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요