The caching Technology of C # functional programming

Source: Internet
Author: User

Caching technology

This section will be divided into two parts, the first part is precomputed , the second part is the cache . Caching this technology for the development of the people are very familiar with, from the page cache to the database cache everywhere, and its most important feature is the first query after the data cache, in the subsequent query process without recalculation and directly from memory to return the results, greatly improve performance, And our cache here is focused on the function.

Expected count

Some people may not immediately understand the meaning of the word, so we simply introduce from the example of life. A lot of people in the work will certainly do such things, such as the boss told you one thing, but the second part of the matter to wait for another colleague to do after the corresponding material to you to complete. But we can't wait until that colleague finishes handing over the material to us before we do it. Instead, the first half of the matter is done, so that the rest of the work is done and handed over to us, and we can just finish the second part. This kind of thinking can also be used in software development, such as the following function:

Inside the function is the use of the value of a to calculate C, and then add C and B to the final result. Of course, this example does not fully reflect the characteristics of the estimate, but it allows us to understand what is expected to be achieved. Suppose this is the

is a time-consuming operation, and in actual use will appear the value of a does not change, but the value of B will change frequently, but each call to this function will be re-calculated according to a C, then we need some way to change this pattern, here we can first try to use some of the application (here is a functional development of the library- >portablefcslib, can be installed in NuGet or downloaded to his GitHub website: https://github.com/rmoritz/PortableFCSLib):

Our intention here is to calculate the value of C only once, not two times, and then we look at the final output:

or calculated two times C, the reason is very simple, because some applications just use the closure to save parameters, only after all parameters are passed to call this function, and do not achieve the effect of pre-calculation, so we need a new way to complete, below we modify dosomething function:

Here we can see that the parameter is only a, and the return value becomes a function, so that the first time this method will be calculated after the C value, because of the closure of the value of C will be saved. Let's take a look at how to invoke:

At the end of the day, we'll see if the value of C is calculated only once:

So we are done, of course, I would like to mention here, these examples are just for the reader to quickly understand, in the actual use of the reader can be flexible according to the situation, for example, the function receives three parameters, but the first two do not change frequently, but the third one is often changed, And the inner part of the function is based on the first two parameters to calculate a result, and the return value needs to be based on the third parameter and this calculated after the merit, then we can return but one parameter of the function, and the function itself needs to receive two.

Cache

before using this technique we need to understand a few nouns, that is, to reference transparency and function purity . Both of these mean that when we call a function, the returned result should be consistent at any time, as long as the arguments passed are consistent. Such a function would allow us to take advantage of the cache. First we define a function, and this function will be the function we need to cache later:

Then we modify the function so that it can be cached:

Here we can see that we have used a dictionary to cache this function. function first from the dictionary to determine whether there is a key of parameter A, if there is a direct return of the computed results, if not exist then calculate the result, and save to the field, so that we implement a simple cache. Let's take a look at the final result, whether the cache is actually used:

When the parameter 10 is passed, because the cache does not have so cache, parameter 5 is the same, and the next line to calculate the 10 is not calculated, but directly from the dictionary to return the corresponding results. However, there is a problem with the above approach, if there are multiple functions need to cache, then this class will have more than one field type of field (some people may ask why can't share a dictionary, so you have to take the name of key to spend a certain amount of effort, and easily cause duplication), Then we need a way to cache without polluting the class, here we first describe how to implement the internal cache using the memoizer in fcslib :

Here we can see that Memoizer exposes a getmemory static way to get the corresponding cache object, and then we can cache it using the returned object, the final effect is the same as before, We can look at the results of the final console output:

If you don't know what the name of this function is, we can use reflection to get the full name of the function, such as the following modified dosomething function that uses this method:

Of course, in addition to the manual modification of the function, we can also use automation to do not use the cache function cache technology, let us write a function to achieve this function:

We can see the red box, in fact, the use of closures, in this function and nested a layer of functions, so that we can be cached, only in the cache does not exist when the function evaluation, but in the face of multiple parameters, these can not be normal cache. Then we need to use a deep cache, and the so-called deep cache is to use dictionary set dictionary to save, such as the following function, need to pass two parameters, then the corresponding cache is:

Then there is the invocation in main:

Finally we can see this result, the first call to Sfunc (10,5) when the cache is established, and then the second pass the same parameter call can see the console does not output the corresponding string:

Of course, the nesting of dictionaries in the case of many parameters, it will appear very complex, and will also consume a lot of memory. However, there is no very good solution at the moment, we can also use the previously written cache function to achieve the above-mentioned multiple parameters of caching:

The focus is our red box, the specific nesting is the first dictionary key is the first parameter, value is a reference to the next function, of course, the function is after the cache wrapper, then naturally after the function of the call value has also played a role in caching.

The caching Technology of C # functional programming

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.