Monday, December 25, 2023

IMemoryCache in .NET Core with Example

IMemoryCache in .NET Core

In .NET Core, managing caching efficiently can significantly enhance the performance of applications. The IMemoryCache interface plays a pivotal role in caching data in memory, providing a simple and effective way to store and retrieve cached data within your applications. This post aims to provide an in-depth understanding of IMemoryCache and its implementation with illustrative examples.

What is IMemoryCache?

IMemoryCache is an interface provided by the .NET Core framework, designed to cache data in memory within an application. It enables developers to temporarily store frequently accessed data, reducing the need to fetch it from the original source repeatedly.

Monday, December 18, 2023

What is the difference between IMemoryCache and IDistributedCache?

IMemoryCache and IDistributedCache are both interfaces in ASP.NET Core used for caching data, but they differ in terms of scope and storage.

IMemoryCache

  • Scope: Local to the application instance.
  • Storage: Caches data in the memory of the local application.
  • Usage: Ideal for scenarios where data needs to be cached within the same application instance and doesn't need to be shared across multiple instances or servers.
  • Pros: Faster access since it operates within the application's memory.
  • Cons: Limited to a single instance and doesn't support sharing data between different instances or servers.

Sunday, December 17, 2023

What is CacheEntryOptions in .NET Core

CacheEntryOptions in .NET Core

Caching plays a crucial role in enhancing the performance and scalability of applications. In .NET Core, MemoryCache class enables storing frequently accessed data in memory, facilitating quick retrieval. To tailor cached item behavior, developers can utilize CacheEntryOptions. This post delves into CacheEntryOptions and its role in customizing caching behavior in .NET core applications.



What are CacheEntryOptions?

CacheEntryOptions, found in the Microsoft.Extensions.Caching.Memory namespace, empowers developers to configure various settings related to cached items in MemoryCache. These options allow control over properties such as expiration time, priority, and post-eviction callbacks for cached items.

Key Properties of CacheEntryOptions

  1. AbsoluteExpiration and AbsoluteExpirationRelativeToNow:These properties allow specifying when a cached item should expire, either at an absolute time or after a certain duration from its addition to the cache.
  2. SlidingExpiration:SlidingExpiration enables defining a time window after which the cached item expires if not accessed. Each access to the item resets the sliding window.
  3. Priority:CacheEntryOptions lets you set the priority of cached items, affecting their likelihood of being removed from the cache upon expiration or when the cache needs space for new items.

Monday, December 11, 2023

Prototype Design Pattern in C#

What is Prototype Design Pattern?

The prototype design pattern is a creational design pattern that allows creating new objects by cloning an existing object. This pattern is useful when the creation of an object is costly or complex, and we want to avoid repeating the same process for each new instance. By using the prototype pattern, we can create new objects by copying the properties and behaviors of an existing object, and then modifying them as needed.

One of the benefits of the prototype pattern is that it reduces the dependency on subclasses and factory methods. Instead of creating objects using specific constructors or factory methods, we can use a generic prototype object that can be cloned and customized. This makes the code more flexible and extensible, as we can add new types of objects without changing the existing code.

Components of Prototype Pattern

  • Prototype: This will be an interface or abstract class used for the types of objects that can be cloned. In our example, it is going to be the Employee Abstract Class.
  • ConcretePrototype: This class will implement the Prototype abstract class or interface for cloning. In our example, it will be the PermanetEmployee and TemporaryEmployee Classes.
  • Client: The client is the class that creates a new object by asking a prototype to clone itself.

Monday, December 4, 2023

Builder Design Pattern in C#

What is the Builder Design Pattern?

The Builder Design Pattern is a creational design pattern that helps in constructing complex objects step by step. It separates the construction of an object from its representation, allowing the same construction process to create different representations. In C#, the Builder pattern is widely used to create objects with varying configurations while keeping the construction process unchanged.

The key idea is to separate the construction of a complex object from its representation, allowing the same construction process to create different representations.

So, the Builder Design Pattern is all about separating the construction process from its representation. When the construction process of your object is very complex, only you need to use the Builder Design Pattern.

COmponents of Builder Design Pattern

  • Abstract Builder: The Builder is an interface defining all the steps to make the concrete product.
  • Concrete Builder: The Concrete Builder Classes implements the Abstract Builder interface and provides implementation to all the abstract methods. The Concrete Builder is responsible for constructing and assembling the individual parts of the product by implementing the Builder interface. It also defines and tracks the representation it creates.
  • Director: The Director takes those individual processes from the Builder and defines the sequence to build the product.
  • Product: The Product is a class, and we want to create this product object using the builder design pattern. This class defines different parts that will make the product.
^ Scroll to Top