- 浏览: 399645 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
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. <quote>http://stackoverflow.com/questions/1302643/microsoft-enterprise-library-caching-application-block-not-thread-safe/<quote> * 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.
发表评论
-
wpf – ListView交替背景色
2013-07-02 20:56 6551Wpf – Alternate background col ... -
C# - 简单介绍TaskScheduler
2013-06-29 17:18 12038标题: C# - 简单介绍TaskSchedulerTit ... -
C# 4.5 - the await and async return types
2013-01-21 15:25 1070In .net 4.5, there are new key ... -
.net - .net 4.0 security model framework v2 and v4 compatibility issues
2012-09-19 18:29 1416the .net framework has a new ... -
.net - tips of using Reflector
2012-08-24 08:24 696Reflector is a very powerful ye ... -
a .net 4.0 application and <startup> element may not work with Prism lib
2012-07-23 17:01 343this article may not have a pr ...
相关推荐
Prism是一个流行的框架,它可以帮助开发者构建适用于.NET平台的模块化应用程序。本手册介绍的是Prism框架的官方指南,并说明了如何将Markdown文件转换成PDF格式。Prism框架主要针对WPF(Windows Presentation ...
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....
**Prism是什么** Prism是微软 Patterns & Practices 团队开发的一个开源框架,主要用于构建Windows Presentation Foundation (WPF)和Universal Windows Platform (UWP)应用。它提供了多种设计模式和最佳实践,帮助...
Prism是一种广泛应用于WPF(Windows Presentation Foundation)和UWP(Universal Windows Platform)应用程序开发的框架,由微软 Patterns & Practices 团队开发。这个框架旨在帮助开发者构建可维护、可扩展且遵循...
**Prism 框架详解** Prism 是一个开源的微软 .NET 框架,专为构建可扩展、模块化、松耦合的 WPF 和 UWP 应用程序而设计。它由 Microsoft patterns & practices 团队开发,旨在帮助开发者遵循最佳实践和设计模式。在...
prism模板 vs插件
Prism.Maui 是一个强大的框架,它将 Prism 框架与 .NET MAUI(Multi-platform App UI)相结合,用于构建高效、可维护且模块化的跨平台移动和桌面应用程序。Prism 是一个针对 Windows Presentation Foundation (WPF)...
在本文中,我们将深入探讨如何在WPF应用中利用Prism框架实现一个可关闭的TabControl。WPF(Windows Presentation Foundation)是.NET Framework的一部分,提供了一种强大的方式来构建丰富的、具有图形用户界面的应用...
Prism 4.0 是一个流行且强大的框架,主要用于构建模块化、可维护性和可扩展的WPF(Windows Presentation Foundation)和Silverlight应用程序。这个安装包包含了一系列针对Desktop、Silverlight以及Windows Phone平台...
GraphPad Prism 是一个包括基础生命统计、曲线匹配和科学作图的强大结合体。在一百多个国家有超过 10 万个科学家正在试用 Prism 来分析、绘图和呈现他们的科学数据。 是什么令许多世界上领先的大学、医学中心、研究...
GraphPad Prism 5.0 中文教程 GraphPad Prism 5.0 是一个功能强大的数据分析和图形化工具,它可以帮助用户快速、准确地处理和可视化数据。在本教程中,我们将详细介绍 GraphPad Prism 5.0 的使用方法和技巧,从...
Prism是Microsoft Patterns & Practices团队开发的一个开源框架,主要用于构建模块化、可维护、可测试的Windows Presentation Foundation (WPF)应用程序。这个“Prism7.1.0.431_WPF_官方中文文档”提供了Prism 7.1...
Prism是一个为.NET框架设计的框架,其目的在于帮助开发者构建松散耦合、易于维护和扩展的复合应用程序。Prism提供了设计模式实现,比如模块化开发和模型-视图-视图模型(MVVM)模式。文档中的内容详细介绍了使用...
**C# WPF Prism模块化开发示例** 在软件开发中,C#、WPF(Windows Presentation Foundation)和Prism框架结合使用,可以构建出高效、可维护且可扩展的桌面应用程序。本示例旨在介绍如何利用Prism进行模块化的WPF...
**Prism 7.1.0.431 WPF 官方中文文档概述** **1. WPF Prism 库简介** Prism 是一个开源框架,专为设计和构建Windows Presentation Foundation (WPF) 应用程序而设计,旨在简化开发过程并提高代码的可维护性和可...
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框架是一个强大的开发工具,主要用于构建Windows Presentation Foundation (WPF)应用程序。它以其模块化、MVVM(Model-View-ViewModel)设计模式支持、依赖注入和事件管理等功能而受到开发者的欢迎。这个"prism...
在.NET Core平台上,Windows Presentation Foundation (WPF)的应用程序开发可以通过Prism框架实现模块化,以提高代码的可重用性和可维护性。本篇文章将深入探讨如何在NetCore环境下利用Prism进行WPF的模块化开发。 ...
**Prism框架与MVVM设计模式** 在软件开发领域,特别是Windows Presentation Foundation(WPF)应用开发中,Prism框架和MVVM(Model-View-ViewModel)设计模式是两个非常重要的概念。本实例代码旨在展示如何结合两者...