<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mravashna on Medium]]></title>
        <description><![CDATA[Stories by Mravashna on Medium]]></description>
        <link>https://medium.com/@mravashna?source=rss-f9b4ba70f9e4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Mravashna on Medium</title>
            <link>https://medium.com/@mravashna?source=rss-f9b4ba70f9e4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 10:11:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mravashna/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Boosting Performance with Caffeine Cache Manager in Spring Boot]]></title>
            <link>https://medium.com/@mravashna/boosting-performance-with-caffeine-cache-manager-in-spring-boot-f99e80d952af?source=rss-f9b4ba70f9e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/f99e80d952af</guid>
            <dc:creator><![CDATA[Mravashna]]></dc:creator>
            <pubDate>Mon, 10 Jun 2024 05:16:00 GMT</pubDate>
            <atom:updated>2024-06-10T05:16:00.694Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Introduction:</strong></p><p>Ever feel like your app could use a little boost, like an extra shot of espresso? That’s where a cache manager comes in. In this blog, we’ll explore what Caffeine Cache Manager is, and why it might just be the secret ingredient you need to supercharge your Spring Boot projects</p><p>Caffeine Cache Manager, developed by Ben Manes, is a high-performance caching library for Java applications. It’s meticulously designed to store and manage frequently accessed data in memory, effectively minimizing the necessity for repetitive database queries or resource-intensive computations. Picture it as the barista for your data, ensuring that the most essential information is readily available to serve at a moment’s notice.</p><p><strong>Key Features:</strong></p><ul><li><strong>High Throughput and Low Latency:</strong> Caffeine is optimized for speed, providing quick access to cached data with minimal delays.</li><li><strong>Advanced Eviction Policies:</strong> Employs sophisticated algorithms to manage which data stays in the cache and which gets evicted.</li><li><strong>Concurrency Support:</strong> Designed for multi-threaded environments, minimizing contention.</li><li><strong>Flexible Expiration Policies:</strong> Allows you to set custom expiration times for cached data.</li><li><strong>Seamless Integration:</strong> Easily integrates with Spring Boot, making it simple to add caching to your application.</li></ul><p><strong>Why Choose Caffeine for Cache Management?</strong></p><p><strong>1. Performance Improvement:</strong> By caching frequently accessed data, Caffeine significantly reduces access times, leading to faster response times for your application. Users will notice the speed boost, much like switching from decaf to espresso.</p><p><strong>2. Reduced Load on Data Sources:</strong> Caching helps offload frequent read operations from your database or external services, reducing the overall load and potentially lowering costs.</p><p><strong>3. Scalability:</strong> With efficient caching strategies, your application can handle higher loads without degrading performance, making it more scalable and responsive.</p><p><strong>Caffeine’s Algorithms: The Secret Sauce</strong></p><p><strong>Window TinyLFU:</strong></p><ul><li>Combines the best of LRU (Least Recently Used) and LFU (Least Frequently Used) to make eviction decisions. It uses a small sample window to estimate the frequency of access, ensuring that frequently accessed items stay in the cache.</li></ul><p><strong>Adaptive Replacement Cache (ARC):</strong></p><ul><li>Balances between recently used and frequently used data, ensuring optimal cache performance by adapting to the workload.</li></ul><p><strong>Clock-Pro:</strong></p><ul><li>A variant of the clock algorithm used in operating systems, designed for high-speed cache eviction decisions.</li></ul><p><strong>How to Integrate Caffeine in Spring Boot Application?</strong></p><p>Step 1: Add Caffeine Dependency</p><p>Add the Caffeine dependency to your build.gradle</p><pre>implementation &#39;com.github.ben-manes.caffeine:caffeine:3.0.4&#39;</pre><p>of for Maven</p><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;com.github.ben-manes.caffeine&lt;/groupId&gt;<br>    &lt;artifactId&gt;caffeine&lt;/artifactId&gt;<br>    &lt;version&gt;3.0.4&lt;/version&gt;<br>&lt;/dependency&gt;</pre><p><strong>Step 2: Configure Caffeine Cache Manager</strong></p><p>Create a configuration class to set up the Caffeine Cache Manager:</p><pre>import org.springframework.cache.annotation.EnableCaching;<br>import org.springframework.cache.caffeine.CaffeineCacheManager;<br>import org.springframework.context.annotation.Bean;<br>import org.springframework.context.annotation.Configuration;<br>import com.github.benmanes.caffeine.cache.Caffeine;<br>import java.util.concurrent.TimeUnit;<br><br>@Configuration<br>@EnableCaching<br>public class CacheConfig {<br><br>    @Bean<br>    public CaffeineCacheManager cacheManager() {<br>        CaffeineCacheManager cacheManager = new CaffeineCacheManager(&quot;myCache&quot;);<br>        cacheManager.setCaffeine(caffeineCacheBuilder());<br>        return cacheManager;<br>    }<br><br>    Caffeine&lt;Object, Object&gt; caffeineCacheBuilder() {<br>        return Caffeine.newBuilder()<br>                .expireAfterWrite(10, TimeUnit.MINUTES)<br>                .maximumSize(100)<br>                .recordStats();<br>    }<br>}</pre><p><strong>Step 3: Use Caffeine Cache with Spring Annotations</strong></p><p>Annotate your service methods to use caching:</p><pre>import org.springframework.cache.annotation.Cacheable;<br>import org.springframework.stereotype.Service;<br><br>@Service<br>public class MyService {<br><br>    @Cacheable(value = &quot;myCache&quot;)<br>    public String getData(String key) {<br>        // Simulate a slow service call<br>        try {<br>            Thread.sleep(3000);<br>        } catch (InterruptedException e) {<br>            e.printStackTrace();<br>        }<br>        return &quot;Data for &quot; + key;<br>    }<br>}</pre><p>In this example, the getData method’s result will be cached, reducing the need to recompute it on subsequent calls with the same argument.</p><h3>Monitoring Cache Performance</h3><p>Enable statistics recording in the cache configuration:</p><pre>Caffeine&lt;Object, Object&gt; caffeineCacheBuilder() {<br>    return Caffeine.newBuilder()<br>            .expireAfterWrite(10, TimeUnit.MINUTES)<br>            .maximumSize(100)<br>            .recordStats();<br>}</pre><p>Access and print cache statistics:</p><pre>import com.github.benmanes.caffeine.cache.Cache;<br>import com.github.benmanes.caffeine.cache.stats.CacheStats;<br>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.cache.caffeine.CaffeineCacheManager;<br>import org.springframework.stereotype.Service;<br><br>@Service<br>public class CacheStatisticsService {<br><br>    @Autowired<br>    private CaffeineCacheManager cacheManager;<br><br>    public void printCacheStats() {<br>        Cache&lt;Object, Object&gt; cache = cacheManager.getCache(&quot;myCache&quot;).getNativeCache();<br>        CacheStats stats = cache.stats();<br>        System.out.println(&quot;Cache Hit Rate: &quot; + stats.hitRate());<br>        System.out.println(&quot;Cache Eviction Count: &quot; + stats.evictionCount());<br>    }<br>}</pre><h3>Conclusion</h3><p>Caffeine Cache Manager excels in performance, concurrency support, and advanced eviction policies, making it as a choice for high-performance in-memory caching in Java applications. Its seamless integration with Spring Framework and flexible configuration options make it easy to use and highly effective.</p><p>Wrapping up with a playful nudge: So, as you embark on your caching adventures with Caffeine, remember to keep your cups full and your code even fuller! <strong>Happy caching! 🚀</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f99e80d952af" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Thymeleaf Tricks: Turning Boring Templates into Dynamic Delights!]]></title>
            <link>https://medium.com/@mravashna/thymeleaf-tricks-turning-boring-templates-into-dynamic-delights-7ed941988a28?source=rss-f9b4ba70f9e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ed941988a28</guid>
            <dc:creator><![CDATA[Mravashna]]></dc:creator>
            <pubDate>Sun, 09 Jun 2024 08:12:02 GMT</pubDate>
            <atom:updated>2024-06-09T08:12:02.803Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Introduction:</strong></p><p>Thymeleaf is a versatile Java template engine that excels in creating dynamic web applications.It possesses the capability to handle various formats such as HTML, XML, JavaScript, CSS, and plain text, offering a versatile solution for dynamic content generation. One of its powerful features is the ability to dynamically insert content and reuse fragments. This capability allows developers to create highly modular and maintainable templates. In this blog post, will delve into concepts of dynamic content and fragment insertion in Thymeleaf.</p><p><strong>Dynamic Content with </strong><strong>th:replace and </strong><strong>th:insert:</strong></p><p>Thymeleaf allows you to define fragments — reusable pieces of templates that can be included in other templates. The th:replace and th:insert attributes are particularly useful for this purpose.</p><ul><li><strong>th:replace:</strong> This attribute replaces the element it is applied to with the content of the specified fragment.</li><li><strong>th:insert:</strong> This attribute inserts the content of the specified fragment inside the element it is applied to.</li></ul><p><strong>Example Usage:</strong></p><p><strong>1. Define Fragments:</strong> Create a Thymeleaf fragment file named fragments.html to hold reusable components.</p><pre>&lt;!-- fragments.html --&gt;<br>&lt;!DOCTYPE html&gt;<br>&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;<br>&lt;body&gt;<br>    &lt;div th:fragment=&quot;header&quot;&gt;<br>        &lt;header&gt;<br>            &lt;h1&gt;Welcome to My Website&lt;/h1&gt;<br>        &lt;/header&gt;<br>    &lt;/div&gt;<br>    &lt;div th:fragment=&quot;footer&quot;&gt;<br>        &lt;footer&gt;<br>            &lt;p&gt;&amp;copy; 2024 My Website&lt;/p&gt;<br>        &lt;/footer&gt;<br>    &lt;/div&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;<br></pre><p><strong>2. Using Fragments in Templates:</strong> Include these fragments in main template file using th:replace and th:insert.</p><pre>&lt;!-- index.html --&gt;<br>&lt;!DOCTYPE html&gt;<br>&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;<br>&lt;head&gt;<br>    &lt;title&gt;Home Page&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>    &lt;!-- Replace the div with header fragment --&gt;<br>    &lt;div th:replace=&quot;fragments :: header&quot;&gt;&lt;/div&gt;<br><br>    &lt;main&gt;<br>        &lt;p&gt;Welcome to the homepage!&lt;/p&gt;<br>    &lt;/main&gt;<br><br>    &lt;!-- Insert the footer fragment content into the div --&gt;<br>    &lt;div th:insert=&quot;fragments :: footer&quot;&gt;&lt;/div&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p><strong>Advanced Fragment Parameters:</strong></p><p>Fragments can also accept parameters, making them even more dynamic and reusable.</p><p><strong>3. Define Fragments with Parameters:</strong> Modify fragments.html to accept parameters.</p><pre>&lt;!-- fragments.html --&gt;<br>&lt;!DOCTYPE html&gt;<br>&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;<br>&lt;body&gt;<br>    &lt;div th:fragment=&quot;header(title)&quot;&gt;<br>        &lt;header&gt;<br>            &lt;h1 th:text=&quot;${title}&quot;&gt;Default Title&lt;/h1&gt;<br>        &lt;/header&gt;<br>    &lt;/div&gt;<br>    &lt;div th:fragment=&quot;footer(year)&quot;&gt;<br>        &lt;footer&gt;<br>            &lt;p th:text=&quot;&#39;&amp;copy; &#39; + ${year}&quot;&gt;© 2024&lt;/p&gt;<br>        &lt;/footer&gt;<br>    &lt;/div&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p><strong>4. Passing Parameters to Fragments:</strong> Pass parameters when including these fragments in your main template.</p><pre>&lt;!-- index.html --&gt;<br>&lt;!DOCTYPE html&gt;<br>&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;<br>&lt;head&gt;<br>    &lt;title&gt;Home Page&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>    &lt;!-- Replace the div with header fragment and pass a parameter --&gt;<br>    &lt;div th:replace=&quot;fragments :: header(&#39;Home Page&#39;)&quot;&gt;&lt;/div&gt;<br><br>    &lt;main&gt;<br>        &lt;p&gt;Welcome to the homepage!&lt;/p&gt;<br>    &lt;/main&gt;<br><br>    &lt;!-- Insert the footer fragment content into the div and pass a parameter --&gt;<br>    &lt;div th:insert=&quot;fragments :: footer(${2024})&quot;&gt;&lt;/div&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p><strong>Conditional Fragment Insertion:</strong></p><p>Sometimes, you may need to conditionally insert fragments based on certain conditions. Thymeleaf allows us to do this efficiently.</p><p><strong>5. Conditional Insertion:</strong> Use Thymeleaf’s conditional attributes to include fragments conditionally.</p><pre>&lt;!-- index.html --&gt;<br>&lt;!DOCTYPE html&gt;<br>&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;<br>&lt;head&gt;<br>    &lt;title&gt;Home Page&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>    &lt;!-- Replace the div with header fragment --&gt;<br>    &lt;div th:replace=&quot;fragments :: header(&#39;Home Page&#39;)&quot;&gt;&lt;/div&gt;<br><br>    &lt;main&gt;<br>        &lt;p&gt;Welcome to the homepage!&lt;/p&gt;<br><br>        &lt;!-- Conditional fragment insertion --&gt;<br>        &lt;div th:if=&quot;${user.isLoggedIn}&quot;&gt;<br>            &lt;div th:replace=&quot;fragments :: welcomeUser(${user.name})&quot;&gt;&lt;/div&gt;<br>        &lt;/div&gt;<br>        &lt;div th:unless=&quot;${user.isLoggedIn}&quot;&gt;<br>            &lt;div th:replace=&quot;fragments :: loginPrompt&quot;&gt;&lt;/div&gt;<br>        &lt;/div&gt;<br>    &lt;/main&gt;<br><br>    &lt;!-- Insert the footer fragment content into the div --&gt;<br>    &lt;div th:insert=&quot;fragments :: footer(${2024})&quot;&gt;&lt;/div&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p><strong>Conclusion:</strong></p><p>Thymeleaf’s capabilities for dynamic content and fragment insertion significantly enhance the flexibility and maintainability of web applications. By leveraging th:replace, th:insert, and passing parameters to fragments, you can build reusable components that streamline template management. Additionally, conditional fragment insertion adds a dynamic aspect to Thymeleaf templates, making them adaptable to various user contexts and scenarios. Embracing these features can lead to more efficient and modular web application development, ultimately improving both developer productivity and application performance.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ed941988a28" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Queries in RTL (React Testing Library)]]></title>
            <link>https://medium.com/@mravashna/queries-in-rtl-react-testing-library-484368f1bb87?source=rss-f9b4ba70f9e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/484368f1bb87</guid>
            <dc:creator><![CDATA[Mravashna]]></dc:creator>
            <pubDate>Tue, 07 Nov 2023 16:29:46 GMT</pubDate>
            <atom:updated>2023-11-07T16:29:46.712Z</atom:updated>
            <content:encoded><![CDATA[<p>Ever found yourself in a testing labyrinth, where the lines blur between different queries, leaving you in a state of confusion? Don’t worry; you’re not alone! We’ve all been there, scratching our heads, wondering what makes each query unique and when to apply them effectively. In fact, it’s precisely this head-scratching that led to the creation of this blog! 😄</p><p><strong>Intro:</strong></p><p>Queries in RTL are methods that help you search for elements within your React components. The fundamental idea behind RTL is to test your application from a user’s perspective rather than focusing on implementation details. RTL provides a wide range of queries to find, access, and interact with elements based on their characteristics.</p><p>There are 3 main types of RTL queries namely <strong>get, find and query</strong></p><h3><strong>Purpose of <em>getBy </em>:</strong></h3><p>getBy queries allow you to test your application&#39;s user interface and user interactions. This encourages you to think from the user&#39;s perspective and ensures that your tests cover the real user experience.</p><p>With getBy queries, you create tests that are more meaningful and maintainable. They focus on what the user sees and does rather than the internal workings of your components. This results in tests that are less brittle and more adaptable to changes in your application.</p><p>getBy queries are valuable when you want to access specific elements in your component&#39;s output. Whether it&#39;s a navigation link, a login button, or a form input, getBy helps you target and assert these elements precisely.</p><p>When it comes to the getBy queries, there are some common ones you&#39;ll frequently use:</p><ul><li>getByText: Finds an element by its text content.</li><li>getByTestId: Locates an element by its data-testid attribute.</li><li>getByRole: Finds an element based on its accessibility role.</li><li>getByPlaceholderText: Searches for form elements by their placeholder text.</li></ul><h4>Example: Using getBy Queries</h4><pre>import React from ‘react’;<br>import { render } from ‘@testing-library/react’;<br>import LoginButton from ‘./LoginButton’;<br><br>test(&#39;Login button is displayed&#39;, () =&gt; {<br> const { getByText } = render(&lt;LoginButton /&gt;);<br> const loginButton = getByText(&#39;Login&#39;);<br> expect(loginButton).toBeInTheDocument();<br>});</pre><p>In this test, we render the LoginButton component and use getByText to find the &quot;Login&quot; button. We then assert that the button is present using expect(loginButton).toBeInTheDocument().</p><h3><strong>Purpose of <em>queryBy </em>:</strong></h3><p>queryBy queries are designed to find and assert the presence of elements in component&#39;s rendered output. The significant difference between queryBy and its counterparts, such as getBy or findBy, is that queryBy queries do not throw errors when the element is not found. Instead, they return either the element if it&#39;s present or null if it&#39;s not found. This makes a great choice for testing scenarios where element presence is uncertain.</p><p>Here are some common queryBy queries:</p><ul><li>queryByText: Searches for an element with specific text content.</li><li>queryByTestId: Locates an element by its data-testid attribute.</li><li>queryByRole: Finds an element based on its accessibility role.</li><li>queryByPlaceholderText: Searches for form elements by their placeholder text.</li></ul><h4>Example: Using queryBy Queries</h4><p>Imagine you have a React component that renders a button when a user is logged in and a login form when the user is not logged in. You want to test that the login form is displayed when the user is not logged in. Here’s how you can use queryBy:</p><pre>import React from &#39;react&#39;;<br>import { render } from &#39;@testing-library/react&#39;;<br>import App from &#39;./App&#39;;<br><br>test(&#39;Login form is displayed when the user is not logged in&#39;, () =&gt; {<br>  render(&lt;App isLoggedIn={false} /&gt;);<br><br>  const loginForm = screen.queryByTestId(&#39;login-form&#39;);<br>  expect(loginForm).toBeInTheDocument();<br>});</pre><p>In this example, we use screen.queryByTestId(&#39;login-form&#39;) to check that the login form is displayed when the user is not logged in.</p><h3><strong>Purpose of <em>findBy </em>:</strong></h3><p>The findBy queries are asynchronous variations of the common getBy queries. They are used to find elements that are expected to appear in the DOM but may not be present immediately. The main difference is that getBy queries throw an error if they cannot find the element, whereas findBy queries return a promise that resolves when the element is found or rejects with an error if it&#39;s not found within a specified timeout.</p><p>Here are some commonly used findBy queries:</p><ul><li>findByText: Finds an element with specific text content.</li><li>findByTestId: Finds an element by its data-testid attribute.</li><li>findByRole: Finds an element based on its accessibility role.</li><li>findByPlaceholderText: Finds form elements by their placeholder text.</li></ul><h4>Example: Using queryBy Queries</h4><p>Imagine you have a React component that loads user data from an API and displays it. The data loading process might take some time, and you want to ensure that the loading state is correctly displayed and that the user data is eventually rendered.</p><pre>import React from &#39;react&#39;;<br>import { render, waitFor, screen } from &#39;@testing-library/react&#39;;<br>import UserComponent from &#39;./UserComponent&#39;;<br><br>test(&#39;UserComponent displays user data after loading&#39;, async () =&gt; {<br>  render(&lt;UserComponent /&gt;);<br><br>  // Wait for the loading indicator to disappear.<br>  const loadingIndicator = screen.getByText(&#39;Loading...&#39;);<br>  await waitFor(() =&gt; {<br>    expect(loadingIndicator).not.toBeInTheDocument();<br>  });<br><br>  // Find the user&#39;s name in the rendered component.<br>  const userName = await screen.findByText(&#39;John Doe&#39;);<br><br>  expect(userName).toBeInTheDocument();<br>});</pre><p>In this example, we use screen.findByText(&#39;John Doe&#39;) to find the user&#39;s name, which is expected to appear after the loading indicator disappears. We use waitFor to wait for the loading indicator to be removed from the DOM.</p><h3><strong>Conclusion:</strong></h3><p>Till now we saw the single queries — findBy, getBy, queryBy. We also have multiple queries like getAllBy, findAllBy, and queryAllBy which are used to find and interact with multiple matching elements in your component&#39;s rendered output. These queries are valuable when you need to work with arrays of elements that share common characteristics, such as multiple buttons, links, or form inputs. They help you perform actions or assertions on all matching elements simultaneously, making your tests more efficient and concise.</p><p>Mastering queries in React Testing Library is not just about understanding how to find elements or make assertions. It’s about adopting a user-centric mindset and crafting tests that tell the story of your application’s behaviour from the user’s perspective.So, embrace these queries, write tests with confidence, and embark on a journey to deliver exceptional user experiences through robust and meaningful testing.</p><p>Happy testing!🤩</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=484368f1bb87" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>