concept cache in category c#

appears as: cache, cache, caches, The caches
Functional Programming in C#: How to write better C# code

This is an excerpt from Manning's book Functional Programming in C#: How to write better C# code.

For example, imagine a method that looks up an element from the cache. A delegate can be provided and can be invoked in case of a cache miss.

Listing 12.4. The program refactored to use Try for error handling
public class Program { public static void Main() => MainRec("Enter a currency pair like 'EURUSD', or 'q' to quit" , Rates.Empty); static void MainRec(string message, Rates cache) { WriteLine(message); var input = ReadLine().ToUpper(); if (input == "Q") return; GetRate(Yahoo.TryGetRate, input, cache) .Run().Match( ex => MainRec($"Error: {ex.Message}", cache), result => MainRec(result.Rate.ToString(), result.NewState)); } static Try<(decimal Rate, Rates NewState)> GetRate (Func<string, Try<decimal>> getRate, string ccyPair, Rates cache) { if (cache.ContainsKey(ccyPair)) return Try(() => (cache[ccyPair], cache)); else return from rate in getRate(ccyPair) select (rate, cache.Add(ccyPair, rate)); } }

12.1.1. Maintaining a cache of retrieved resources

We want to store rates in a cache when they’re retrieved, and only make HTTP requests for rates we haven’t seen before, as shown in figure 12.1.

Figure 12.1. Keeping a cache of previously retrieved rates
C# in Depth

This is an excerpt from Manning's book C# in Depth.

Here LogEntity is a partial method, and LoadAndCache loads an entity from the database and inserts it into the cache. You might want to use this instead:

The caches themselves are executable, which takes a while to understand. The C# compiler generates code to simply execute the call site’s L0 cache (which is a delegate accessed through the Target property). That’s it! The L0 cache has a single rule, which it checks when it’s called. If the rule matches, it executes the associated behavior. If the rule doesn’t match (or if this is the first call, so it doesn’t have even one rule), it calls into the L1 cache, which in turn calls into the L2 cache. If the L2 cache can’t find any matching rules, it asks the receiver or the binder to resolve the call. The results are then put into the cache for next time.

ASP.NET 4.0 in Practice

This is an excerpt from Manning's book ASP.NET 4.0 in Practice.

HttpModules are responsible for the majority of the features in ASP.NET and provide great flexibility when you have to add functionalities to an application. They work as filters for both the request and the response, and they register themselves for Http-Application events. Using HttpModules, ASP.NET offers mechanisms like authentication, authorization, session state, cache, and many others. You can write your own modules to modify the default behaviors and give yourself the flexibility you need.

When the page is requested for the first time (or, in general, when a cached version doesn’t exist), the page itself is executed normally. The OuputCacheModule, which is the HttpModule implementing this behavior, looks for the page to be cached and saves its results in the cache. Generally, the cache is in memory and works with a timeout-based expiration, but you can write a custom provider (in ASP.NET 4.0) and manage different dependencies (this feature already existed in previous versions of ASP.NET). On subsequent requests, the page class isn’t instantiated at all, but the saved markup is instead sent directly.

Figure 14.10. When an item is added to the cache, AppFabric caching automatically balances it across the cluster’s nodes. When retrieved, the object can be sent from any server in the cluster.