SlideShare a Scribd company logo
.NET 4.0 - SYSTEM.RUNTIME.CACHING
Larry Nung
AGENDA
Feature
Component
Develop Flow
DEMO
Reference
Q & A
2
FEATURE
FEATURE
 可擴充
 可套用至所有 .NET Framework 應用程式
 內建記憶體快取的實現
 主要是在擴展性方面做了改進
 改變了原來只能利用內存進行緩存的侷限
 允許用戶在不改變代碼的情況下通過修改配置的方式,靈活的切換緩存
介質
COMPONENT
OBJECTCACHE
 記憶體快取框架中的主要類別
 提供存取物件快取所需方法和屬性
 支援加入、擷取與更新快取資料
 主要任務為建立管理快取元素、指定快取回收與到期資訊、觸發元件變更事件。
 快取框架中所提供的MemoryCache類別即為ObjectCache抽象類別的實作
OBJECTCACHE
OBJECTCACHE
OBJECTCACHE
OBJECTCACHE
CHANGEMONITOR
 為一抽象基底類別
 用以監控快取項目所依存之資料的狀態變更
 可監控快取介質的狀態並判斷快取是否過期需要更新,透過適當的設定
可讓資料在享有快取好處的同時也能保證資料不至於過時
 快取框架中的CacheEntryChangeMonitor、FileChangeMonitor、
HostFileChangeMonitor、SqlChangeMonitor等類別都是該抽象類別的
實作
CHANGEMONITOR
CHANGEMONITOR
CHANGEMONITOR
CACHEITEMPOLICY
 快取項目收回和期限的詳細資料
CACHEITEMPOLICY
CACHEITEMPOLICY
DEVELOP FLOW
DEVELOP FLOW
 Assembly
 System.Runtime.Caching.dll
 Namespace
 System.Runtime.Caching
DEVELOP FLOW
using System.Runtime.Caching;
...
private ObjectCache _cache;
private ObjectCache m_Cache
{
get {
if (_cache == null)
_cache = MemoryCache.Default;
return _cache;
}
}
...
const string CACHE_KEY = "Content";
var content = m_Cache[CACHE_KEY] as string;
if (content == null) {
m_Cache.Set(CACHE_KEY, GetContent(), GetPolicy());
}
Init
ObjectCache
Set Content &
CacheItemPolicy
to ObjectCache
Get content from
cache
DEMO
CacheItemPolicy
larrynung/CacheItemPolicyDemo
CACHEITEMPOLICY
…
public String GetContent() {
const string CACHE_KEY = "Content";
string content = m_Cache[CACHE_KEY] as string;
if (content == null) {
CacheItemPolicy policy = new CacheItemPolicy();
//policy.AbsoluteExpiration = DateTime.Now.AddMilliseconds(1500);
policy.SlidingExpiration = TimeSpan.FromMilliseconds(1500);
//policy.UpdateCallback = new CacheEntryUpdateCallback((args) => {
// Console.WriteLine("MyCachedItemUpdatedCallback...");
// Console.WriteLine(string.Format("Remove {0}...Because {1}",args.Key,args.RemovedReason.ToString()));
// Console.WriteLine(new string('=', 50)); //});
//policy.RemovedCallback = new CacheEntryRemovedCallback((args) => {
//Console.WriteLine("CacheEntryRemovedCallback...");
//Console.WriteLine(string.Format("Remove {0}...Because {1}", args.CacheItem.Key, args.RemovedReason.ToString()));
Console.WriteLine(new string('=', 50)); });
content = Guid.NewGuid().ToString();
Thread.Sleep(1000);
m_Cache.Set(CACHE_KEY, content, policy);
}
return content;
}
…
DEMO
HostFileChangeMonitor
larrynung/HostFileChangeMonitorDemo
HOSTFILECHANGEMONITOR
…
public String GetContent() {
const string CACHE_KEY = "Content";
string content = m_Cache[CACHE_KEY] as string;
if (content == null) {
CacheItemPolicy policy = new CacheItemPolicy();
policy.SlidingExpiration = TimeSpan.FromMilliseconds(1500);
var changeMonitor = new HostFileChangeMonitor(new List<string> { File });
policy.ChangeMonitors.Add(changeMonitor);
content = System.IO.File.ReadAllText(File);
Thread.Sleep(1000);
m_Cache.Set(CACHE_KEY, content, policy);
}
return content;
}
…
DEMO
Custom ChangeMonitor
larrynung/CustomChangeMonitorDemo
CUSTOM CHANGEMONITOR
using System.Runtime.Caching;
...
public class ClipboardChangeMonitor : ChangeMonitor {
...
private string m_ClipboardText {
get { return _clipboardText ?? string.Empty; }
set {
if (_clipboardText == value) return;
_clipboardText = value;
OnChanged(value);
}
}
public override string UniqueId {
get { return _uniqueID ?? (_uniqueID = Guid.NewGuid().ToString()); }
}
public ClipboardChangeMonitor() {
m_Timer.Interval = 1000;
m_Timer.Tick += m_Timer_Tick;
m_Timer.Start();
_clipboardText = Clipboard.GetText();
InitializationComplete();
}
...
void m_Timer_Tick(object sender, EventArgs e) {
m_ClipboardText = Clipboard.GetText();
}
}
REFERENCE
27
REFERENCE
 .NET 4.0 New Feature - System.Runtime.Caching - Level Up- 點部落
 http://www.dotblogs.com.tw/larrynung/archive/2010/11/26/19746.aspx
 How to customize .NET 4.0's
