[MUSIC PLAYING]
WESLEY: Hi, and welcome to "Serverless Migration Station."
I'm Wesley from Google, joined by Martin,
to help you modernize your apps running
on one of our serverless compute platforms.
In this and the next migration module,
our little friend Porter will take you
on a journey to migrate from App Engine
Memcache to Cloud Memorystore.
But we need to start using Memcache first
before we can migrate to Memorystore,
so this video won't feature any migrating at all.
MARTIN: Really?
More nonmigration content in a migration video series?
WESLEY: You caught me again.
Yes, Porter does take users from point A to point B,
but we need to first take a detour to reacquaint folks
with App Engine Memcache.
We'll add Memcache usage to the module 1 Flask App Engine NDB
app.
Then we can migrate it to Cloud Memorystore next time,
in module 13.
MARTIN: Sounds good.
Where do we start?
WESLEY: We're going to begin with the module 1 app,
the one we just migrated from webapp 2 to Flask,
and add use of App Engine Memcache to it.
Here's a link to that module 1 codelab and video
so you can review it, if necessary.
MARTIN: That sounds good, Wes.
Now both of those are in Python 2.
What about Python 3 developers?
WESLEY: Yeah.
Until fall 2021, you couldn't port the module 1 Flask app
to Python 3 because it uses App Engine bundled services--
in this case, NDB.
Once this feature launched, Python 3 users
are now able to access many of those services which
brought users to App Engine.
We'll cover this new feature in another video soon.
But suffice it to say that users can port the module 1
app to Python 3 first or add Memcache,
like we're going to do today, and then upgrade to Python 3.
Either way works.
MARTIN: Sounds good, Wes.
What are we going to use Memcache for in our sample app?
WESLEY: I'm glad you asked because I was just
about to get into that.
As you know, our app creates a new entity for each page visit.
Because it only shows the 10 most recent visitors,
there's no need to create new visits if the same user keeps
hitting our website, right?
So we'll just cache the most recent visits
for an hour for the same user.
If a new user comes along, that will reset the cache.
Also, the cache will expire after an hour.
Sound good?
MARTIN: OK.
But I have to be honest.
I haven't used App Engine Memcache in quite a while.
Can I get my hands dirty and do it myself,
while you are showing us onscreen?
WESLEY: Absolutely.
Go grab the module 1 code, if you did the codelab,
or clone the repo or download the ZIP file if you didn't.
Pause here if you need to do that.
Module 1 is where we'll start.
If you want to do this by hand with me
now or on your own time, follow the module 12 codelab.
Ready?
Let's go to the computer now and do this.
Because it's always good to start from a working app,
use gcloud app deploy to redeploy your module 1 app
and ensure it works before we update it.
Python 3 users can delete the appengine_config.py file
and relax for a bit, while Python 2 developers install
the third-party libraries into the lib folder with the pip
install command you see onscreen.
Then everyone can use gcloud app deploy
to upload the app to the cloud, then
confirm that that module 1 app still works.
Now we can add some caching to it.
Before we make changes, be aware you
won't see a difference in output afterwards.
Instead, we're just going to avoid the extra Datastore calls
and see lower latency.
With that out of the way, let's start
with the imports at the top.
All that's needed is to bring in the App Engine Memcache
library.
Since we're going to cache for an hour,
add a constant for that.
Register visits, querying for the latest visits to display,
and other Datastore calls stay the same.
The biggest updates to the code are in the main application.
So visits are determined by a visitor's IP address,
along with a user agent.
In the original app, we create the visit entity immediately.
In the update, we save the visitor info first,
then try to get the latest visits from the cache.
If the cache is empty or it's a new visitor,
save this as the newest visit.
Fetch all the latest visits from Datastore
and cache them for an hour, in case the same visitor
comes back sooner.
And that's all that's necessary to change this app to add
caching to the module 1 app.
So now just save and gcloud app deploy
to see our updated app in action.
Again, the output doesn't change,
but what you should notice is that page refreshes
will be much quicker.
Hit your app from another computer
or wait for more than an hour to see
that the app goes back to its original latency
in fetching of the initial data.
Now, before we get back to the main part of the show,
we want to acknowledge that most of the migration effort
is directed to Python 2 users to get them to the latest App
Engine runtimes.
Onscreen now are the Python 2 and 3 versions of the app.
The differences you see are what Python 2 developers
have to do to port their apps to Python 3.
This is an addition to any other Python 2-to-3 migration
steps you need to implement.
Our sample app is simple enough that it runs on Python 2 and 3
without modification.
So what you see is how to access App Engine bundled
services in Python 3.
More details can be found in the Docs link below,
as well as how to access these services from Java 11,
Go 1.12 and newer, and PHP 7.
OK, now let's go back to the main presentation.
MARTIN: Thanks for the tutorial, Wes.
Now where should I go if I want to reacquaint myself
with App Engine Memcache?
WESLEY: Yes, I'd say start with the Memcache overview
page in the docs.
For specific details, also check out the Memcache reference docs
in Python 2 or in Python 3.
MARTIN: Cool.
I'll take a look.
But can you give me a heads up on what we're doing next?
WESLEY: Sure, Martin.
Now that we have App Engine Memcache in our app,
we'll migrate to Cloud Memorystore in module 13.
If you want to read ahead, check out the links
to the migration page, as well as the Cloud Memorystore docs.
You can also take a look at the module 13 codelab
to see what we'll discuss in that video.
Of course, there are plenty of other migrations to consider.
But, at the end of this module 12,
you can get off at this station, staying on App Engine Memcache,
or consider that upgrade to Cloud Memorystore.
And we look forward to having you join us
for that discussion in module 13.
This is Wesley, on behalf of Martin and Porter,
and we hope to see you at that next Migration Station
or on another Serverless Expedition soon.
[MUSIC PLAYING]