Momento Cacheを使ったファイルの追加と取得
Momento Cacheのアイテムはバイト配列なので、アイテムあたりの上限1MB以内であれば、作成したいファイルのほとんどを簡単にキャッシュに保存することができます。
以下は、ファイルシステムからファイルを読み込み、そのファイルをキャッシュのアイテムに保存し、キャッシュから読み込んでからファイルシステムに書き込む例です。
- JavaScript
- Python
import * as fs from 'node:fs';
import {CacheClient, CacheGetResponse, CacheSetResponse, Configurations, CredentialProvider} from '@gomomento/sdk';
const filePath = './myfile.json';
const fileName = 'myfile';
const CACHE_NAME = 'test-cache';
// Read a file from the filesystem
function readFile(filePath: string) {
try {
const data = fs.readFileSync(filePath);
return new Uint8Array(data);
} catch (error) {
console.error('Error reading file:', error);
return null;
}
}
// Creates the Momento cache client object
function createCacheClient(): Promise<CacheClient> {
return CacheClient.create({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: 'MOMENTO_API_KEY',
}),
defaultTtlSeconds: 600,
});
}
async function writeToCache(client: CacheClient, cacheName: string, key: string, data: Uint8Array) {
const setResponse = await client.set(cacheName, key, data);
switch (setResponse.type) {
case CacheSetResponse.Success:
console.log('Key stored successfully!');
break;
case CacheSetResponse.Error:
console.log(`Error setting key: ${setResponse.toString()}`);
break;
}
}
async function readFromCache(client: CacheClient, cacheName: string, key: string) {
const fileResponse = await client.get(cacheName, key);
switch (fileResponse.type) {
case CacheGetResponse.Miss:
console.log('cache miss');
break;
case CacheGetResponse.Hit: {
console.log(`cache hit: ${fileResponse.valueString()}`);
const contents = fileResponse.valueUint8Array();
const buffer = Buffer.from(contents);
fs.writeFileSync('./myfile2.json', buffer);
break;
}
case CacheGetResponse.Error:
console.log(`Error: ${fileResponse.message()}`);
break;
}
}
async function run() {
const byteArray = readFile(filePath);
if (byteArray === null) {
return;
}
const cacheClient = await createCacheClient();
await writeToCache(cacheClient, CACHE_NAME, fileName, byteArray);
await readFromCache(cacheClient, CACHE_NAME, fileName);
}
run().catch(e => {
console.error('Uncaught exception!', e);
throw e;
});
import os
from datetime import timedelta
from momento import CacheClient, Configurations, CredentialProvider
from momento.responses import CacheGet, CacheSet
file_path = './myfile.json'
file_name = 'myfile'
CACHE_NAME = 'test-cache'
# Read a file from the filesystem
def read_file(file_path):
with open(file_path, 'rb') as file:
byte_array = file.read()
return byte_array
# Write a file to the filesystem
def write_file(file_path, data):
with open(file_path, "wb") as out_file:
out_file.write(data)
# Get a connection to and existing cache with your API key.
def client():
momento_api_key = CredentialProvider.from_environment_variable('MOMENTO_API_KEY')
momento_ttl_seconds = os.getenv('MOMENTO_TTL_SECONDS')
ttl = timedelta(seconds=int(momento_ttl_seconds))
config = {
'configuration': Configurations.Laptop.v1(),
'credential_provider': momento_api_key,
'default_ttl': ttl
}
return CacheClient.create(**config)
def run():
# read the file contents in. They already come in byte format, so no casting necessary
byte_array = read_file(file_path)
# Get the client connection object.
with client() as cache_client:
# write the file to the cache
set_response = cache_client.set(CACHE_NAME, file_name, byte_array)
if isinstance(set_response, CacheSet.Success):
print('Key stored successfully!')
elif isinstance(set_response, CacheSet.Error):
print(f'Error setting key: {set_response}')
else:
print(f'Some other error: {set_response}')
# read the file from the cache
file_response = cache_client.get(CACHE_NAME, file_name)
if isinstance(file_response, CacheGet.Hit):
print(f'Cache hit! The value is: {file_response.value_string}')
buffer = bytes(file_response.value_string, 'utf-8')
print('Writing file to filesystem.')
write_file("./myfile2.json", buffer)
elif isinstance(file_response, CacheGet.Miss):
print('cache miss')
elif isinstance(file_response, CacheGet.Error):
print(f'Error: {file_response.message()}')
if __name__ == '__main__':
run()
Node.jsSDKの使い方については、Node.jsチートシートをご覧ください。
Python SDKの使い方については、Pythonチートシートをご覧ください。