CYQ. Data V5 Distributed Automatic Cache Design (2), cyq. datav5

Source: Internet
Author: User

CYQ. Data V5 Distributed Automatic Cache Design (2), cyq. datav5
Preface:

Recently, I started an IT connection business, so most of my energy and writing articles are sharing my entrepreneurial experience.

Few articles about my three frameworks CYQ. Data, Aries, And Taurus. MVC have been written.

However, the maintenance and upgrade of the framework is still in progress from time to time. This can be seen from the code submission time on the open-source Github.

After all, the background WebAPI of IT connection uses Taurus. MVC and Aries for background system management.

But today, I will not write any articles related to entrepreneurship. I will share some technical articles first.

CYQ. Data Distributed Automatic Cache

I have written an article about CYQ. Data V5 Distributed Automatic cache design.

In the previous article, the basic information and ideas have been clearly described. Here we will add two points:

1: Automatic cache is enabled by default. The related control and configuration are as follows:

When global shutdown is required:

Code can be used at the entry point when the application is started:

AppConfig.Cache.IsAutoCache=false;

Or app. config or web. config Configuration:

<appSettings>    <add key="IsAutoCache" value="false"/></appSettings>

When code is partially disabled:

Using (MAction action = new MAction ("table name") {action. SetAopState (CYQ. Data. Aop. AopOp. OnlyOuter); // disable automatic caching and retain Aop}

When using code control to clear table cache:

String key = CacheManage. GetKey (CacheKeyType. AutoCache, "table name"); CacheManage. Instance. Remove (key );

Other control configuration items:

/// <Summary> /// when AutoCache is enabled, you can set a Table that is not cached, multiple static string NoCacheTables {get {return GetApp ("NoCacheTables", "");} set {SetApp ("NoCacheTables ", value); CYQ. data. cache. autoCache. noCacheTables = null ;}/// <summary> // when AutoCache is enabled, you can set a column name that is not affected by the update, in Json format. /// {TalbeName1: 'column1, column2 ', talbeName2: 'column1, column2 '} // </summary> public static string IngoreCacheColumns {get {return GetApp ("IngoreCacheColumns", "");} set {SetApp ("IngoreCacheColumns", value ); CYQ. data. cache. autoCache. ingoreCacheColumns = null ;}}
2: Cache Control for distributed applications

For a single application, the framework has been well processed.

For multiple applications, the Framework provides a solution to enable distributed cache or Redis through configuration.

The related configuration is as follows (all AppConfig can correspond to the ettings item under web. config ):

/// <Summary> /// MemCache distributed cache server configuration. multiple servers are configured with commas (,) separate /// </summary> public static string MemCacheServers {get {return GetApp ("MemCacheServers", string. empty) ;}set {SetApp ("MemCacheServers", value) ;}/// <summary> /// MemCache Backup Server (when the master server is down, the request is redirected to the backup server. // </summary> public static string MemCacheServersBak {get {return GetApp ("MemCacheServersBak", string. empty);} set {SetApp ("MemCacheServersBak", value) ;}/// <summary> // configure the Redis distributed cache server. Use commas (,) separate /// </summary> public static string RedisServers {get {return GetApp ("RedisServers", string. empty);} set {SetApp ("RedisServers", value) ;}/// <summary> // number of databases used by Redis (1 by default, db0) /// </summary> public static int RedisUseDBCount {get {return GetAppInt ("RedisUseDBCount", 1);} set {SetApp ("RedisUseDBCount", value. toString () ;}/// <summary> // the DB index used by Redis (0 by default. If configured, RedisUseDBCount is ignored) /// </summary> public static int RedisUseDBIndex {get {return GetAppInt ("RedisUseDBIndex", 0);} set {SetApp ("RedisUseDBIndex", value. toString () ;}/// <summary> // Redis Backup Server (when the master server is down, the request is redirected to the backup server) /// </summary> public static string redisserversbk {get {return GetApp ("redisserversbk", string. empty);} set {SetApp ("redisserversbk", value );}}
Upgrade of the cache invalidation policy in distributed mode

Distributed cache can be centrally controlled in multiple applications.

However, for a single server, I began to think about whether there is a simpler solution. I can choose not to install MemCache or Redis.

For example, the webapi and aries backend of the IT connection startup project are two sets of programs. The same server operates a database and each has an automatic cache.

After thinking, we finally chose to use the regular scan table for processing.

Solution Principle:

For each application, you only need to configure the Database Link (the configured database can be any ):

/// <Summary> /// CYQ. data. cache automatic Cache-Database Link Configuration // when you operate the same database in multiple different application projects (you do not want to use the distributed Cache MemCache or Redis), you can enable this option, achieve Smart cache cleanup. /// </Summary> public static string AutoCacheConn {get {if (_ AutoCacheConn = null) {_ AutoCacheConn = AppConfig. getConn ("AutoCacheConn");} return _ AutoCacheConn;} set {_ AutoCacheConn = value ;}}

Then, the Framework automatically creates a SysAutoCache table, which contains the CacheKey and CacheTime columns:

Then the framework will have a scheduled scan to handle:

Public static void AutoCacheKeyTask (object threadID) {while (true) // periodically scans the database {int time = AppConfig. cache. autoCacheTaskTime; if (time <= 0) {time = 1000;} Thread. sleep (time); if (removeListForKeyTask. count> 0) {string baseKey = removeListForKeyTask. dequeue (); if (! String. IsNullOrEmpty (baseKey) {KeyTable. SetKey (baseKey) ;}} if (KeyTable. HasAutoCacheTable) // read to see if there is any key to be removed. {KeyTable. ReadAndRemoveKey ();}}}

The default scan interval is 1 second. You can configure the scan interval by yourself:

/// <Summary> /// when AutoCacheConn is enabled, the task time of regularly scanning the database (in milliseconds) is ), default 1000 // </summary> public static int AutoCacheTaskTime {get {return GetAppInt ("AutoCacheTaskTime", 1000);} set {SetApp ("AutoCacheTaskTime", value. toString ());}}
Summary:

There are two benefits for this upgrade:

1: It is easier to use multiple applications. An alternative solution is provided.

2: If you prefer to manually modify the database data or the Stored Procedure Code that the framework cannot monitor, you can also update the SysAutoCache table time manually or during the stored procedure to notify the framework to update the cache.

Overall:

1: Automatic cache can improve the quality of your applications.

2: in some large projects, you should use more sophisticated functions. Configure tables that do not need to be cached, and update columns that do not affect caching.

3: Please save that the system has enough cache.

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.