0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

momento cache sdkを触ってみる。

Posted at

はじめに

札幌で開催されたserverless meetup sapporoにて、momentoを触ったので、それ元に自身の知識を整理する記事です。

今回はTypeScriptを使って簡単にキャッシュの操作をしてみます。

前回はこちら。

触ったみた!

まずは完成形

キャッシュクライアントを作成し、キャッシュ保存・取得を速度測定と共に行なっているコードです。

なお、この記事用に書いたソースは以下にも格納しています。

import {
  CacheClient,
  Configurations,
  CredentialProvider,
  CreateCache,
  CacheSet,
  CacheGet,
  CacheDictionaryFetch,
  CacheDictionaryGetField,
} from "@gomomento/sdk";

const DEFAULT_TTL = 600;

/**
 * 処理速度を計測するデコレータ
 */
function measureProcessingTime() {
  return function (
    target: any,
    propertyKey: string,
    descriptor: PropertyDescriptor
  ) {
    const originalMethod = descriptor.value;

    descriptor.value = async function (...args: any[]) {
      const start = performance.now();
      const result = await originalMethod.apply(this, args);
      const end = performance.now();
      const processingTime = end - start;
      console.log(
        `${propertyKey}の処理時間: ${processingTime.toFixed(2)}ミリ秒`
      );
      return result;
    };

    return descriptor;
  };
}

class CacheManager {
  private client: CacheClient | null = null;

  /**
   * 処理速度を計測するデコレータ
   */
  @measureProcessingTime()
  async createCacheClient() {
    this.client = await CacheClient.create({
      configuration: Configurations.Laptop.v1(),
      credentialProvider: CredentialProvider.fromEnvironmentVariable({
        environmentVariableName: "MOMENTO_API_KEY",
      }),
      defaultTtlSeconds: DEFAULT_TTL,
    });
  }

  @measureProcessingTime()
  async createCache(cacheName: string) {
    if (!this.client) throw new Error("Client not initialized");

    const response: CreateCache.Response = await this.client.createCache(
      cacheName
    );
    console.log(`createCache:${response.type}`);
  }

  @measureProcessingTime()
  async writeString(cacheName: string, key: string, value: string) {
    if (!this.client) throw new Error("Client not initialized");

    const response: CacheSet.Response = await this.client.set(
      cacheName,
      key,
      value
    );

    console.log(`writeString:${response.type}`);
  }

  @measureProcessingTime()
  async writeDictionary(
    cacheName: string,
    key: string,
    value: Map<string, string>
  ) {
    if (!this.client) throw new Error("Client not initialized");

    const response = await this.client.dictionarySetFields(
      cacheName,
      key,
      value
    );
    console.log(`writeDictionary:${response.type}`);
  }

  @measureProcessingTime()
  async writeDictionaryItem(
    cacheName: string,
    directoryName: string,
    key: string,
    value: string
  ) {
    if (!this.client) throw new Error("Client not initialized");

    const response = await this.client.dictionarySetField(
      cacheName,
      directoryName,
      key,
      value
    );
    console.log(`writeDictionaryItem:${response.type}`);
  }

  @measureProcessingTime()
  async readFromCache(cacheName: string, key: string) {
    if (!this.client) throw new Error("Client not initialized");

    const response: CacheGet.Response = await this.client.get(cacheName, key);
    if (response instanceof CacheGet.Hit) {
      console.log("readFromCache: ", response.value());
    } else {
      console.log("readFromCache: ", response.type);
    }
  }

  @measureProcessingTime()
  async featchFromCache(cacheName: string, key: string) {
    if (!this.client) throw new Error("Client not initialized");

    const response = await this.client.dictionaryFetch(cacheName, key);
    if (response instanceof CacheDictionaryFetch.Hit) {
      console.log("featchFromCache: ", response.value());
    } else {
      console.log("featchFromCache: ", response.type);
    }
  }

  @measureProcessingTime()
  async getDirectoryItem(
    cacheName: string,
    directoryName: string,
    key: string
  ) {
    if (!this.client) throw new Error("Client not initialized");

    const response = await this.client.dictionaryGetField(
      cacheName,
      directoryName,
      key
    );
    if (response instanceof CacheDictionaryGetField.Hit) {
      console.log("getDirectoryItem: ", response.value());
    } else {
      console.log("getDirectoryItem: ", response.type);
    }
  }
}

async function main() {
  const cacheManager = new CacheManager();
  const cacheName = "sample-cache";

  // Cacheの作成
  await cacheManager.createCacheClient();
  await cacheManager.createCache(cacheName);

  // string型のValueのWrite/Read
  await cacheManager.writeString(cacheName, "string-key", "hoge");
  await cacheManager.readFromCache(cacheName, "string-key");

  // Dictionary型のValueのWrite/Read
  await cacheManager.writeDictionary(
    cacheName,
    "dic-key",
    new Map([
      ["key1", "value1"],
      ["key2", "value2"],
    ])
  );

  // valueのDirectoryに一つアイテムを追加
  await cacheManager.writeDictionaryItem(
    cacheName,
    "dic-key",
    "key3",
    "value3"
  );

  // Key-Valueの順序は不同
  await cacheManager.featchFromCache(cacheName, "dic-key");

  // valueのDirectoryの中のキーを指定して取得
  await cacheManager.getDirectoryItem(cacheName, "dic-key", "key2");
}

main().catch(console.error);

実行すると以下のような結果が得られます。
インターネット経由の確認ですが、レイテンシも少ないと思います。