System.Runtime.Caching.ChangeMonitor - Level Up- 點部落
 http://www.dotblogs.com.tw/larrynung/archive/2013/06/12/105458.aspx
 How to: Cache Objects Simply using
System.Runtime.Caching.MemoryCache - CodeProject
 http://www.codeproject.com/Articles/756423/How-to-Cache-Objects-Simply-
using-System-Runtime-C
REFERENCE
 larrynung/CacheItemPolicyDemo
 https://github.com/larrynung/CacheItemPolicyDemo
 ObjectCache 類別 (System.Runtime.Caching)
 https://msdn.microsoft.com/zh-
tw/library/system.runtime.caching.objectcache(v=vs.110).aspx
 MemoryCache 類別 (System.Runtime.Caching)
 https://msdn.microsoft.com/zh-tw/library/dd780634(v=vs.110).aspx
REFERENCE
 CacheItemPolicy 類別 (System.Runtime.Caching)
 https://msdn.microsoft.com/zh-
tw/library/system.runtime.caching.cacheitempolicy(v=vs.110).aspx
 ChangeMonitor 類別 (System.Runtime.Caching)
 https://msdn.microsoft.com/zh-
tw/library/system.runtime.caching.changemonitor(v=vs.110).aspx
Q&A
31
QUESTION & ANSWER
32

More Related Content

Similar to .Net 4.0 system.runtime.caching

Caching for J2ee Enterprise Applications
Caching for J2ee Enterprise ApplicationsCaching for J2ee Enterprise Applications
Caching for J2ee Enterprise Applications
Debajani Mohanty
 
Spring framework
Spring frameworkSpring framework
Spring framework
Kani Selvam
 
Krazykoder struts2 intro
Krazykoder struts2 introKrazykoder struts2 intro
Krazykoder struts2 intro
Krazy Koder
 
A generic log analyzer for auto recovery of container orchestration system
A generic log analyzer for auto recovery of container orchestration systemA generic log analyzer for auto recovery of container orchestration system
A generic log analyzer for auto recovery of container orchestration system
Conference Papers
 

Similar to .Net 4.0 system.runtime.caching (20)

Caching for J2ee Enterprise Applications
Caching for J2ee Enterprise ApplicationsCaching for J2ee Enterprise Applications
Caching for J2ee Enterprise Applications
 
