計算中の存続時間 (TTL) - Amazon DynamoDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

計算中の存続時間 (TTL)

TTL を実装する一般的な方法は、作成日または最終更新日に基づいて項目の有効期限を設定することです。これは、createdAtupdatedAtとタイムスタンプに時間を追加することで実現できます。たとえば、新しく作成されたアイテムの TTL を createdAt +90 日に設定できます。アイテムが更新されると、TTL は +90 日に再計算できます。updatedAt

計算された有効期限は、秒単位のエポック形式である必要があります。TTL の有効期限が切れたり、削除されたりしたと見なされるには、TTL が 5 年以上前のものであってはなりません。他の形式を使用すると、TTL プロセスはその項目を無視します。有効期限をfuture どこかに設定してアイテムの有効期限を設定すると、その期間が過ぎるとアイテムは期限切れになります。たとえば、有効期限を 1724241326 (2024 年 8 月 21 日 (月) 11:55:26 (GMT)) に設定したとします。アイテムは指定した時間が過ぎると有効期限が切れます。

アイテムを作成し、有効期間を設定します。

次の例は、TTL expireAt 属性名として使用して、新しいアイテムを作成するときに有効期限を計算する方法を示しています。代入ステートメントは、現在の時刻を変数として取得します。この例では、有効期限は現在の時刻から 90 日後に計算されます。その後、時刻はエポック形式に変換され、TTL 属性に整数データ型として保存されます。

Python
import boto3 from datetime import datetime, timedelta def create_dynamodb_item(table_name, region, primary_key, sort_key): """ Creates a DynamoDB item with an attached expiry attribute. :param table_name: Table name for the boto3 resource to target when creating an item :param region: string representing the AWS region. Example: `us-east-1` :param primary_key: one attribute known as the partition key. :param sort_key: Also known as a range attribute. :return: Void (nothing) """ try: dynamodb = boto3.resource('dynamodb', region_name=region) table = dynamodb.Table(table_name) # Get the current time in epoch second format current_time = int(datetime.now().timestamp()) # Calculate the expiration time (90 days from now) in epoch second format expiration_time = int((datetime.now() + timedelta(days=90)).timestamp()) item = { 'primaryKey': primary_key, 'sortKey': sort_key, 'createdAt': current_time, 'expiredAt': expiration_time } table.put_item(Item=item) print("Item created successfully.") except Exception as e: print(f"Error creating item: {e}") raise # Use your own values create_dynamodb_item('your-table-name', 'us-west-2', 'your-partition-key-value', 'your-sort-key-value')
Javascript

このリクエストでは、新しく作成されたアイテムの有効期限を計算するロジックを追加します。

import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb"; function createDynamoDBItem(table_name, region, partition_key, sort_key) { const client = new DynamoDBClient({ region: region, endpoint: `https://dynamodb.${region}.amazonaws.com` }); // Get the current time in epoch second format const current_time = Math.floor(new Date().getTime() / 1000); // Calculate the expireAt time (90 days from now) in epoch second format const expire_at = Math.floor((new Date().getTime() + 90 * 24 * 60 * 60 * 1000) / 1000); // Create DynamoDB item const item = { 'partitionKey': {'S': partition_key}, 'sortKey': {'S': sort_key}, 'createdAt': {'N': current_time.toString()}, 'expireAt': {'N': expire_at.toString()} }; const putItemCommand = new PutItemCommand({ TableName: table_name, Item: item, ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1, }, }); client.send(putItemCommand, function(err, data) { if (err) { console.log("Exception encountered when creating item %s, here's what happened: ", data, ex); throw err; } else { console.log("Item created successfully: %s.", data); return data; } }); } // use your own values createDynamoDBItem('your-table-name', 'us-east-1', 'your-partition-key-value', 'your-sort-key-value');

アイテムを更新し、有効期間を更新します。

この例は、前のセクションの例の続きです。アイテムが更新されると、有効期限を再計算できます。次の例では、expireAtタイムスタンプを現在の時刻から 90 日後に再計算します。

Python
import boto3 from datetime import datetime, timedelta def update_dynamodb_item(table_name, region, primary_key, sort_key): """ Update an existing DynamoDB item with a TTL. :param table_name: Name of the DynamoDB table :param region: AWS Region of the table - example `us-east-1` :param primary_key: one attribute known as the partition key. :param sort_key: Also known as a range attribute. :return: Void (nothing) """ try: # Create the DynamoDB resource. dynamodb = boto3.resource('dynamodb', region_name=region) table = dynamodb.Table(table_name) # Get the current time in epoch second format current_time = int(datetime.now().timestamp()) # Calculate the expireAt time (90 days from now) in epoch second format expire_at = int((datetime.now() + timedelta(days=90)).timestamp()) table.update_item( Key={ 'partitionKey': primary_key, 'sortKey': sort_key }, UpdateExpression="set updatedAt=:c, expireAt=:e", ExpressionAttributeValues={ ':c': current_time, ':e': expire_at }, ) print("Item updated successfully.") except Exception as e: print(f"Error updating item: {e}") # Replace with your own values update_dynamodb_item('your-table-name', 'us-west-2', 'your-partition-key-value', 'your-sort-key-value')

更新操作の出力には、時刻は変更されていないが、createdAtupdatedAtexpireAtおよび時刻は更新されていることが示されています。これで、expireAt時刻は最終更新日の 2023 年 10 月 19 日 (木) 午後 1 時 27:15 から 90 日間に設定されました。

partition_key createdAt updatedAt 有効期限: 属性_1 属性_2

サムバリュー

2023-07-17 14:11:05.322 323 2023-07-19 13:27:15.213 423 1697722035 new_value some_value
Javascript
import { DynamoDBClient, UpdateItemCommand } from "@aws-sdk/client-dynamodb"; import { marshall, unmarshall } from "@aws-sdk/util-dynamodb"; async function updateDynamoDBItem(tableName, region, partitionKey, sortKey) { const client = new DynamoDBClient({ region: region, endpoint: `https://dynamodb.${region}.amazonaws.com` }); const currentTime = Math.floor(Date.now() / 1000); const expireAt = Math.floor((Date.now() + 90 * 24 * 60 * 60 * 1000) / 1000); ////is there a better way to do this? const params = { TableName: tableName, Key: marshall({ partitionKey: partitionKey, sortKey: sortKey }), UpdateExpression: "SET updatedAt = :c, expireAt = :e", ExpressionAttributeValues: marshall({ ":c": currentTime, ":e": expireAt }), }; try { const data = await client.send(new UpdateItemCommand(params)); const responseData = unmarshall(data.Attributes); console.log("Item updated successfully: %s", responseData); return responseData; } catch (err) { console.error("Error updating item:", err); throw err; } } //enter your values here updateDynamoDBItem('your-table-name', 'us-east-1', 'your-partition-key-value', 'your-sort-key-value');

このイントロダクションで説明した TTL の例は、最近更新された項目だけをテーブルに保持する方法を示しています。更新された項目は有効期限が延びますが、作成後に更新されなかった項目は有効期限が切れて無料で削除されるため、ストレージが減り、テーブルがクリーンに保たれます。