いい選択です 👍
diskcache は ディスクにキャッシュを保存できるライブラリ で、メモリを圧迫せずに大量のデータをキャッシュできます。しかも API がシンプルで、Webアプリや機械学習の前処理結果キャッシュなどにもよく使われます。
pip install diskcacheimport diskcache as dc
cache = dc.Cache("./mycache")
# 値を保存
cache["x"] = 42
# 値を取得
print(cache["x"]) # 42
# デフォルト値付き取得
print(cache.get("y", "default")) # defaultimport 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回目はキャッシュから即時取得cache.set("key", "value", expire=10) # 10秒で消える
print(cache.get("key")) # valuefrom 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)👉 diskcache をどんな用途で使いたいですか?
(例:APIレスポンスのキャッシュ、データ前処理結果の保存、Webアプリの応答高速化など)
それに合わせてベストプラクティスを提案できます。
Popular topics in this scrapbook:
Other scraps from "k1ito-tech"
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...
[2508.20722] rStar2-Agent: Agentic Reasoning Technical Report URL: https://www.arxiv.org/abs/2508.20722 Captured: 2025/9/6 17:39:22 --- Computer ...
Daytona Sandbox:開発環境の新たな可能性 Daytona Sandboxとは Daytona Sandboxは、開発者がクラウド上で瞬時に開発環境を構築・共有できる革新的なプラットフォームです。従来のローカル開発環境の制約を取り払い、どこからでもアクセス可能な統一された開発体験...
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...
Agentic workflow patterns integrate modular software agents with structured large language model (LLM) workflows, enabling autonomous reasoning and ac...
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/...
Want to create your own articles?
Get Started