Monday, May 30, 2022
HomeWordPress DevelopmentLearn how to use App Engine Memcache in Flask apps (Module 12)

Learn how to use App Engine Memcache in Flask apps (Module 12)



Posted by Wesley Chun

Background

In our ongoing Serverless Migration Station sequence aimed toward serving to builders modernize their serverless functions, one of many key aims for Google App Engine builders is to improve to the most recent language runtimes, akin to from Python 2 to three or Java 8 to 17. One other goal is to assist builders learn to transfer away from App Engine legacy APIs (now referred to as “bundled companies”) to Cloud standalone equal companies. As soon as this has been completed, apps are rather more transportable, making them versatile sufficient to:

In right now’s Module 12 video, we’ll begin our journey by implementing App Engine’s Memcache bundled service, setting us up for our subsequent transfer to a extra full in-cloud caching service, Cloud Memorystore. Most apps usually depend on some database, and in lots of conditions, they will profit from a caching layer to cut back the variety of queries and enhance response latency. Within the video, we add use of Memcache to a Python 2 app that has already migrated net frameworks from webapp2 to Flask, offering better portability and execution choices. Extra importantly, it paves the way in which for an eventual 3.x improve as a result of the Python 3 App Engine runtime doesn’t assist webapp2. We’ll cowl each the three.x and Cloud Memorystore ports subsequent in Module 13.

Acquired an older app needing an replace? We will help with that.

Including use of Memcache

The pattern software registers particular person net web page “visits,” storing customer data such because the IP handle and consumer agent. Within the authentic app, these values are saved instantly, after which the newest visits are queried to show within the browser. If the identical consumer constantly refreshes their browser, every refresh constitutes a brand new go to. To discourage the sort of abuse, we cache the identical consumer’s go to for an hour, returning the identical cached listing of most up-to-date visits except a brand new customer arrives or an hour has elapsed since their preliminary go to.

Under is pseudocode representing the core a part of the app that saves new visits and queries for the newest visits. Earlier than, you may see how every go to is registered. After the replace, the app makes an attempt to fetch these visits from the cache. If cached outcomes can be found and “contemporary” (throughout the hour), they’re used instantly, but when cache is empty, or a brand new customer arrives, the present go to is saved as earlier than, and this newest assortment of visits is cached for an hour. The bolded strains symbolize the brand new code that manages the cached information.

Including App Engine Memcache utilization to pattern app

Wrap-up

Immediately’s “migration” started with the Module 1 pattern app. We added a Memcache-based caching layer and arrived on the end line with the Module 12 pattern app. To apply this by yourself, comply with the codelab doing it by-hand whereas following the video. The Module 12 app will then be able to improve to Cloud Memorystore do you have to select to take action.

In Fall 2021, the App Engine workforce prolonged assist of most of the bundled companies to next-generation runtimes, which means you’re not required emigrate to Cloud Memorystore when porting your app to Python 3. You’ll be able to proceed utilizing Memcache in your Python 3 app as long as you retrofit the code to entry bundled companies from next-generation runtimes.

Should you do need to transfer to Cloud Memorystore, keep tuned for the Module 13 video or strive its codelab to get a sneak peek. All Serverless Migration Station content material (codelabs, movies, supply code [when available]) will be accessed at its open supply repo. Whereas our content material initially focuses on Python customers, we hope to in the future cowl different language runtimes, so keep tuned. For added video content material, try our broader Serverless Expeditions sequence.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments