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
}
}