@@ -62,18 +62,37 @@ def __init__(
62
62
>>> return {"StatusCode": 200}
63
63
"""
64
64
65
- boto_config = boto_config or Config ()
66
- session = boto3_session or boto3 .session .Session ()
67
- self ._ddb_resource = session .resource ("dynamodb" , config = boto_config )
65
+ self ._boto_config = boto_config or Config ()
66
+ self ._boto3_session = boto3_session or boto3 .session .Session ()
67
+
68
+ self ._table = None
68
69
self .table_name = table_name
69
- self .table = self ._ddb_resource .Table (self .table_name )
70
70
self .key_attr = key_attr
71
71
self .expiry_attr = expiry_attr
72
72
self .status_attr = status_attr
73
73
self .data_attr = data_attr
74
74
self .validation_key_attr = validation_key_attr
75
75
super (DynamoDBPersistenceLayer , self ).__init__ ()
76
76
77
+ @property
78
+ def table (self ):
79
+ """
80
+ Caching property to store boto3 dynamodb Table resource
81
+
82
+ """
83
+ if self ._table :
84
+ return self ._table
85
+ ddb_resource = self ._boto3_session .resource ("dynamodb" , config = self ._boto_config )
86
+ self ._table = ddb_resource .Table (self .table_name )
87
+ return self ._table
88
+
89
+ @table .setter
90
+ def table (self , table ):
91
+ """
92
+ Allow table instance variable to be set directly, primarily for use in tests
93
+ """
94
+ self ._table = table
95
+
77
96
def _item_to_data_record (self , item : Dict [str , Any ]) -> DataRecord :
78
97
"""
79
98
Translate raw item records from DynamoDB to DataRecord
@@ -125,7 +144,7 @@ def _put_record(self, data_record: DataRecord) -> None:
125
144
ExpressionAttributeNames = {"#id" : self .key_attr , "#now" : self .expiry_attr },
126
145
ExpressionAttributeValues = {":now" : int (now .timestamp ())},
127
146
)
128
- except self ._ddb_resource .meta .client .exceptions .ConditionalCheckFailedException :
147
+ except self .table .meta .client .exceptions .ConditionalCheckFailedException :
129
148
logger .debug (f"Failed to put record for already existing idempotency key: { data_record .idempotency_key } " )
130
149
raise IdempotencyItemAlreadyExistsError
131
150
0 commit comments