<?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 Degisew Mengist on Medium]]></title>
        <description><![CDATA[Stories by Degisew Mengist on Medium]]></description>
        <link>https://medium.com/@jegisew21?source=rss-a79336905dba------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GKqtv9YnHoxjDS6sljxMJw.jpeg</url>
            <title>Stories by Degisew Mengist on Medium</title>
            <link>https://medium.com/@jegisew21?source=rss-a79336905dba------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 15:54:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jegisew21/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[Caching in Django]]></title>
            <link>https://medium.com/@jegisew21/caching-in-django-f37d0916eae3?source=rss-a79336905dba------2</link>
            <guid isPermaLink="false">https://medium.com/p/f37d0916eae3</guid>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[caching]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[performance-optimization]]></category>
            <dc:creator><![CDATA[Degisew Mengist]]></dc:creator>
            <pubDate>Sun, 06 Jul 2025 22:01:19 GMT</pubDate>
            <atom:updated>2025-07-06T22:01:19.012Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong><em>Caching = Where to store + What to cache</em></strong></blockquote><h4>What is Caching?</h4><p>Caching is the process of storing copies of files or data in a temporary storage location so that future requests for that data can be served much faster than accessing the data’s primary storage location. It can also mean saving the result of an expensive calculation so you don’t have to perform the calculation again.</p><p><strong>Cache</strong> is a high-speed data storage layer that stores either the result of an earlier computation or a copy of data stored elsewhere.</p><p>There are different types of caching like <strong>browser</strong> caching, <strong>CDN</strong> caching, <strong>DNS</strong> caching, <strong>in-memory</strong> caching but here, I’ll focus on server-side caching and how to implement it in Django.</p><h4>Why Do We Need Caching?</h4><p>Every time a user requests a page in your application, the web server performs numerous calculations: database queries, business logic processing, and template rendering (if you’re building a full-stack app). This creates the requested page from scratch each time. This approach becomes expensive, especially for medium- to high-traffic sites, and can lead to poor user experience or even server crashes when traffic exceeds server limits. Caching helps solve this by storing the result of these expensive computations and reuse precomputed responses, which significantly boosting performance and reducing load.</p><p>Now that you understand what caching is and why we need it, let’s explore the steps and techniques for implementing caching in <strong>Django</strong> — Python’s mature full-stack web framework.</p><h4>1. Where to Store the Cache (Cache Backends)</h4><p>First, you need to decide where to store your cache data. This choice significantly affects your cache’s performance, since some cache types are faster than others.</p><p>Django provides a robust cache system that lets you save dynamic pages so they don’t need to be recalculated for each request. You can choose from several storage options:</p><h4><strong>In-Memory Caching</strong></h4><p>Stores data in your computer’s RAM, making it extremely fast to access compared to reading from disk or a database. Django supports several in-memory caching options:</p><ul><li><strong>Local memory caching</strong> (Django’s default)</li><li><strong>Redis</strong></li><li><strong>Memcached</strong></li></ul><h4><strong>File system Caching</strong></h4><p>This method stores cache data as separate files in your file system. File system caching is the simplest to configure but becomes the slowest option when storing numerous files.</p><h4><strong>Database Caching</strong></h4><p>You can use your database to store caches in Django. <em>You might wonder:</em> <strong><em>“Isn’t the database slow? Isn’t avoiding database hits the whole point of caching?”</em></strong></p><p>Here’s the key difference: During normal requests, the server performs heavy calculations including<strong> database queries</strong>, <strong>external API calls</strong>, and other complex operations. With database caching, you perform these calculations once and store the result in the database. Future requests only need a single, fast query to retrieve the pre-calculated result, instead of repeating all those expensive operations.</p><p><strong><em>Note: </em></strong><em>This works best with a fast, well-indexed database server.</em></p><h4><strong>Custom Cache Backend</strong></h4><p>While Django includes several cache backends out-of-the-box, you might occasionally need a customized solution. However, unless you have a compelling reason (such as host limitations), stick to Django’s included cache backends. They’ve been well-tested and are well-documented.</p><p><strong><em>Note:</em></strong><em> You can find the cache backends configuration setting in the official </em><a href="https://docs.djangoproject.com/en/5.2/topics/cache/#setting-up-the-cache"><em>documentation</em></a><em>.</em></p><h4>2. What to Cache (Caching Strategy)</h4><p>Once your cache is configured, you can choose what data to cache for fast lookup. Django offers the following main caching strategies:</p><h4><strong>Per-Site Cache</strong></h4><p>Per-site caching means Django caches the entire response for every request. This works best for mostly static websites.</p><p>To implement per-site caching, add these middleware configurations to your settings:</p><pre>MIDDLEWARE = [<br>    &quot;django.middleware.cache.UpdateCacheMiddleware&quot;,<br>    # ... other middleware ...<br>    &quot;django.middleware.cache.FetchFromCacheMiddleware&quot;,<br>]</pre><p><strong><em>Important middleware order:</em></strong></p><ul><li>UpdateCacheMiddleware saves responses to cache after views run (place first)</li><li>FetchFromCacheMiddleware checks if URLs are cached before views run (place last)</li></ul><p>And add these required settings to your Django settings file:</p><pre>CACHE_MIDDLEWARE_ALIAS = &#39;default&#39;  # The cache alias for storage<br>CACHE_MIDDLEWARE_SECONDS = 600      # Seconds each page should be cached<br>CACHE_MIDDLEWARE_KEY_PREFIX = &#39;&#39;    # Site name if using multiple sites</pre><p><strong><em>Note: </em></strong><em>Per-site caching only caches </em><strong><em>GET</em></strong><em> and </em><strong><em>HEAD</em></strong><em> requests.</em></p><h4><strong>Per-View Caching</strong></h4><p>This caches the full response for specific views based on URL and request parameters. If multiple URLs point to the same view, each URL is cached separately. Per-view caching can be implemented using the cache_page decorator:</p><pre>from django.views.decorators.cache import cache_page<br><br>@cache_page(60 * 5)  # Cache for 5 minutes<br>def my_view(request):<br>    # view logic here<br>    pass</pre><p>For <strong>class-based views</strong>, use the method decorator:</p><pre>from django.views.decorators.cache import cache_page<br>from django.utils.decorators import method_decorator<br><br>@method_decorator(cache_page(60 * 5), name=&#39;get&#39;) # set the name of the method like get, post<br>class MyView(View):<br>    def get(self, request):<br>        # view logic here<br>        pass</pre><p>But the above implementation has downsides because we’re hard-coding caching into the view itself. That means that the view<strong> </strong>is always cached, and we can’t easily reuse that view in a non-cached environment. It’s also not flexible since it’s tightly coupled to caching logic.</p><p><strong>Better approach:</strong> Instead of hard-coding caching into views, apply caching in your URL configuration for more flexibility:</p><pre>from django.views.decorators.cache import cache_page<br>from myapp.views import my_view<br><br>urlpatterns = [<br>    path(&#39;my-url/&#39;, cache_page(60 * 15)(my_view)),<br>]</pre><p>This approach keeps your views reusable and decouples caching logic from view implementation.</p><h4><strong>Template Fragment Caching</strong></h4><p>For full-stack Django applications, you can cache specific template fragments using the {% cache %} template tag. This is perfect for expensive-to-render or static page sections like sidebars or dropdowns.</p><pre>{% load cache %}<br>{% cache 300 product_sidebar %}<br>    &lt;!-- expensive rendering logic --&gt;<br>{% endcache %}</pre><h4><strong>Low-Level Cache API</strong></h4><p>Use low-level caching when you need to cache any python data that isn’t views or templates, such as <strong>querysets</strong>, <strong>calculated values</strong>, or <strong>API responses</strong>. This API gives you full control over cache keys, timeouts, and invalidation.</p><p>To use low level cache API, you use Django’s built-in default cache object from <strong><em>django.core.cache</em>. </strong>The default cache object has methods like <strong><em>cache.set()</em></strong><em>, </em><strong><em>cache</em></strong><em>.</em><strong><em>get()</em></strong><em>, </em><strong><em>cache.delete()</em></strong> to manage the cache.</p><pre>from django.core.cache import cache<br><br>def get_expensive_data():<br>    key = &quot;cache_unique_key&quot;<br>    data = cache.get(key) # get the data from the cache<br>    <br>    if data is None:<br>        data = expensive_function()  # Database query or expensive operation<br>        cache.set(key, data, timeout=600)  # Cache for 10 minutes<br>    <br>    return data</pre><p>Now, you can use this function wherever you need. The function performs the expensive calculation only once, then serves cached results for subsequent requests. I personally use low-level caching to store data lookups (like dropdown values), because they rarely change. You can also handle cache invalidation on creation or update using Django signals.</p><h4>Conclusion</h4><p>Caching is essential for building performant Django applications. Start by choosing the right cache backend for your needs, then implement the appropriate caching strategy based on your application’s requirements. <strong>Remember:</strong> the best caching strategy often combines multiple approaches. You might use per-view caching for dynamic pages, while employing low-level caching for expensive data lookups.</p><p>The key to successful caching is understanding your application’s bottlenecks and choosing the right caching strategy for each use case. So before doing any caching or performance improvement, track the bottlenecks using tools like <a href="https://github.com/django-commons/django-debug-toolbar">django debug toolbar</a>, <a href="https://github.com/jazzband/django-silk">django silk</a>. You can also use <a href="https://github.com/locustio/locust">locust</a> for performance/load testing.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f37d0916eae3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A couple of things you should know before start learning how to code]]></title>
            <link>https://medium.com/@jegisew21/a-couple-of-things-you-should-know-before-start-learning-how-to-code-a5f8ea097dea?source=rss-a79336905dba------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5f8ea097dea</guid>
            <dc:creator><![CDATA[Degisew Mengist]]></dc:creator>
            <pubDate>Wed, 16 Nov 2022 18:35:06 GMT</pubDate>
            <atom:updated>2022-11-16T18:35:06.547Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/474/1*w7iH2XwIvsefBj5Ud-dxew.jpeg" /></figure><p>In order to achieve your goal, you have to have two critical things. Those things are known as <strong><em>consistency</em></strong> and<strong><em> time-management skills</em></strong>.</p><p><strong><em>Stay consistent is a key to success!</em></strong></p><p>Staying consistent is a very crucial thing to achieving your goals. You should also have good time management techniques as well.</p><p>In this article, we will discuss how staying consistent and time management skills helped me in my tech journey.</p><p>Learning how to code is not easy. It needs high effort and time to master. It has a lot of ups and downs and you may be vulnerable to imposter syndrome. But trust me, this is not your problem rather it happens to every programmer especially if you are a beginner.</p><p>5 years ago, when I was a freshman university student, I started learning to code. I was so happy when I meet my first programming language which was C++. I planned to learn in detail and When I start studying, it gets complex as I go deep into it. I struggled a lot every day to get familiar with programming and I stayed consistent with my goal and finally bitted the complexity. After graduation, I got a job Immediately even if it was not my dream job. My dream is to be a world-class software developer and I work for it. I left a lot of jobs that pay worth a lot but I left them for my dream. I stayed consistent with my goal to be a world-class developer. While I’m working on my dream I use a block time-management mechanism to use my time efficiently. This mechanism allows me to stick with the schedule and it helps me a lot to achieve my goal. With these things, I achieved my goal. Now, I am a full-stack web developer which was my dream for years.</p><p>Conclusion:</p><p>I will tell my readers that the key takeaways from this article are consistency and time management skills. Be consistent and manage your time efficiently to live your dream. I hope you enjoyed reading it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5f8ea097dea" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>