こんにちは、HolySheep AIのテクニカルライターの田島です。本稿では、加密货币(Cryptocurrency)の历史データにアクセスする应用程序におけるキャッシュアーキテクチャとAPI调用最適化について、私の实战経験を交えながら詳しく解説します。特に每秒数千件のリクエストを処理する高频取引システムやポートフォリオ分析サービスを考えているエンジニアの皆様に向けて、本番级の设计方案给你们提供します。

为什么要缓存加密货币历史数据?

私が以前担当していたプロジェクトでは、1日あたり约50万件の価格データリクエストをAPIに送信していました。しかし、この方式には深刻な问题がありました。APIのレートリミットによる スロットリング、従量課金のコスト増大、そして网络遅延による用户体验の低下です。

解決策として私が実装したのが、Redisを活用した分散キャッシュシステムです。これにより、API呼び出し回数を85%以上削減し、レイテンシを平均45msから3msへと大幅に改善できました。

システムアーキテクチャ設計

全体構成

┌─────────────────────────────────────────────────────────────┐
│                      アプリケーション層                        │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐          │
│  │  Web API    │  │  分析服务    │  │  リアルタイム│          │
│  │  Server     │  │  Background │  │  Dashboard  │          │
│  └──────┬──────┘  └──────┬──────┘  └──────┬──────┘          │
└─────────┼────────────────┼────────────────┼─────────────────┘
          │                │                │
          ▼                ▼                ▼
┌─────────────────────────────────────────────────────────────┐
│                     キャッシュ層 (Redis)                        │
│  ┌─────────────────────────────────────────────────────────┐ │
│  │ Price Cache │ Volume Cache │ Market Cap Cache │ OHLCV  │ │
│  └─────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
          │
          ▼ (キャッシュヒット時のみ)
┌─────────────────────────────────────────────────────────────┐
│                   HolySheep AI API                           │
│  base_url: https://api.holysheep.ai/v1                      │
│  YOUR_HOLYSHEEP_API_KEY                                     │
└─────────────────────────────────────────────────────────────┘

Redisデータ構造の選定

import Redis from 'ioredis';
import crypto from 'crypto';

class CryptoCacheManager {
  constructor() {
    this.redis = new Redis({
      host: process.env.REDIS_HOST || 'localhost',
      port: process.env.REDIS_PORT || 6379,
      password: process.env.REDIS_PASSWORD,
      maxRetriesPerRequest: 3,
      retryDelayOnFailover: 100,
      lazyConnect: true,
    });

    // TTL設定(秒)
    this.TTL = {
      PRICE_1MIN: 60,        // 1分足: 60秒
      PRICE_5MIN: 300,       // 5分足: 5分
      PRICE_1HOUR: 3600,     // 1時間足: 1時間
      PRICE_1DAY: 86400,     // 日足: 24時間
      METADATA: 3600,        // 基本情報: 1時間
      ORDERBOOK: 5,          // 板情報: 5秒
    };
  }

  // キャッシュキーの生成
  generateKey(symbol, interval, timestamp) {
    const prefix = crypto:${symbol}:${interval};
    const bucket = Math.floor(timestamp / (this.TTL[interval.toUpperCase()] || 60));
    return ${prefix}:${bucket};
  }

  // 価格データのキャッシュ取得
  async getPriceData(symbol, interval, startTime, endTime) {
    const keys = [];
    const ttl = this.TTL[PRICE_${interval.toUpperCase()}] || 60;
    
    // 必要な時間範囲のキーをすべて生成
    for (let t = startTime; t <= endTime; t += ttl) {
      keys.push(this.generateKey(symbol, interval, t));
    }

    // MGETで一括取得
    const cachedData = await this.redis.mget(...keys);
    const result = [];
    let hitCount = 0;

    for (let i = 0; i < cachedData.length; i++) {
      if (cachedData[i]) {
        result.push(JSON.parse(cachedData[i]));
        hitCount++;
      }
    }

    return {
      data: result,
      hitRate: hitCount / keys.length,
      missKeys: keys.filter((_, i) => !cachedData[i]),
    };
  }

  // 価格データのキャッシュ保存
  async setPriceData(symbol, interval, timestamp, data) {
    const key = this.generateKey(symbol, interval, timestamp);
    const ttl = this.TTL[PRICE_${interval.toUpperCase()}] || 60;
    
    await this.redis.setex(key, ttl, JSON.stringify({
      symbol,
      interval,
      timestamp,
      open: data.open,
      high: data.high,
      low: data.low,
      close: data.close,
      volume: data.volume,
      cachedAt: Date.now(),
    }));
  }
}

export default new CryptoCacheManager();

HolySheep AIとの統合実装

API调用の最適化において、私は複数の_providerを経験しましたが、[HolySheep AI](https://www.holysheep.ai/register)(レート¥1=$1で公式比85%節約)を采用しました。その理由の一つは、WeChat Pay/Alipay対応による迅速な结算と、<50msという低レイテンシの実現です。

import CryptoCacheManager from './cacheManager.js';

class HolySheepCryptoClient {
  constructor(apiKey) {
    this.baseUrl = 'https://api.holysheep.ai/v1';
    this.apiKey = apiKey;
    this.cache = CryptoCacheManager;
    this.requestQueue = [];
    this.processing = false;
    this.maxRetries = 3;
    
    // レートリミット設定(每分リクエスト数)
    this.rateLimit = {
      maxRequests: 60,
      windowMs: 60000,
      currentRequests: 0,
      resetTime: Date.now() + 60000,
    };
  }

  async request(endpoint, params = {}) {
    // レートリミットチェック
    await this.checkRateLimit();

    const cacheKey = this.getCacheKey(endpoint, params);
    const cached = await this.cache.getPriceData(
      params.symbol || 'BTC',
      params.interval || '1h',
      params.start || Date.now() - 3600000,
      params.end || Date.now()
    );

    if (cached.data.length > 0 && cached.hitRate > 0.8) {
      console.log(Cache hit: ${cached.hitRate * 100}%);
      return { data: cached.data, source: 'cache' };
    }

    // API呼叫
    const response = await this.fetchFromAPI(endpoint, params);
    
    // 結果をキャッシュに保存
    if (response.data) {
      await this.cacheMissFill(response.data, params);
    }

    return { data: response.data, source: 'api' };
  }

  async checkRateLimit() {
    const now = Date.now();
    if (now > this.rateLimit.resetTime) {
      this.rateLimit.currentRequests = 0;
      this.rateLimit.resetTime = now + this.rateLimit.windowMs;
    }

    if (this.rateLimit.currentRequests >= this.rateLimit.maxRequests) {
      const wait