Whats new in Enterprise 5.0 Product Suite
Whats new in Enterprise 5.0 Product SuiteWhats new in Enterprise 5.0 Product Suite
Whats new in Enterprise 5.0 Product Suite
 
Top 8 WCM Trends 2010
Top 8 WCM Trends 2010Top 8 WCM Trends 2010
Top 8 WCM Trends 2010
 
Struts
StrutsStruts
Struts
 
Hibernate Interview Questions | Edureka
Hibernate Interview Questions | EdurekaHibernate Interview Questions | Edureka
Hibernate Interview Questions | Edureka
 
Eclipse plug in development
Eclipse plug in developmentEclipse plug in development
Eclipse plug in development
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
Framework adoption for java enterprise application development
Framework adoption for java enterprise application developmentFramework adoption for java enterprise application development
Framework adoption for java enterprise application development
 
Hibernate interview questions
Hibernate interview questionsHibernate interview questions
Hibernate interview questions
 
Krazykoder struts2 intro
Krazykoder struts2 introKrazykoder struts2 intro
Krazykoder struts2 intro
 
Php Web Frameworks
Php Web FrameworksPhp Web Frameworks
Php Web Frameworks
 
Caching In Java- Best Practises and Pitfalls
Caching In Java- Best Practises and PitfallsCaching In Java- Best Practises and Pitfalls
Caching In Java- Best Practises and Pitfalls
 
Hibernate Interview Questions and Answers
Hibernate Interview Questions and AnswersHibernate Interview Questions and Answers
Hibernate Interview Questions and Answers
 
ASP.NET 4.0 Cache Extensibility
ASP.NET 4.0 Cache ExtensibilityASP.NET 4.0 Cache Extensibility
ASP.NET 4.0 Cache Extensibility
 
Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2
 
Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2
 
Introduction to rook
Introduction to rookIntroduction to rook
Introduction to rook
 
Introduction to j2 ee frameworks
Introduction to j2 ee frameworksIntroduction to j2 ee frameworks
Introduction to j2 ee frameworks
 
Framework
FrameworkFramework
Framework
 
A generic log analyzer for auto recovery of container orchestration system
A generic log analyzer for auto recovery of container orchestration systemA generic log analyzer for auto recovery of container orchestration system
A generic log analyzer for auto recovery of container orchestration system
 

More from Larry Nung

More from Larry Nung (20)

Ansible - simple it automation
Ansible - simple it automationAnsible - simple it automation
Ansible - simple it automation
 
sonarwhal - a linting tool for the web
sonarwhal - a linting tool for the websonarwhal - a linting tool for the web
sonarwhal - a linting tool for the web
 
LiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data fileLiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data file
 
PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8
 
MessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization formatMessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization format
 
PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7
 
BenchmarkDotNet - Powerful .NET library for benchmarking
BenchmarkDotNet  - Powerful .NET library for benchmarkingBenchmarkDotNet  - Powerful .NET library for benchmarking
BenchmarkDotNet - Powerful .NET library for benchmarking
 
PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6
 
SonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code QualitySonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code Quality
 
Visual studio 2017
Visual studio 2017Visual studio 2017
Visual studio 2017
 
Web deploy command line
Web deploy command lineWeb deploy command line
Web deploy command line
 
Web deploy
Web deployWeb deploy
Web deploy
 
SikuliX
SikuliXSikuliX
SikuliX
 
Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...
 
Common.logging
Common.loggingCommon.logging
Common.logging
 
protobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NETprotobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NET
 
PL/SQL & SQL CODING GUIDELINES – Part 5
PL/SQL & SQL CODING GUIDELINES – Part 5PL/SQL & SQL CODING GUIDELINES – Part 5
PL/SQL & SQL CODING GUIDELINES – Part 5
 
Regular expression
Regular expressionRegular expression
Regular expression
 
PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4
 
Fx.configuration
Fx.configurationFx.configuration
Fx.configuration
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 

Recently uploaded (20)

IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 

.Net 4.0 system.runtime.caching