createCacheClientの処理時間: 174.08ミリ秒
createCache:AlreadyExists
createCacheの処理時間: 88.47ミリ秒
writeString:Success
writeStringの処理時間: 26.49ミリ秒
readFromCache:  hoge
readFromCacheの処理時間: 20.75ミリ秒
writeDictionary:Success
writeDictionaryの処理時間: 27.44ミリ秒
writeDictionaryItem:Success
writeDictionaryItemの処理時間: 21.93ミリ秒
featchFromCache:  { key2: 'value2', key1: 'value1', key3: 'value3' }
featchFromCacheの処理時間: 27.01ミリ秒
getDirectoryItem:  value2
getDirectoryItemの処理時間: 20.55ミリ秒

では、各処理ごとにみていきます。

事前準備

今回はAPIキーを環境変数に定義するので、MOMENTO_API_KEYに自分の環境変数を設定

export MOMENTO_API_KEY=[APIキーの内容]

キャッシュクライアントの作成

credentialに環境変数MOMENTO_API_KEYを利用することを指定しています。

データの保持期間はTTLで指定します。

指定した名前のキャッシュクライアントが既に存在する場合は、CreateCacheResponse.AlreadyExistsが返却されるみたいです。

  @measureProcessingTime()
  async createCacheClient() {
    this.client = await CacheClient.create({
      configuration: Configurations.Laptop.v1(),
      credentialProvider: CredentialProvider.fromEnvironmentVariable({
        environmentVariableName: "MOMENTO_API_KEY",
      }),
      defaultTtlSeconds: DEFAULT_TTL,
    });
  }

キャッシュの作成

次にキャッシュを作ります。
以降で紹介する各種キャッシュを保存する領域みたいなものを作るイメージです。

  @measureProcessingTime()
  async createCache(cacheName: string) {
    if (!this.client) throw new Error("Client not initialized");

    const response: CreateCache.Response = await this.client.createCache(
      cacheName
    );
    console.log(`createCache:${response.type}`);
  }

キャッシュへの登録(String)

momento chacheで保持できるデータの型は以下の定義の通りです。
今回は、簡単なStringとdictionaryを試してみました。

まずは、stringです。

  @measureProcessingTime()
  async writeString(cacheName: string, key: string, value: string) {
    if (!this.client) throw new Error("Client not initialized");

    const response: CacheSet.Response = await this.client.set(
      cacheName,
      key,
      value
    );

    console.log(`writeString:${response.type}`);
  }
 await cacheManager.writeString(cacheName, "string-key", "hoge");

キャッシュから取得(String)

キーの取得です。
(直感的に記載できるので、あまり書くこともないです。。)

  @measureProcessingTime()
  async readFromCache(cacheName: string, key: string) {
    if (!this.client) throw new Error("Client not initialized");

    const response: CacheGet.Response = await this.client.get(cacheName, key);
    if (response instanceof CacheGet.Hit) {
      console.log("readFromCache: ", response.value());
    } else {
      console.log("readFromCache: ", response.type);
    }
  }

await cacheManager.readFromCache(cacheName, "string-key");
// hoge

キャッシュへの登録(dictionary)

dictionary型は[["key1", "value1"],["key2", "value2"]]みたいな、Key-Value型のストレージです。

ストレージを丸ごと保存することもできるし、ストレージの中に新しいキーを保存することもできます。

  // 丸ごと保存
  @measureProcessingTime()
  async writeDictionary(
    cacheName: string,
    key: string,
    value: Map<string, string>
  ) {
    if (!this.client) throw new Error("Client not initialized");

    const response = await this.client.dictionarySetFields(
      cacheName,
      key,
      value
    );
    console.log(`writeDictionary:${response.type}`);
  }

  // 特定のキーのみに取得
  @measureProcessingTime()
  async writeDictionaryItem(
    cacheName: string,
    directoryName: string,
    key: string,
    value: string
  ) {
    if (!this.client) throw new Error("Client not initialized");

    const response = await this.client.dictionarySetField(
      cacheName,
      directoryName,
      key,
      value
    );
    console.log(`writeDictionaryItem:${response.type}`);
  }

  await cacheManager.writeDictionary(
    cacheName,
    "dic-key",
    new Map([
      ["key1", "value1"],
      ["key2", "value2"],
    ])
  );

  await cacheManager.writeDictionaryItem(
    cacheName,
    "dic-key",
    "key3",
    "value3"
  );

キャッシュから取得(dictionary)

dictionaryのキー取得は、順不同なのが注意点です。

またこちらも同様にストレージから特定のキーを指定し、取得することも可能です。

  @measureProcessingTime()
  async featchFromCache(cacheName: string, key: string) {
    if (!this.client) throw new Error("Client not initialized");

    const response = await this.client.dictionaryFetch(cacheName, key);
    if (response instanceof CacheDictionaryFetch.Hit) {
      console.log("featchFromCache: ", response.value());
    } else {
      console.log("featchFromCache: ", response.type);
    }
  }

  @measureProcessingTime()
  async getDirectoryItem(
    cacheName: string,
    directoryName: string,
    key: string
  ) {
    if (!this.client) throw new Error("Client not initialized");

    const response = await this.client.dictionaryGetField(
      cacheName,
      directoryName,
      key
    );
    if (response instanceof CacheDictionaryGetField.Hit) {
      console.log("getDirectoryItem: ", response.value());
    } else {
      console.log("getDirectoryItem: ", response.type);
    }
  }
  // Key-Valueの順序は不同
  await cacheManager.featchFromCache(cacheName, "dic-key");
  // { key3: 'value3', key1: 'value1', key2: 'value2' }

  // valueのDirectoryの中のキーを指定して取得
  await cacheManager.getDirectoryItem(cacheName, "dic-key", "key2");
  // value2

最後に

初めて書きましたが、とっても直感的に書くことができ、簡単に実装できそうです!

ぜひ業務に取り入れいたいところです!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?