Cache for Microsoft Enterprise Library

Source: Internet
Author: User

Cache for Microsoft Enterprise Library

Typically, applications can improve performance by storing data that is frequently accessed, as well as those that require a lot of processing time to be created in memory. Based on the Microsoft Enterprise Library, we quickly create a cache implementation.

New PrismSample.Infrastructure.Cache

Create a new Class library project, name it PrismSample.Infrastructure.Cache, and then download the Microsoft Enterprise Library Cache from NuGet.

Then create our CacheManager class:

Using Microsoft.Practices.EnterpriseLibrary.Caching;Using System.ComponentModel.Composition;Namespaceprismsample.infrastructure.cache{[Export ("Prismsamplecache",typeof (Icachemanager))]PublicClassCacheManager:Icachemanager {Icachemanager _cachemanager;PublicCacheManager () {_cachemanager = Cachefactory.getcachemanager ("Memorycachemanager"); }PublicObjectthis[String key] {get {return _cachemanager[key]; } }Publicint Count {get {return _cachemanager.count; } }PublicvoidADD (String key,ObjectValue) {_cachemanager.add (key,Value); }PublicvoidADD (String key,ObjectValue, CacheItemPriority scavengingpriority, Icacheitemrefreshaction refreshaction,params icacheitemexpiration[] expirations) {_cachemanager.add (key,Value, Scavengingpriority, Refreshaction, expirations); }Publicboolcontains (string key) {return _cachemanager.contains (key); } public void flush (public object getdata (string key) {return _cachemanager.getdata (key); } public void remove (string key) {_ Cachemanager.remove (key); } }} 

Where Memorycachemanager is the one we need to configure in the app. Config file later.

To modify a post-build event:

"$(TargetPath)" "$(SolutionDir)\PrismSample\bin\Debug\" /Y

The post-build event was added because the introduction of the cache's class library was not implemented through project references between projects, but was the container of the runtime MEF to find the instruction set, so we put the assembly in the run directory.

Modify the app. config configuration file
<?xml version= "1.0" encoding= "Utf-8"?><Configuration><Configsections><SectionName="Cachingconfiguration"Type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching "/></Configsections><Startup><Supportedruntimeversion="V4.0"sku=". netframework,version=v4.6.1 "/></Startup><Runtime><Assemblybindingxmlns="Urn:schemas-microsoft-com:asm.v1" ><Dependentassembly><assemblyidentityName="Microsoft.Practices.ServiceLocation"publickeytoken="31bf3856ad364e35"Culture="Neutral"/><BindingRedirectoldversion="0.0.0.0-1.3.0.0"newversion="1.3.0.0"/></Dependentassembly></Assemblybinding></Runtime><CachingconfigurationDefaultcachemanager="Memorycachemanager" ><Cachemanagers><AddName="Memorycachemanager"Type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching"expirationpollfrequencyinseconds="60"maximumelementsincachebeforescavenging="1000"numbertoremovewhenscavenging= "Nullbackingstore"/> </ cachemanagers> <backingStores> Span class= "Hljs-tag" ><add type= " Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching "name=" Nullbackingstore "/> </backingstores> </cachingconfiguration></CONFIGURATION>            

Configuration files can be generated through the Microsoft Enterprise Library tools (configuration method)

Modify bootstrapper to import all eligible DLLs under the directory
Using PRISM.MEF;Using PrismSample.Infrastructure.Abstract.Presentation.Interface;Using System.ComponentModel.Composition.Hosting;Using System.Windows;Using Prism.logging;Using PrismSample.Infrastructure.Logger;Using System.IO;Using System;Namespaceprismsample{PublicClassBootstrapper:Mefbootstrapper {PrivateConstString Search_pattern ="Prismsample.infrastructure.*.dll";ProtectedOverride DependencyObjectCreateshell () {Iviewmodel Shellviewmodel =This. Container.getexportedvalue<iviewmodel> ("Shellviewmodel");Return Shellviewmodel.viewAs DependencyObject; }ProtectedOverridevoidInitializeshell () {Application.Current.MainWindow = (Shell)This. Shell; Application.Current.MainWindow.Show (); }ProtectedOverridevoidConfigureaggregatecatalog () {Base. Configureaggregatecatalog ();Load yourselfThis. AGGREGATECATALOG.CATALOGS.ADD (New Assemblycatalog (This. GetType (). Assembly));Load current directory DirectoryInfo Dirinfo =new DirectoryInfo (@ ". \"); foreach (FileInfo FileInfo in dirinfo.enumeratefiles (SEARCH_ PATTERN) {try {this. AGGREGATECATALOG.CATALOGS.ADD (new Assemblycatalog (Fileinfo.fullname));} catch (Exception ex) {this. Logger.Log (string. Format ( "Import exception: {0}", ex. Message), category.exception, Priority.none); }}} protected override Iloggerfacade createlogger ( return new Logger ();}}}          
Test run

Modifying the Shellviewmodel constructor

[Importingconstructor]public shellviewmodel ([Import ( Span class= "hljs-string" > "Shellview", typeof (IView))]iview view, [Import]iloggerfacade Logger, [import ( "Prismsamplecache" , typeof (Icachemanager))] Icachemanager _cachemanager) {this . view = view; this. View.datacontext = this; _cachemanager.add ( "SampleValue", Span class= "hljs-string" > "Cachevalue"); this._text = _cachemanager.getdata ( "SampleValue"). ToString (); Logger. Log ( "Shellviewmodel Created", Category.info, Priority.none);}     

Operation Result:

Summary

This paper uses Microsoft Enterprise Library to implement a simple cache system.
SOURCE download

Reference information

patterns & Practices–enterprise Library
Bill: Microsoft Enterprise Library 5.0 Series Tutorial (i): Caching application Block (beginner)

Cache for Microsoft Enterprise Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.