{"id":127,"date":"2022-11-01T14:03:00","date_gmt":"2022-11-01T14:03:00","guid":{"rendered":"http:\/\/blog.firatyasar.com\/?p=127"},"modified":"2023-03-19T18:03:27","modified_gmt":"2023-03-19T18:03:27","slug":"how-to-clear-redis-cache-with-an-automated-function-in-azure-function-app","status":"publish","type":"post","link":"https:\/\/blog.firatyasar.com\/?p=127","title":{"rendered":"How to Clear Redis Cache with an Automated Function in Azure Function App"},"content":{"rendered":"\n<h4 id=\"7529\">Overview<\/h4>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/liftcodeplay.com\/images\/af-and-redis.png\" alt=\"LIFT.CODE.PLAY\"\/><\/figure>\n\n\n\n<p id=\"0e6b\">Sometimes we need to automate some processes instead of doing them manually. In a project that I work for, I needed to flush Redis DB automatically on an hourly basis. So I developed a simple function written in Python in&nbsp;<strong>Azure Functions<\/strong>&nbsp;to flush the Redis DB. Today, I\u2019m giving some information and an example about how to use Azure Function App.<\/p>\n\n\n\n<h4 id=\"fbc6\">What are the&nbsp;<strong>Azure Functions<\/strong>&nbsp;In a Nutshell?<\/h4>\n\n\n\n<p id=\"a211\">Azure Functions are a serverless concept of cloud-native design that allows a piece of code to be deployed and executed without any need for server infrastructure, web server, or any configurations. Azure Functions can be written in multiple languages such as C#, Java, JavaScript, TypeScript, and Python.<\/p>\n\n\n\n<p id=\"d0c9\">To begin with, on the&nbsp;<strong>Create Function App<\/strong>&nbsp;page, we must pick mandatory areas like Subscription, Resource Group, Function App name, Publish type, Runtime stack, Version, and Region.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"820\" src=\"\/wp-content\/uploads\/2023\/03\/image-12-1024x820.png\" alt=\"\" class=\"wp-image-128\" srcset=\"\/wp-content\/uploads\/2023\/03\/image-12-1024x820.png 1024w, \/wp-content\/uploads\/2023\/03\/image-12-300x240.png 300w, \/wp-content\/uploads\/2023\/03\/image-12-768x615.png 768w, \/wp-content\/uploads\/2023\/03\/image-12-660x529.png 660w, \/wp-content\/uploads\/2023\/03\/image-12.png 1328w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>On the&nbsp;<strong>Hosting<\/strong>&nbsp;page, we can define&nbsp;<em>Storage account<\/em>,&nbsp;<em>Operating System<\/em>, and&nbsp;<em>Plan type<\/em>. The runtime stack contains .NET, Node.js, Python, Java, Powershell Core, and Custom Handler.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"665\" src=\"\/wp-content\/uploads\/2023\/03\/image-13-1024x665.png\" alt=\"\" class=\"wp-image-129\" srcset=\"\/wp-content\/uploads\/2023\/03\/image-13-1024x665.png 1024w, \/wp-content\/uploads\/2023\/03\/image-13-300x195.png 300w, \/wp-content\/uploads\/2023\/03\/image-13-768x499.png 768w, \/wp-content\/uploads\/2023\/03\/image-13-660x429.png 660w, \/wp-content\/uploads\/2023\/03\/image-13.png 1398w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><em>if Linux is needed to be used as an operating system, we can\u2019t use the Azure Portal for editing and developing the functions. At the beginning, we should write the function that we need locally then everything is done, the function must be pushed to Azure Function App from the local environment. When you decide to develop your function in Python, there is no other chance than to use the Linux operating system.<\/em><\/p>\n\n\n\n<h3 id=\"deb3\">Create your local project<\/h3>\n\n\n\n<p id=\"d67c\">In this section, you should use Visual Studio Code to create a local Azure Functions for your project.<\/p>\n\n\n\n<ol><li>Choose the Azure icon in the Activity bar, then in the Azure: Functions area, select the Create new project\u2026 icon.<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"550\" src=\"\/wp-content\/uploads\/2023\/03\/image-14-1024x550.png\" alt=\"\" class=\"wp-image-130\" srcset=\"\/wp-content\/uploads\/2023\/03\/image-14-1024x550.png 1024w, \/wp-content\/uploads\/2023\/03\/image-14-300x161.png 300w, \/wp-content\/uploads\/2023\/03\/image-14-768x412.png 768w, \/wp-content\/uploads\/2023\/03\/image-14-660x354.png 660w, \/wp-content\/uploads\/2023\/03\/image-14.png 1192w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>2. Before you can publish your app, you must sign in to Azure.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"632\" src=\"\/wp-content\/uploads\/2023\/03\/image-15-1024x632.png\" alt=\"\" class=\"wp-image-131\" srcset=\"\/wp-content\/uploads\/2023\/03\/image-15-1024x632.png 1024w, \/wp-content\/uploads\/2023\/03\/image-15-300x185.png 300w, \/wp-content\/uploads\/2023\/03\/image-15-768x474.png 768w, \/wp-content\/uploads\/2023\/03\/image-15-660x407.png 660w, \/wp-content\/uploads\/2023\/03\/image-15.png 1232w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>3. Choose the Azure icon in the Activity bar, then in the Azure: Functions area, choose the Deploy to function app button. Then follow the steps which are given in the prompts.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"549\" src=\"\/wp-content\/uploads\/2023\/03\/image-16-1024x549.png\" alt=\"\" class=\"wp-image-132\" srcset=\"\/wp-content\/uploads\/2023\/03\/image-16-1024x549.png 1024w, \/wp-content\/uploads\/2023\/03\/image-16-300x161.png 300w, \/wp-content\/uploads\/2023\/03\/image-16-768x411.png 768w, \/wp-content\/uploads\/2023\/03\/image-16-660x354.png 660w, \/wp-content\/uploads\/2023\/03\/image-16.png 1206w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 id=\"54d2\"><strong>Function that is flushing the Redis DB<\/strong><\/h3>\n\n\n\n<p id=\"99e1\">There are a lot of template functions in the dropdown menu. To meet my needs, I just picked the&nbsp;<em>Timer trigger<\/em>&nbsp;function.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"646\" src=\"\/wp-content\/uploads\/2023\/03\/image-17-1024x646.png\" alt=\"\" class=\"wp-image-133\" srcset=\"\/wp-content\/uploads\/2023\/03\/image-17-1024x646.png 1024w, \/wp-content\/uploads\/2023\/03\/image-17-300x189.png 300w, \/wp-content\/uploads\/2023\/03\/image-17-768x484.png 768w, \/wp-content\/uploads\/2023\/03\/image-17-660x416.png 660w, \/wp-content\/uploads\/2023\/03\/image-17.png 1354w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p id=\"5bca\">After creating your timer trigger function, you may write your script under the main namespace. You just simply give your Redis host, port, and password to the method called Redis.<\/p>\n\n\n\n<p id=\"29a8\">Based on this definition, the&nbsp;<code>__init__.py<\/code>&nbsp;the file that contains the function code might look like the following<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"634\" src=\"\/wp-content\/uploads\/2023\/03\/image-18-1024x634.png\" alt=\"\" class=\"wp-image-134\" srcset=\"\/wp-content\/uploads\/2023\/03\/image-18-1024x634.png 1024w, \/wp-content\/uploads\/2023\/03\/image-18-300x186.png 300w, \/wp-content\/uploads\/2023\/03\/image-18-768x475.png 768w, \/wp-content\/uploads\/2023\/03\/image-18-660x408.png 660w, \/wp-content\/uploads\/2023\/03\/image-18.png 1364w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>When you\u2019re ready to publish, make sure that all your publicly available dependencies are listed in the&nbsp;<strong>requirements.txt<\/strong>&nbsp;file, which is located at the root of your project directory. Like given dependency for the Redis below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"390\" src=\"\/wp-content\/uploads\/2023\/03\/image-19-1024x390.png\" alt=\"\" class=\"wp-image-135\" srcset=\"\/wp-content\/uploads\/2023\/03\/image-19-1024x390.png 1024w, \/wp-content\/uploads\/2023\/03\/image-19-300x114.png 300w, \/wp-content\/uploads\/2023\/03\/image-19-768x292.png 768w, \/wp-content\/uploads\/2023\/03\/image-19-660x251.png 660w, \/wp-content\/uploads\/2023\/03\/image-19.png 1356w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p id=\"a9c6\">Inputs are divided into two categories in Azure Functions: trigger input and other input. Although they\u2019re different in the&nbsp;<code>function.json<\/code>&nbsp;file, usage is identical in Python code. Connection strings or secrets for trigger and input sources map to values in the&nbsp;<code>local.settings.json<\/code>&nbsp;file when running locally, and the application settings when running in Azure.<\/p>\n\n\n\n<p id=\"8cdc\">Here\u2019s the binding data in the&nbsp;<em>function.json<\/em>&nbsp;file<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"468\" src=\"\/wp-content\/uploads\/2023\/03\/image-20-1024x468.png\" alt=\"\" class=\"wp-image-136\" srcset=\"\/wp-content\/uploads\/2023\/03\/image-20-1024x468.png 1024w, \/wp-content\/uploads\/2023\/03\/image-20-300x137.png 300w, \/wp-content\/uploads\/2023\/03\/image-20-768x351.png 768w, \/wp-content\/uploads\/2023\/03\/image-20-660x302.png 660w, \/wp-content\/uploads\/2023\/03\/image-20.png 1356w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>When your timer triggered function starts working you may check the&nbsp;<em>invocations&nbsp;<\/em>and&nbsp;<em>logs&nbsp;<\/em>under the&nbsp;<strong>Monitor&nbsp;<\/strong>section.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"501\" src=\"\/wp-content\/uploads\/2023\/03\/image-21-1024x501.png\" alt=\"\" class=\"wp-image-137\" srcset=\"\/wp-content\/uploads\/2023\/03\/image-21-1024x501.png 1024w, \/wp-content\/uploads\/2023\/03\/image-21-300x147.png 300w, \/wp-content\/uploads\/2023\/03\/image-21-768x376.png 768w, \/wp-content\/uploads\/2023\/03\/image-21-660x323.png 660w, \/wp-content\/uploads\/2023\/03\/image-21.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>For more detailed information, the documents below must be inspected by those who want to dive deeply.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"490\" height=\"354\" src=\"\/wp-content\/uploads\/2023\/03\/image-22.png\" alt=\"\" class=\"wp-image-139\" srcset=\"\/wp-content\/uploads\/2023\/03\/image-22.png 490w, \/wp-content\/uploads\/2023\/03\/image-22-300x217.png 300w\" sizes=\"(max-width: 490px) 100vw, 490px\" \/><\/figure>\n\n\n\n<p>Source: https:\/\/learn.microsoft.com\/en-us\/azure\/azure-functions\/<\/p>\n\n\n\n<p>F\u0131rat<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview Sometimes we need to automate some processes instead of doing them manually. In a project that I work for, I needed to flush Redis DB automatically on an hourly basis. So I developed a simple function written in Python in&nbsp;Azure Functions&nbsp;to flush the Redis DB. Today, I\u2019m giving some information and an example about\u2026 <span class=\"read-more\"><a href=\"https:\/\/blog.firatyasar.com\/?p=127\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":139,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.firatyasar.com\/index.php?rest_route=\/wp\/v2\/posts\/127"}],"collection":[{"href":"https:\/\/blog.firatyasar.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.firatyasar.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.firatyasar.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.firatyasar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=127"}],"version-history":[{"count":3,"href":"https:\/\/blog.firatyasar.com\/index.php?rest_route=\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":181,"href":"https:\/\/blog.firatyasar.com\/index.php?rest_route=\/wp\/v2\/posts\/127\/revisions\/181"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.firatyasar.com\/index.php?rest_route=\/wp\/v2\/media\/139"}],"wp:attachment":[{"href":"https:\/\/blog.firatyasar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.firatyasar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.firatyasar.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}