LoginJoin GenScrap
Back to Public Gallery
From: k1ito-techby k1itoabout 1 month ago

いい選択です 👍 diskcacheディスクにキャッシュを保存できるライブラリ で、メモリを圧迫せずに大量のデータをキャッシュできます。しかも API がシンプルで、Webアプリや機械学習の前処理結果キャッシュなどにもよく使われます。


##インストール

bash
pip install diskcache

##基本の使い方

###キー・バリューのキャッシュ

python
import diskcache as dc cache = dc.Cache("./mycache") # 値を保存 cache["x"] = 42 # 値を取得 print(cache["x"]) # 42 # デフォルト値付き取得 print(cache.get("y", "default")) # default

###関数の結果をキャッシュ(デコレータ)

python
import time import diskcache as dc cache = dc.Cache("./mycache") @cache.memoize() def slow_function(x): time.sleep(2) return x * x print(slow_function(5)) # 初回は2秒かかる print(slow_function(5)) # 2回目はキャッシュから即時取得

###TTL(有効期限つきキャッシュ)

python
cache.set("key", "value", expire=10) # 10秒で消える print(cache.get("key")) # value

###Webアプリ (Flask) での例

python
from flask import Flask import diskcache as dc app = Flask(__name__) cache = dc.Cache("./flask_cache") @app.route("/square/<int:n>") @cache.memoize(expire=60) # 60秒キャッシュ def square(n): return str(n * n) if __name__ == "__main__": app.run(debug=True)

##特徴まとめ

  • 速い(C実装が多い)。
  • 永続化:プロセスが終了してもキャッシュ残る。
  • 柔軟:TTL、サイズ制限、memoizeデコレータ対応。
  • スレッド/プロセス安全なので並列処理にも強い。

👉 diskcache をどんな用途で使いたいですか? (例:APIレスポンスのキャッシュ、データ前処理結果の保存、Webアプリの応答高速化など) それに合わせてベストプラクティスを提案できます。

About this Scrapbook
See Also

Other scraps from "k1ito-tech"

Best mcp server development sdk?

If by “MCP server” you mean a server implementing the Model Context Protocol (MCP) to allow LLMs / AI agents to interact with external tools/data sour...

about 1 month ago
#model context protocol#mcp sdk+3

# [2508.20722] rStar2-Agent: Agentic Reasoning Technical Report

[2508.20722] rStar2-Agent: Agentic Reasoning Technical Report URL: https://www.arxiv.org/abs/2508.20722 Captured: 2025/9/6 17:39:22 --- Computer ...

about 2 months ago
#agentic reinforcement learning#large language models+3

Daytona Sandbox:開発環境の新たな可能性

Daytona Sandbox:開発環境の新たな可能性 Daytona Sandboxとは Daytona Sandboxは、開発者がクラウド上で瞬時に開発環境を構築・共有できる革新的なプラットフォームです。従来のローカル開発環境の制約を取り払い、どこからでもアクセス可能な統一された開発体験...

about 2 months ago
#daytona#sandbox+3

E2B example in Python

step-by-step E2B example in Python that shows stateful execution, installing packages, uploading a file, and doing a quick SQLite query—all inside a s...

about 2 months ago
#e2b#python+3

# Agentic workflow patterns - AWS Prescriptive Guidance

Agentic workflow patterns integrate modular software agents with structured large language model (LLM) workflows, enabling autonomous reasoning and ac...

2 months ago
#aws#agentic ai+3

Amazon EC2 Single GPU P5 instances are now generally available

What's New at AWS - Cloud Innovation & News URL: https://aws.amazon.com/jp/about-aws/whats-new/2025/08/amazon-p5-single-gpu-instances-now-available/...

2 months ago
#AWS EC2#NVIDIA H100+3

Want to create your own articles?

Get Started