Datarabbit lightweight ORM framework (16) -- entity Cache

Source: Internet
Author: User
In many project applications, the frequency of data changes in some tables in the database we designed is very slow. For example, we have a gameinformation table that stores information about all online games, the data changes in this table are very frequent (because a new game may be launched in a month or two or occasionally modify the details of the online game (usually without urgent updates )), in addition, data in this table is often used, such as getting the game name and introduction based on the gameid. This type of table is suitable for caching in the memory, which can provide better performance and effectively reduce the load of the database.

The datarabbit. application. cache namespace supports entity caching and regularly refreshes the cache (for example, refreshing the cache every 15 minutes ).
First, in the Framework, entity cache is a common cache. Let's take a look at the definition of the cache (esbasic. cache. icache Interface) by the framework: public interface icache
{
Bool enabled {Get; set ;}

Void initialize ();

Void refresh ();

Event cbsimple cacherefreshed;
}

Second, the esbasic. cache namespace has an icachemanager which is used to manage all cache instances and regularly refreshes each cache. Its interface is defined as follows: public interface icachemanager
{
/// <Summary>
/// Refreshspaninsecs
/// </Summary>
Int refreshspaninsecs {set ;}

Ilist <icache> cachelist {set ;}

Void initialize ();
Void refreshnow ();
Void addcache (icache cache );
Void removecache (icache cache );
Void removecaches ();

Event cbcacheexception cacherefreshfailed;
}

Esbasic. cache. cachemanager class implements this interface.

Now, with the support of the basic cache interface and cache manager, we can implement entity cache on its basis ).
First, to cache an entity, you must implement the icachedentity generic interface: // <summary>
/// The icachedentity that can be cached
/// </Summary>
/// <Typeparam name = "tpkey"> Primary Key type </typeparam>
Public interface icachedentity <tpkey>
{
Tpkey GETID ();
}

What operations can the object cache provide? Simply think about it, we can know, for example, getting all the cached entity, getting entity based on ID, and so on. The ientitycache interface defines these features: // <summary>
/// Ientitycache obtains and caches qualified entity from the database. 2007.07.07
/// </Summary>
/// <Typeparam name = "tpkey"> Primary Key type of the corresponding data table </typeparam>
/// <Typeparam name = "tentity"> entity type </typeparam>
Public interface ientitycache <tpkey, tentity>: icache where tentity: Class, icachedentity <tpkey>
{
/// <Summary>
/// Getentity gets the target entity. If the cache does not exist, refresh the cache. If no action is received, null is returned.
/// </Summary>
Tentity getentity (tpkey entityid );
Ilist <tentity> getentitylist ();

Transactionscopefactory {set ;}
Cacherefreshmode {set ;}
}

/// <Summary>
/// Cacherefreshmode cache refresh mode: replace all and increment.
/// </Summary>
Public Enum cacherefreshmode
{
Replaceall,
Increasement
}

Datarabbit. application. cache. the entitycache class implements this interface and provides the virtual createfiltertree method to allow the derived class to provide a series of conditions. Only the entity that meets these conditions can be obtained from the database for caching. The createfiltertree method is defined as follows:

# Region createfiltertree
/// <Summary>
/// Setfiltertree: This method can be used to set conditions for objects to be cached. the return value is null, indicating all entity values in the cache table.
/// </Summary>
Protected virtual ifiltertree createfiltertree ()
{
Return NULL;
}
# Endregion

The following is an example to describe the application of the entity cache.
For example, I Need To Cache all enabled gameinformation.
First, let the gameinformation object class inherit from icachedentity: public partial class gameinformation: icachedentity <string>
{
//
}

The primary key ID of the gameinformation table is a string type.
Next, inherit from datarabbit. application. cache. entitycache to obtain the specific cache class gameinfocache: public class gameinfocache: entitycache <string, gameinformation>
{
Protected override ifiltertree createfiltertree ()
{
// Retrieve all enabled Game Information
Return new simplefiltertree (logictype. And, new filter (gameinformation. _ enabled, true ));
}
}

Finally, we can configure the cache manager and entity cache through spring.net. Suppose we refresh the cache manager every 15 minutes (900 s. <Object name = "gameinfocache" type = "gamerabbit. entity. cache. gameinfocache, gamerabbit. Entity" init-method = "initialize">
<Property name = "enabled" value = "true"/>
<Property name = "transactionscopefactory" ref = "transactionscopefactory"/>
<Property name = "cacherefreshmode" value = "replaceall"/>
</Object>

<Object name = "cachemanager" type = "esbasic. cache. cachemanager, esbasic" init-method = "initialize">
<Property name = "refreshspaninsecs" value = "900"/>
<Property name = "cachelist">
<List element-type = "esbasic. cache. icache, esbasic">
<Ref object = "gameinfocache"/>
</List>
</Property>
</Object>

In this case. The above introduction not only uses the classes in datarabbit, but also uses related esbasic components. Don't worry, I have a lightweight ORM framework in datarabbit (00) -- the download of datarabbit3.0 provided at the end of the preface contains the necessary esbasic dynamic library. You can download it from there to try entitycache convenience.

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.