Enterprise Library step by step series (9): Buffer application blocks-entry

Source: Internet
Author: User
Preface: enterprise database Step by step The series of essays is now the ninth article, during which many friends have received support. They think that they have provided good information for beginners. I would like to express my heartfelt thanks here. Some friends reported the usage of the enterprise database.ArticleI already have a lot on the Internet. I hope I can write some articles on Enterprise Library Design. I am also very grateful to these friends for their suggestions. When I write this series of essays, I am a beginner. I hope to help beginners. So I will continue with this series of Essays. After finishing the introductory and advanced articles, I will write the class design articles, and finally I plan to write a summary article, as a summary of the enterprise database, I would like to thank you again for your attention and support.

 I. cache applicationProgramBlock overview:

The enterprise database buffering Application Block provides a convenient and scalable caching mechanism that allows developers to integrate local caches into their applications. It supports memory cache and backup storage (Optional). The latter can be an Application Block for accessing enterprise library data or an independent storage method (Isolated Storage

). The application block can be used without modification. It also provides all the functions required to retrieve, add, and delete cached data. Configurable expiration time and clearing policy are also part of the Application Block function.

Architects and developers face many challenges when building enterprise-level distributed applications. Caching helps you overcome some of the following challenges:

Performance:By storing the data that is as close as possible to the data user, the cache can improve the performance of the application. This prevents repeated data creation, processing, and transmission.

Scalability:Storing information in the cache helps to save resources and improve scalability as application requirements increase

Availability:By storing data in the local cache, applications can withstand system faults, such as network waiting time,WebService Problems and hardware faults

Applicable situations:

(1) The static data must be accessed repeatedly or data that is rarely changed.

(2Data Access overhead is high in terms of creation, access, or transmission.

(3) Even if the source (such as the server) is unavailable, the data must always be available.

The buffer application block can be applied to any of the following application types:

WindowsForm

Console

WindowsService

Enterprise Services

ASP. NET webApplication orWebService

The cache application block should be deployed in a single application domain. Each application domain can have one or more caches (either or not with backup storage ). The cache cannot be shared between different application domains.

The performance of the cache application block has been optimized, and it is thread-safe and exception-safe. You can expand it to include your own expiration Policy and backup storage.

 Ii. Buffer Storage:

Buffer Storage is divided into memory-resident buffer and disk-resident buffer.

1. Memory resident Buffer:

Typical applications of memory resident Buffer:

Applications often use the same data.

An application often needs to obtain data again.

2. Disk resident Buffer:

Typical applications of disk resident Buffer:

Large data volume

At the same time, it is costly to retrieve data from application service providers (such as databases ).

In the buffer lifecycle, the system must be restarted.

Iii. Typical buffer usage steps:

Before getting started, we also think that you already have a project andApp. configConfiguration file.

1.Run the Enterprise Library Configuration tool to open the project configuration file (Web. configOrApp. config), CreateCache Application Block.

2Select application | caching Application Block | cache manager. here we can see several parameters:

Expirationpollfrequencyinsecond: Indicates the expiration polling cycle, that is, how long it takes to check which cache items have expired and are executed by independent processes in the background. This parameter is critical. If the time is set too short, the background process will be executed continuously, occupying a large amount of resources.CPUTime, if it is too long, it will waste memory again, to find a not long or short, it is recommended to use performance counters to monitor, select a moderate parameter.

Maximumelementslncachebeforescavenging: Maximum number of elements in the buffer.

Name:Cache manage.Cache manageTo use different buffer policies.

Numbertoremovewhenscavenging: Number of items removed at a time.

Where2,4Two Parameters. I will focus on clearing them in the advanced section.

 

3. Create another isolation-based buffer.Isolated Storage.

 

4Finally, do not forget to copy the directory.

1 Copy " $ (Projectdir) \ *. config "   " $ (Targetdir) "

5. Add reference.

1 Using Microsoft. Practices. enterpriselibrary. caching;
2 Using Microsoft. Practices. enterpriselibrary. caching. expirations;
3 Using Microsoft. Practices. enterpriselibrary. Data;

6. Add cache items.

1 /**/ ///Read data from the database and put it in datareader.
2 Database DB = Databasefactory. createdatabase ( " Database instance " );
3 Idatareader Dr = DB. executereader (commandtype. Text, " Select * from products " );
4
5 This . Mydr = Dr;
6
7 /**/ ///Create a cachemanager
8 Cachemanager mycachemanager = Cachefactory. getcachemanager ();
9
10 /**/ ///Add cache items
11///Default clear priority: normal
12///Default expiration settings: No expiration
13///Note: The Key of the added entry is mydatareader. If you add another entry with the same key, the first entry will be overwritten.
14 Mycachemanager. Add ( " Mydatareader " , This . Mydr );

7. Read cache items.

When reading a cache entry, pay special attention to the following two points:

(1) The data type must be correct during reading, because the data type is determined by ourselves when the buffer is added, so we know the data type during reading;

(2) Make sure to check whether it is null during reading. Remember!

1 /**/ ///The type must be correct.
2 Idatareader tobedisplay = (Idatareader) mycachemanager. getdata ( " Mydatareader " );
3
4 /**/ ///To determine whether it is null
5 If (Tobedisplay ! =   Null )
6 {
7 While (Tobedisplay. Read ())
8 {
9This. ListBox. Items. Add (tobedisplay. getvalue (2));
10}
11 }

8. Remove cache entries:

ExploitationRemove ()Method

1 /**/ ///Remove entries
2 Mycachemanager. Remove ( " Filekey " );

Conclusion: This is the introduction to the cache application block. I hope you can help beginners. In this advanced article, we will introduce time-based expiration policies and reminder-based expiration policies, buffer storage encryption, entry removal, loading buffering, refresh buffering, and other content.

 

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.