Esbasic reusable. Net class library (16) -- regularly refresh the cache manager irefreshablecachemanager

Source: Internet
Author: User

1.Origin:

To improve system performance or reduce database pressure, we often use cache in the system to keep frequently used data in the memory. If, for some reason, the frequently used data in the cache cannot be synchronously updated with the data source in a timely manner, it may be a suitable option to use timed refresh to refresh the data in the cache.

If your cache is refreshed on a regular basis, you need to maintain a timer or loop engine for it. If there are multiple caches in your system that are refreshed regularly like this, and each cache has different time interval requirements, then make these caches run as expected, you need to spend some effort.

I have designed a timed cache refresh manager.IrefreshablecachemanagerTo help you manage all the caches in your system that require timed refresh. It can refresh the cache managed by each cache at the right time according to the Refresh Interval requirements. You no longer need to manually process the timer or loop engine,IrefreshablecachemanagerThis is automatically done for you.

 

2.Applicable scenarios:

(1)One or more caches need to be refreshed regularly in the system.

(2)The refresh interval required by each cache may be different.

(3)The interval of refreshing is not accurate.

 

3Design ideas and implementation

This section describes how to regularly refresh the cache manager.IrefreshablecachemanagerThe key is "manager", not "cache", which must be clear. The cache is only an object managed by the manager.

Can beIrefreshablecachemanagerThe managed cache must be a cache that can be refreshed regularly, that is, these caches must be implementedIrefreshablecacheInterface to indicate that you can accept the management of the manager.IrefreshablecacheDefinition:

Public   Interface   Irefreshablecache
{
///   <Summary>
/// Refreshspaninsecs: the time interval (in seconds) for timed refresh ). If it is set to 0, the refresh time is consistent with that of irefreshablecachemanager.
///   </Summary>
Int Refreshspaninsecs { Get ;}

///   <Summary>
/// Lastrefreshtime the last refresh time.
///   </Summary>
Datetime Lastrefreshtime { Get ; Set ;}

Void Refresh ();
}

This interface is quite simple. It only requires the cache to provideRefreshMethod, andRefreshspaninsecsAttribute indicates the interval at which you want to regularly refresh.If this attribute is set0(Default) indicates that the cache accepts the unified scheduling of the manager.

LastrefreshtimeThis attribute is used to record the end time of the last refresh. The value of this attribute is set by the manager.

FromIrefreshablecacheThe interface definition shows that the manager does not need to care about any information related to the data in the cache, which is implemented by the specific application.IrefreshablecacheInterface.

Next, let's take a lookIrefreshablecachemanagerInterface Definition:

Public   Interface   Irefreshablecachemanager
{
///   <Summary>
/// Refreshspaninsecs: The interval at which cache is refreshed regularly.
///   </Summary>
Int Refreshspaninsecs { Set ;}

Ilist<Irefreshablecache>Cachelist {Set;}

VoidInitialize ();

//


// refreshnow manually refresh all the managed caches.
//
void refreshnow ();

//


// addcache dynamically adds a cache.
//
void addcache ( irefreshablecache cache );

//


// removecache dynamically removes the cache.
//
void removecache ( irefreshablecache cache );

//


// cacherefreshfailed: this event is triggered when an exception is thrown during cache refreshing.
//
event cbcacheexception cacherefreshfailed;
}

this interface also has a refreshspaninsecs attribute, if the managed cache refreshspaninsecs set properties 0 , then the manager will schedule it with its own property. irefreshablecachemanager interface refreshspaninsecs attributes exist to simplify the situation where all the cached files are refreshed at a unified interval.

RefreshablecachemanagerThe implementation is still based on the loop engine described earlierAgilecycleengineFor timed control.

IrefreshablecachemanagerProvidedAddcacheAndRemovecacheThis method is used to dynamically add or remove caches during running.

If an exception is thrown when a cache is being refreshedIrefreshablecachemanagerWill triggerCacherefreshfailedEvent. The event parameter contains the cache and abnormal objects with exceptions.

 

AboutRefreshablecachemanagerNote the following:

(1)The implementation of the manager is thread-safe and can be used in a multi-threaded environment. We have locked the cache list.

(2)Manager (Initialize), The cycle engine (itsDetectspaninsecsMust be set1Seconds ).LastrefreshtimeAre set to the current time.

(3) In implementation Engineaction The Foreach Block Used in Try To capture exceptions thrown during engine refresh, instead Try Block. Foreach . This order is important if Try Block. Foreach When a cache is refreshed, an exception is thrown. Foreach After the cache is interrupted, the cache refresh method will not be detected or called.

(4)The lock is not only used to synchronize changes to the internal cache list set, but also to manually call the refresh MethodRefreshnowIt also needs to be synchronized. Otherwise, two threads may detect and refresh the cache at the same time (one is the thread of the loop engine and the other is manually called ).RefreshnowMethod thread ).

 

4.Precautions for use

(1)Because the internal implementation of the Manager uses a loop engine, the interval between regular refreshes cannot be very accurate, and the sequential calling of each cache refreshing method is another cause of this inaccuracy.

(2)Because the internal implementation of the Manager adopts the loop engine, the minimum detection interval that can be set by the loop engine is1Seconds, so the cache Refresh Interval cannot be smaller1Seconds.

(3)If an exception is thrown when a cache is refreshedLastrefreshtimeThe attribute is the last refresh time of the record.

 

5.Extension

the cache manager regularly refreshes the notification of cache refresh failure by exposing events. When an exception occurs during cache refresh, we may need to record the exception to the log. If so, you can directly use esbasic provided refreshablecacheexceptionlogbridge .

RefreshablecacheexceptionlogbridgeWithEsbasic. Logger. iagileloggerComponent to record detailed information about the exception to the target log. Logs can be text files, databases, and other storage types.EsbasicProvidedIagileloggerInterface implementationFileagileloggerUsed to write logs into text files.

 

Note: esbasic source code can be downloaded from http://esbasic.codeplex.com/.
esbasic open source preface

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.