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!