Based on the. NET Core MicroServices framework-Talk cache middleware and cache demotion

Source: Internet
Author: User
Tags delete cache intel core i7

1 , preface

Surging by a lot of. NET classmate's favor, also raised a lot of questions, mention the most is when Integration API gateway, here to answer everyone has recently started to research and development, should be in 1, 2 months will have a first edition API Gateway, other like token authentication, Function completion time for current-limit demotion will be pushed backwards

A new version of surging has also recently been updated

Update content:

1. The cache middleware has turned Servicestack.redis into Stackexchange based on a third-party library on which Redis relies
2. Increase Cache demotion
3. Increase the interception cache demotion example

Open Source Address: https://github.com/dotnetcore/surging

2 ,Use of the cache middleware

On the structure thought of stripping surging the cache middleware is roughly mentioned in this article, and we have a preliminary impression of the cache middleware , which we'll talk about using the cache middleware

1. Create a configuration file

Cachesettings.json

{"Cachingsettings": [{"Id": "Ddlcache", "Class": "Surging.core.caching.rediscache.rediscontext,s Urging.          Core.caching "," Properties ": [{" Name ":" Apprulefile "," Ref ":" Rule "},                {"Name": "Datacontextpool", "Ref": "Ddls_sample", "Maps": [{ "Name": "Redis", "Properties": [{"Value": "127.0.0.1:6379::1            "}]}, {" Name ":" MemoryCache "}          ]}, {"Name": "Defaultexpiretime", "value": "120"},            {"Name": "ConnectTimeout", "Value": "+"}, {"Name": "MinSize",      "Value": "1"}, {"Name": "MaxSize", "Value": "10"}] }    ]}

Multiple instance configurations can be supported to identify unique by configuration ID, and by configuring maps to initialize the list of service nodes, select the service node through hash consistency

Configuration parameter list

Parameters function

Cachingsettings

Parent configuration section with multiple instances

Id

Uniquely identifies

Class

For context adaptation
Properties Cache Related configuration section

Maps

Configure the Service section list

MinSize

Minimum number of object pools

MaxSize

Objectpool Maximum number

2. Code Configuration

        /// <summary>        /// Configure the Cache        service /// </summary>         Public Static void Configurecache (iconfigurationbuilder build)        {            build              . Addcachefile ("cachesettings.json"false);        }   

Above we have completed the cache middleware configuration, next can be called by the following code

Redis-based cache invocation

  Cacheprovider = cachecontainer.getinstances<icacheprovider> ("Ddlcache.redis");

MemoryCache-based cache invocation

Cachecontainer.getinstances<icacheprovider> ("memorycache")
3 ,Cache demotion

For the data returned by the service, some of them do not need to be modified frequently, can be returned by the cache data, such as product application, commodity classification, product list, user information, order information, shopping cart, etc., through the above requirements we will be blocking the proxy, back to cache or delete the cache.

The following questions are available for caching

    1. Data consistency: For services that are distributed across servers, cache storage reads should also support distributed, so you should choose to support a distributed cache framework such as: Redis,couchbase,membercache
    2. Cache Hit Ratio : In the absence of a hit, it is not possible to get the desired data directly from the cache, but to fetch the data directly by invoking the remote service, which increases response time and affects throughput, so we must reference the tool for monitoring

Cache demotion

Surging cache demotion is for proxy interception, if there is cached data, then return the cached data, otherwise call the proceed method calls the remote service.

When the data is update,delete, it needs to be deleted for the dependent cache, and the cache can be deleted by proxy interception.

Cache demotion has the following advantages

1. High performance: Can reduce response time and improve throughput

2. Efficient: Design for business logic, no need to consider the cache, to achieve seamless integration

The following example shows how to use the

Add the following attribute on the business interface method

[Command (strategy= strategytype.failover,failovercluster =3, requestcacheenabled =true)]  // requestcacheenabled =true is enabling caching

Intercept Get Cache

" getuser_id_{0} " 480)]

Block Delete Cache

" getuser_id_{0} " " getusername_name_{0} ", Mode = Cachetargettype.redis)]

Apply [CacheKey] to identify the cache key, as

     Public class Usermodel    {        [CacheKey (1)]        publicintgetset;}         Publicstringgetset;}          Public int Get Set ; }    }

Configuring interceptors

. Addclientintercepted (typeof(Cacheproviderinterceptor))
4. Testing

Test environment

Cpu:intel Core I7-4710MQ

Memory: 16G

HDD: 1T ssd+512g HDD

Network: LAN

Turn on Redis test results as follows: Deactivate Redis test results as follows: 5. Summary

API gateways have been developed, and recent updates will only fix bugs and submit unit tests. If interested please pay more attention or join QQ Group: 615562965

Based on the. NET Core MicroServices framework-Talk cache middleware and cache demotion

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.