Using anonymous functions to make it easier to use caching

Source: Internet
Author: User

A data cache is generally completed by data sources, cache schemes, and cache reads. Now, with an anonymous function, you can change the data source to an anonymous function. In this way, you can directly declare a cache:

Protected   Static Cachemanager < Company > Companycache =   New Cachemanager < Company > ( New Smsdatabasecache < Company > (), (Key) =>
{
Return Smsdatabase. instance. Context. Company. Where (C => C. Serial number = ( String ) Key). singleordefault ();
});

Smsdatabasecache is a cache solution. You can also use simplecache, timeoutcache, and other classes to implement the icache interface. Because my icachemanager inherits from icache, it can also implement Hierarchical Cache connection. For example:

Smsdatebasecache is a cache Scheme Based on smsdatabase. instance. Context. Data is cleared during linqcontextdispose. Smsdatabasecache


Public   Class Smsdatabasecache < Tdata > : Icache < Tdata >
{
Private Dictionary < Object , Tdata > Cache =   New Dictionary < Object , Tdata > ();

Public Smsdatabasecache ()
{
Smsdatabase. instance. predisposing + = (S, E) =>
{
Cache. Clear ();
};
}

Public Tdata get ( Object Key)
{
Return Cache [Key];
}

Public   Void Set ( Object Key, tdata data)
{
Cache [Key] = Data;
}

}
Implementation Code As follows: Cache code

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> using system;
using system. collections. generic;
using system. text;
using system. LINQ;

NamespaceEvlon. utils
{
Public InterfaceIcache<Tdata>
{
Tdata get (ObjectKey );
VoidSet (ObjectKey, tdata data );
}

Public   Interface Icachemanager < Tdata > : Icache < Tdata >
{
Func < Object , Tdata > Sourcegetter { Get ;}
}

Public   Class Simplecache < Tdata > : Icache < Tdata >
{
Private Dictionary < Object , Tdata > Cache =   New Dictionary < Object , Tdata > ();

# RegionIcachemanager <tdata> Member

PublicTdata get (ObjectKey)
{
ReturnCache [Key];
}

Public VoidSet (ObjectKey, tdata data)
{
Cache [Key]=Data;
}

# Endregion
}

Public   Class Timeoutcache < Tdata > : Icache < Tdata >
{
Public Timespan expire { Get ; Set ;}
Private Datetime timelastscan = Datetime. now;
Private Dictionary < Object , Keyvaluepair < Tdata, datetime > Cache =   New Dictionary < Object , Keyvaluepair < Tdata, datetime > ();

PublicTimeoutcache ()
{
Expire=Timespan. fromminutes (5);
}

Private   Void Tryclearexpireobject ()
{
Timespan TS = Datetime. Now - Timelastscan;
If (TS. totalseconds > Expire. totalseconds /   2 )
{
Timelastscan = Datetime. now;
Cache. Where (Kv => KV. value. Value > Datetime. Now. Subtract (expire). tolist (). foreach (Kv =>
{
Cache. Remove (Kv. Key );
});
}
}

# RegionIcachemanager <tdata> Member

PublicTdata get (ObjectKey)
{
Tryclearexpireobject ();

If(!Cache. containskey (key ))
Return Default(Tdata );

Keyvaluepair < Tdata, datetime > KV = Cache [Key];
If (Kv. value. Add (expire) < Datetime. Now)
{
Cache. Remove (key );
Return   Default (Tdata );
}
Return KV. Key;
}

Public void set ( Object key, tdata data)
{< br> cache [Key] = New keyvaluepair tdata, datetime > (data, datetime. now);
}

# Endregion
}

Public   Class Cachemanager < Tdata > : Icachemanager < Tdata >
{
Private Icache < Tdata > Cache =   Null ;

Public Func < Object , Tdata > Sourcegetter { Get ; Private   Set ;}
Public Cachemanager (icache < Tdata > Cache, func < Object , Tdata > Sourcegetter)
{
This . Cache = Cache;
This . Sourcegetter = Sourcegetter;
}

# RegionIcachegetter <tdata> Member

Public Tdata get ( Object Key)
{
Tdata = Cache. Get (key );
If (Data ! =   Null )
Return Data;

Data= This. Sourcegetter (key );

Cache. Set (Key, data );

ReturnData;
}

Public VoidSet (ObjectKey, tdata data)
{
Cache. Set (Key, data );
}

# Endregion
}

}

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.