ASP. NET laziness Method 5 (using anonymity to simplify cache)

Source: Internet
Author: User

This is not what I think. It is what I think of Lao Zhao from the blog Park. It is very good. I just borrowed it. Is the original article good at using anonymous functions?

I just encapsulated the reuse into a class.

Public static class cachehelper
{
Public Delegate bool cachegetter <tdata> (Out tdata data );
Public Delegate tdata func <tdata> ();
Public Delegate void actions <tdata> (tdata data );
Public static tdata get <tdata> (cachegetter <tdata> cachegetter, func <tdata> sourcegetter, actions <tdata> cachesetter)
{
Tdata data;
If (cachegetter (out data ))
{
Return data;
}

Data = sourcegetter ();
Cachesetter (data );

Return data;
}

Public static tdata get <tdata> (string cachekey, func <tdata> cachegetter)
{
Tdata data;
Object objdata = frameworkcache. smcache. getcacheservice (). retrieveobject (cachekey );
Data = (objdata = NULL )? Default (tdata): (tdata) objdata;
If (objdata! = NULL)
{
Return data;
}
Else
{
Data = cachegetter ();
Frameworkcache. smcache. getcacheservice (). addobject (caccachekey, data );
Return data;
}
}

}
The first method is to release the cache-removing delegate, and the second method is to block the cache-adding method, because the cache method is the same in my project, but the key is different.

When calling:

String cachekey = "listcountofprovince ";
Return cachehelper. Get <ilist <Bisou. model. bs_area> (
Cachekey,

Delegate () // source Getter
{
/// Read data .....

Return dataset;
});

A lot of code is left!

Related Article

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.