0Pricing
Flask Academy · レッスン

コストの高い関数をメモ化する

引数ごとに関数の結果をキャッシュします

「コストの高い関数をメモ化する」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Beyond Whole Views

Sometimes only one slow helper is the problem, not the whole page. Memoization caches a function result keyed by its arguments.

The memoize Decorator

You wrap a function with @cache.memoize(). Same arguments return the stored result instead of recomputing it. 💡

@cache.memoize()
def heavy(n):
    return slow_calc(n)

Keyed by Arguments

Memoize builds the key from the arguments. heavy(2) and heavy(5) are cached separately, each remembering its own answer.

cached vs memoize

Use cached for views keyed by URL, and memoize for plain functions keyed by their inputs. They solve different layers.

Set a memoize Timeout

Just like views, you give memoize a timeout so results expire. After it passes, the next call recomputes and re-stores.

@cache.memoize(timeout=120)
def rates():
    ...

Perfect for Pure Functions

Memoize shines on pure functions: same input always gives the same output, with no side effects to lose.

Avoid Caching Side Effects

Do not memoize a function that sends email or writes a row. On a cache hit the body is skipped, so the effect never runs.

Methods Need Care

For instance methods, the key includes self. Two objects cache separately, which is usually exactly what you want.

Drop One Memoized Result

You can evict a single entry with delete_memoized, passing the function and the exact arguments to clear.

cache.delete_memoized(rates)

Clear by Argument

Pass arguments to delete_memoized to drop just that variant, leaving every other cached result untouched.

cache.delete_memoized(heavy, 5)

Measure Before Memoizing

Only memoize what is actually slow. Profile first, because caching cheap work adds key overhead with little real gain.

Quick Check

You memoize a function that also logs each call to your database.

Recap: Memoization

You can memoize slow pure functions, key results by arguments, set timeouts, and evict with delete_memoized. 🎯

よくある質問

「コストの高い関数をメモ化する」レッスンは無料ですか?

はい。「コストの高い関数をメモ化する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「コストの高い関数をメモ化する」で何を学びますか?

引数ごとに関数の結果をキャッシュします ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Flask Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「コストの高い関数をメモ化する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFlask Academyレッスンでコードを書いて実行できますか?

はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Flask-Cachingをセットアップする
  2. cachedでビューをキャッシュする
  3. コストの高い関数をメモ化する
  4. キャッシュエントリを無効化して期限切れにする
← Flask Academyに戻る