`

Prism enterprise Cache Manager

    博客分类:
  • .net
阅读更多

Caching is a advanced topic if you decied to develope some enterprise level application. There are a case studied in this page "Cache and DataBase synchronization through SqlDependency". 

 

 

A feel of the CacheManager

 

However, in this article, I am going to show a flavor of the CacheManager by analysing a consistence issue regarding CacheManager. The code and the configuration is based on "Microsoft Enterprise Library Caching Application Block not thread safe?"

 

 

First let see the configuration in a file called EnterpriseCacheManager.config file.

 

 

 

<configuration>
  <configSections>
    <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <cachingConfiguration defaultCacheManager="Cache Manager">
    <cacheManagers>
      <add expirationPollFrequencyInSeconds="1" maximumElementsInCacheBeforeScavenging="1000"
      numberToRemoveWhenScavenging="10" backingStoreName="Null Storage"
      type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      name="Cache Manager" />
    </cacheManagers>
    <backingStores>
      <add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      name="Null Storage" />
    </backingStores>
  </cachingConfiguration>
</configuration>

 

 

 

and the source code that manipulate the CacheManager is like this : 

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

namespace EnterpriseCacheManager
{
  class Program
  {
    public static ICacheManager cacheManager = null;

    private static void InitializeCacheManager()
    {
      if (cacheManager == null)
      {
        var fileSource = new FileConfigurationSource("EnterpriseCacheManager.config");
        var factory = new CacheManagerFactory(fileSource);
        cacheManager = factory.CreateDefault();
      }
    }
    
    static void Main(string[] args)
    {
      Test2();
    }

    private static void Test1()
    {
      InitializeCacheManager();

      while (true)
      {
        System.Threading.Thread.Sleep(1000);
        var key = new Random().Next(3).ToString();
        string value = "";

        lock (cacheManager)
        {
          if (!cacheManager.Contains(key))
          {
            cacheManager.Add(key, key, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromSeconds(5)));
            value = (string)cacheManager.GetData(key);
          }
        }

        Console.WriteLine("{0} ---> '{1}'", key, value);
      }
    }


    static void Test2()
    {
      /**
       *
       * As explained in the reply from Tuzo, in his own word. &lt;quote&gt;http://stackoverflow.com/questions/1302643/microsoft-enterprise-library-caching-application-block-not-thread-safe/&lt;quote&gt;
       * What you are seeing is that your CacheItem has expired due to the 5 second SlidingTime expiration.

Before returning the cached value, the GetData method performs a check to see if the CacheItem has expired. If it has expired, the CacheItem is removed from the cache and null is returned. However, the call to Contains will return true because the CacheItem is in the cache even though it's expiration may have elapsed. This seems to be by design. With that in mind, it would be wise not to cache a null value to represent no data since you would not be able to discern an expired CacheItem from an actual cached value of null.

Assuming that you do not cache a null value then Luke's solution should suit you:
       */
      InitializeCacheManager();

      while (true)
      {
        System.Threading.Thread.Sleep(1000);
        var key = new Random().Next(3).ToString();

        string value = "";

        lock (cacheManager)
        {
          value = cacheManager.GetData(key) as string;
          if (value == null)
          {
            value = key;
            cacheManager.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromSeconds(5)));
          }
          value = cacheManager.GetData(key) as string;
        }

        Console.WriteLine("{0} ---> '{1}'", key, value);
      }
    }
  }
}
 

 

If you run Test1(), then you might see something like this in the output.

 

 

2 --> '2'
1 --> '1'
2 --> '2'
0 --> '0'
2 --> '2'
0 --> '0'
1 --> ''
0 --> '0'
1 --> '1'
2 --> ''
0 --> '0'
2 --> '2'
0 --> '0'
1 --> ''
2 --> '2'
1 --> '1'
Press any key to continue . . .

 

 

What CacheManager can lend itself to?

 

CacheManager can have the following. 

 

GetDate

StoreData

Flush

Remove

Expiration time on CacheItem

Expiration Strategy on cacheItem (FileDependency (when file is updated), AbsoluteTime, NeverExpired, SlidingTime (time window))

...

 

 

 

NOTE: This topic may deserve further exploration.

 

 

 

 

 

分享到:
评论

相关推荐

    Prism Manual.pdf Prism8框架官方手册 MD 文件转pdf

    Prism是一个流行的框架,它可以帮助开发者构建适用于.NET平台的模块化应用程序。本手册介绍的是Prism框架的官方指南,并说明了如何将Markdown文件转换成PDF格式。Prism框架主要针对WPF(Windows Presentation ...

    Delphi Prism Xe Crack

    4. Run C:\Program Files\Embarcadero\Delphi Prism\Bin\LicenseManager.exe 5. Selete the menu item "License\Import",import C:\Program Files\Embarcadero\Delphi Prism\License\PrismXe.slip 6. exit and save....

    WPF Prism实例(多个实例包含Prism各个重要概念实例)

    **Prism是什么** Prism是微软 Patterns & Practices 团队开发的一个开源框架,主要用于构建Windows Presentation Foundation (WPF)和Universal Windows Platform (UWP)应用。它提供了多种设计模式和最佳实践,帮助...

    Prism经典案例,Prism经典应用

    Prism是一种广泛应用于WPF(Windows Presentation Foundation)和UWP(Universal Windows Platform)应用程序开发的框架,由微软 Patterns & Practices 团队开发。这个框架旨在帮助开发者构建可维护、可扩展且遵循...

    C#Prism 例子与源码

    **Prism 框架详解** Prism 是一个开源的微软 .NET 框架,专为构建可扩展、模块化、松耦合的 WPF 和 UWP 应用程序而设计。它由 Microsoft patterns & practices 团队开发,旨在帮助开发者遵循最佳实践和设计模式。在...

    prism模板 vs插件

    prism模板 vs插件

    Prism.Maui-master.zip

    Prism.Maui 是一个强大的框架,它将 Prism 框架与 .NET MAUI(Multi-platform App UI)相结合,用于构建高效、可维护且模块化的跨平台移动和桌面应用程序。Prism 是一个针对 Windows Presentation Foundation (WPF)...

    WPF Prism 可关闭的TabControl

    在本文中,我们将深入探讨如何在WPF应用中利用Prism框架实现一个可关闭的TabControl。WPF(Windows Presentation Foundation)是.NET Framework的一部分,提供了一种强大的方式来构建丰富的、具有图形用户界面的应用...

    Prism 4.0安装包

    Prism 4.0 是一个流行且强大的框架,主要用于构建模块化、可维护性和可扩展的WPF(Windows Presentation Foundation)和Silverlight应用程序。这个安装包包含了一系列针对Desktop、Silverlight以及Windows Phone平台...

    GraphPad Prism 5.0 usb免安装版

    GraphPad Prism 是一个包括基础生命统计、曲线匹配和科学作图的强大结合体。在一百多个国家有超过 10 万个科学家正在试用 Prism 来分析、绘图和呈现他们的科学数据。 是什么令许多世界上领先的大学、医学中心、研究...

    Graphpad-Prism5中文教程

    GraphPad Prism 5.0 中文教程 GraphPad Prism 5.0 是一个功能强大的数据分析和图形化工具,它可以帮助用户快速、准确地处理和可视化数据。在本教程中,我们将详细介绍 GraphPad Prism 5.0 的使用方法和技巧,从...

    Prism7.1.0.431_WPF_官方中文文档

    Prism是Microsoft Patterns & Practices团队开发的一个开源框架,主要用于构建模块化、可维护、可测试的Windows Presentation Foundation (WPF)应用程序。这个“Prism7.1.0.431_WPF_官方中文文档”提供了Prism 7.1...

    Prism7.1.0.431_WPF_官方中文文档.pdf

    Prism是一个为.NET框架设计的框架,其目的在于帮助开发者构建松散耦合、易于维护和扩展的复合应用程序。Prism提供了设计模式实现,比如模块化开发和模型-视图-视图模型(MVVM)模式。文档中的内容详细介绍了使用...

    C#+WPF+Prism模块化开发示例

    **C# WPF Prism模块化开发示例** 在软件开发中,C#、WPF(Windows Presentation Foundation)和Prism框架结合使用,可以构建出高效、可维护且可扩展的桌面应用程序。本示例旨在介绍如何利用Prism进行模块化的WPF...

    Prism7.1.0.431_WPF_官方中文文档.docx

    **Prism 7.1.0.431 WPF 官方中文文档概述** **1. WPF Prism 库简介** Prism 是一个开源框架,专为设计和构建Windows Presentation Foundation (WPF) 应用程序而设计,旨在简化开发过程并提高代码的可维护性和可...

    GraphPad.Prism.v5.0.注册版].prism.rar

    GraphPad.Prism.v5.0.注册版] This Statistics Guide is a companion to GraphPad Prism 5. Available for both Mac and Windows, Prism makes it very easy to graph and analyze scientific data. Download a free...

    prism中文翻译教程

    Prism框架是一个强大的开发工具,主要用于构建Windows Presentation Foundation (WPF)应用程序。它以其模块化、MVVM(Model-View-ViewModel)设计模式支持、依赖注入和事件管理等功能而受到开发者的欢迎。这个"prism...

    NetCore下WPF基于Prism的模块化开发

    在.NET Core平台上,Windows Presentation Foundation (WPF)的应用程序开发可以通过Prism框架实现模块化,以提高代码的可重用性和可维护性。本篇文章将深入探讨如何在NetCore环境下利用Prism进行WPF的模块化开发。 ...

    基于Prism框架的MVVM设计模式的WPF实例代码

    **Prism框架与MVVM设计模式** 在软件开发领域,特别是Windows Presentation Foundation(WPF)应用开发中,Prism框架和MVVM(Model-View-ViewModel)设计模式是两个非常重要的概念。本实例代码旨在展示如何结合两者...

Global site tag (gtag.js) - Google Analytics