Monday, March 11, 2024

Understanding In-Memory Caching in .NET Core with IMemoryCache Interface

1. In-Memory Caching in .NET Core:
- In-Memory Caching is used to provide faster response to incoming requests by retrieving data from cache rather than the original source.
- Data Caching allows retrieval of data from cache as long as it doesn't expire.

2. Terms Related to Caching:
- Cache Hit refers to the requested data being in the cache, while Cache Miss refers to the data not being in the cache.
- In the case of Cache Miss, data is fetched from the data source and written back into the cache.

3. In-Memory Cache in .NET Core:
- In .NET Core, data can be written to cache, read, or deleted using the IMemoryCache interface in the Microsoft.Extensions.Caching.Memory library.
- Various options such as AbsoluteExpiration, ExpirationTokens, Priority, Size, and SlidingExpiration can be used to manage the cache.

4. Usage in a Project:
- Memory Cache is enabled in the ConfigureServices method in the startup.cs class by adding services.AddMemoryCache().
- The IMemoryCache interface is injected in the related controller to use 'In-Memory Cache'.

Convert String to Title case using Javascript

Here is the function to convert string to title case, which can handle spaces and underscores. Below function will remove underscores from the string.

// Import the function
function convertToTitleCase(input) {
  return input.toLowerCase().replace(/_/g, ' ').replace(/\b\w/g, function(match) {
    return match.toUpperCase();
  });
}

You can call the convertToTitleCase function in HTML by including a script tag with the function definition, and then using JavaScript to call the function and display the result.

Here's an example of how you can call the convertToTitleCase function in HTML:

<!DOCTYPE html>
<html>
<head>
  <title>Convert to Title Case</title>
</head>
<body>

<p id="output"></p>

<script>
// Function definition
function convertToTitleCase(input) {
  return input.toLowerCase().replace(/_/g, ' ').replace(/\b\w/g, function(match) {
    return match.toUpperCase();
  });
}

  // Call the function and display the result
  let input = "Nagasai_Srinivas_Mudara";
  let convertedString = convertToTitleCase(input);
  document.getElementById("output").innerHTML = convertedString;
</script>

</body>
</html>

In this JavaScript function, the replace method is used with a regular expression to match the underscores and lowercase letters and convert the lowercase letters to uppercase when preceded by an underscore or at the beginning of the string.

You can use the convertToTitleCase function to convert any input string to title case in a generic and reusable way.

Wednesday, March 06, 2024

How to implement retry logic for DB Transactions

In SQL Server, you can implement retry logic for transactions using T-SQL and error handling. Here's an example of how you can create a stored procedure that includes retry logic for handling deadlock errors:

CREATE PROCEDURE usp_RetryTransaction
AS
BEGIN
    DECLARE @retryCount INT = 0
    DECLARE @maxRetries INT = 3

    WHILE @retryCount < @maxRetries
    BEGIN
        BEGIN TRY
            BEGIN TRANSACTION
            -- Your transactional logic goes here
            COMMIT TRANSACTION
            RETURN
        END TRY
        BEGIN CATCH
            IF ERROR_NUMBER() = 1205  -- Deadlock error number
            BEGIN
                ROLLBACK TRANSACTION
                SET @retryCount = @retryCount + 1
                WAITFOR DELAY '00:00:01'  -- Wait for 1 second before retrying
            END
            ELSE
            BEGIN
                -- Handle other types of errors
                THROW
            END
        END CATCH
    END
    -- If the maximum number of retries is reached, handle the situation as needed
    -- For example, raise an error or log the issue
END
  

In this example, the stored procedure attempts the transaction logic within a retry loop, and if a deadlock error (error number 1205) occurs, it rolls back the transaction, increments the retry count, and waits for a short duration before retrying the transaction. If the maximum number of retries is reached, you can handle the situation as needed based on your application's requirements.

You can then call this stored procedure whenever you need to perform a transaction with retry logic for deadlock handling.

Tuesday, March 05, 2024

How to check if string exists in JQuery

In jQuery, you can use the indexOf method to check if a string contains another string. Here's an example:

var mainString = "Hello, world";
var subString = "world";

if (mainString.indexOf(subString) !== -1) {
    // subString is found in mainString
    console.log("Substring found");
} else {
    // subString is not found in mainString
    console.log("Substring not found");
}
  

In this example, the indexOf method returns the index of the first occurrence of the subString within the mainString. If the subString is not found, indexOf returns -1. You can use this to check if a string contains another string in jQuery.

Monday, March 04, 2024

What are Langchain Agents?

The LangChain framework is designed for building applications that utilize large language models (LLMs) to excel in natural language processing, text generation, and more. LangChain agents are specialized components within the framework designed to perform tasks such as answering questions, generating text, translating languages, and summarizing text. They harness the capabilities of LLMs to process natural language input and generate corresponding output.

High level Overview:
1. LangChain Agents: These are specialized components within the LangChain framework that interact with the real world and are designed to perform specific tasks such as answering questions, generating text, translating languages, and summarizing text.

2. Functioning of LangChain Agents: The LangChain agents use large language models (LLMs) to process natural language input and generate corresponding output, leveraging extensive training on vast datasets for various tasks such as comprehending queries, text generation, and language translation.

3. Architecture: The fundamental architecture of a LangChain agent involves input reception, processing with LLM, plan execution, and output delivery. It includes the agent itself, external tools, and toolkits assembled for specific functions.

4. Getting Started: Agents use a combination of an LLM or an LLM Chain as well as a Toolkit to perform a predefined series of steps to accomplish a goal. Tools like Wikipedia, DuckDuckGo, and Arxiv are utilized, and the necessary libraries and tools are imported and set up for the agent.

5. Advantages: LangChain agents are user-friendly, versatile, and offer enhanced capabilities by leveraging the power of language models. They hold potential for creating realistic chatbots, serving as educational tools, and aiding businesses in marketing.

6. Future Usage: LangChain agents could be employed in creating realistic chatbots, educational tools, and marketing assistance, indicating the potential for a more interactive and intelligent digital landscape.

Overall, LangChain agents offer user-friendly and versatile features, leveraging advanced language models to provide various applications across diverse scenarios and requirements.