Create a timed cache with. Net

Source: Internet
Author: User
Tags hash new features thread
Create | timed | cache ZDNET china|2002-4-24 In the previous article, I discussed it in detail. The simple use of the net cache. But the point of use can only be the topic of the "aperitif." Now, we went further and added the expiration of entries in the cache based on the cache developed in that article.

The content of listing A is the code we developed last time.

New caching and its improvements
To create a timed cache, we first have to determine whether an object has expired. For the scope of this article, I assume that the user can determine the exact object expiration date. This way, in the future, it can be improved to allow objects that are stored in the cache to determine their expiration dates.

We first modify the Insertcachedobject method to support the expiration of objects in the cache. When an object is inserted into the cache, the method provides the user with an object expiration date. This date parameter is called Dtexpiration. The Insertcachedobject method stores the expiration date passed through this parameter. There are several ways to achieve this goal. For example, we can create a wrapper object that contains the date and object properties. If you do this, the Insertcachedobject method creates an instance of the encapsulated object and provides it with two properties. We choose a simpler second scenario: Create and declare a 2nd hash table, and specify that it runs in parallel with the first hash hash table. This second hash table is called htexpiration.

See listing B for changes to the Insertcachedobject method.

Now we need to find a way to discover expired entries. The simplest and most lazy way to make a request through the Getcachedobject method is to check each buffered object. This is a poor algorithm because the expiration object is not called until the processing request is issued. And it is likely that there is some uncertainty in time. Although these expiration objects reside in the cache, they consume valuable memory resources, which have an extremely negative impact on the performance of the application.

Without this lazy approach, we create a background thread that repeatedly checks the htexpiration hash table.

So we create a class called Thereaperthread, which has a method for creating infinite loops. The class is also declared as an inner class because it is used only within the context of the Customcache object. See listing C for the full code of the class.

You have to be aware of the shared constructor in Listing C, which is responsible for creating a new thread and starting the thread. We then create a test program to ensure that the cache is functioning properly. The test program is shown in Listing D, which is a console program, and of course you can use the. NET Framework to create the above programs as projects.


Summary
The timer cache is fully implemented. Although functionally simple, it is not possible to tell when cached user entries expire or allow objects to determine their own expiration algorithms. However, on the basis of this simple caching, we can discuss the development of new features, such as allocation, memory management, and so on in the future.



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.