キャッシング

term_id: caching

Category: engineering_practice

Definition

AIエンジニアリングにおいて、キャッシングは最近または頻繁なクエリ結果、モデル予測、または中間計算を高速メモリ(RAMなど)に保持することでパフォーマンスを最適化します。これにより、高価な…

Summary

キャッシングは、頻繁にアクセスされるデータを一時高速ストレージ層に保存し、レイテンシを削減し、主要なデータソースへの負荷を軽減する技術です。

Key Concepts

  • レイテンシ削減
  • メモリ最適化
  • _evictionポリシー(淘汰ポリシー)
  • ヒット率

Use Cases

  • モデル推論結果の保存
  • データベースクエリ出力のキャッシュ
  • 特徴量埋め込みの事前計算

Code Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import redis

# Simple caching example
r = redis.Redis(host='localhost', port=6379, db=0)

def get_prediction(model_id, input_data):
    cache_key = f"pred_{model_id}_{hash(str(input_data))}"
    result = r.get(cache_key)
    if result:
        return result.decode('utf-8')
    # Compute if not cached
    prediction = model.predict(input_data)
    r.setex(cache_key, 3600, str(prediction))
    return prediction