How to Clear Redis Cache with an Automated Function in Azure Function App

Onur Yaşar
adessoTurkey
Published in
4 min readApr 11, 2022

--

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 Azure Functions to flush the Redis DB. Today, I’m giving some information and an example about how to use Azure Function App.

What are the Azure Functions In a Nutshell?

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.

To begin with, on the Create Function App page, we must pick mandatory areas like Subscription, Resource Group, Function App name, Publish type, Runtime stack, Version, and Region.

On the Hosting page, we can define Storage account, Operating System, and Plan type. The runtime stack contains .NET, Node.js, Python, Java, Powershell Core, and Custom Handler.

if Linux is needed to be used as an operating system, we can’t 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.

Create your local project

In this section, you should use Visual Studio Code to create a local Azure Functions for your project.

  1. Choose the Azure icon in the Activity bar, then in the Azure: Functions area, select the Create new project… icon.

2. Before you can publish your app, you must sign in to Azure.

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.

Function that is flushing the Redis DB

There are a lot of template functions in the dropdown menu. To meet my needs, I just picked the Timer trigger function.

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.

Based on this definition, the __init__.py the file that contains the function code might look like the following

When you’re ready to publish, make sure that all your publicly available dependencies are listed in the requirements.txt file, which is located at the root of your project directory. Like given dependency for the Redis below.

Inputs are divided into two categories in Azure Functions: trigger input and other input. Although they’re different in the function.json file, usage is identical in Python code. Connection strings or secrets for trigger and input sources map to values in the local.settings.json file when running locally, and the application settings when running in Azure.

Here’s the binding data in the function.json file

When your timer triggered function starts working you may check the invocations and logs under the Monitor section.

For more detailed information, the documents below must be inspected by those who want to dive deeply.

References

--

--