11package com .networknt .aws .lambda .cache ;
22
3- import com . amazonaws . services .dynamodbv2 . AmazonDynamoDB ;
4- import com . amazonaws . services .dynamodbv2 . AmazonDynamoDBClientBuilder ;
5- import com . amazonaws . services .dynamodbv2 . document .* ;
6- import com . amazonaws . services . dynamodbv2 . model .* ;
3+ import software . amazon . awssdk . services .dynamodb . DynamoDbClient ;
4+ import software . amazon . awssdk . services .dynamodb . model .* ;
5+ import software . amazon . awssdk . services .dynamodb . model . AttributeValue ;
6+ import software . amazon . awssdk . regions . Region ;
77import com .networknt .aws .lambda .utility .LambdaEnvVariables ;
88import com .networknt .cache .CacheManager ;
99import org .slf4j .Logger ;
@@ -19,9 +19,8 @@ public class DynamoDbCacheManager implements CacheManager {
1919 private static final String HASH_ID_KEY = "Id" ;
2020 private static final int TABLE_LIST_LIMIT = 100 ;
2121
22- private static final Map <String , Table > tables = new HashMap <>();
23- private final AmazonDynamoDB dynamoClient ;
24- private final DynamoDB dynamoDB ;
22+ private static final Map <String , String > tables = new HashMap <>();
23+ private final DynamoDbClient dynamoClient ;
2524 boolean tableInitiated ;
2625
2726 @ Override
@@ -30,98 +29,83 @@ public void addCache(String cacheName, long maxSize, long expiryInMinutes) {
3029 LOG .debug ("Table does not exist so we need to create it...." );
3130 createCacheTable (cacheName );
3231 }
33- Table table = this .dynamoDB .getTable (cacheName );
34- tables .put (cacheName , table );
32+ tables .put (cacheName , cacheName );
3533 }
34+
3635 @ Override
3736 public Map <Object , Object > getCache (String cacheName ) {
38- // cacheName is serviceId + ":" + jwt or jwk
3937 String applicationId = cacheName .split (":" )[0 ];
4038 String tableName = cacheName .split (":" )[1 ];
41- Item entry ;
39+ Map < String , AttributeValue > entry ;
4240 try {
43- Table table = tables .get (tableName );
44- entry = table .getItem (HASH_ID_KEY , applicationId );
41+ entry = dynamoClient .getItem (GetItemRequest .builder ()
42+ .tableName (tableName )
43+ .key (Collections .singletonMap (HASH_ID_KEY , AttributeValue .builder ().s (applicationId ).build ()))
44+ .build ()).item ();
4545 if (entry == null )
4646 return null ;
4747 } catch (NullPointerException e ) {
4848 return null ;
4949 }
50- return convertMap (entry . asMap () );
50+ return convertMap (entry );
5151 }
5252
53- public static Map <Object , Object > convertMap (Map <String , Object > originalMap ) {
54- return new HashMap <>(originalMap );
53+ public static Map <Object , Object > convertMap (Map <String , AttributeValue > originalMap ) {
54+ Map <Object , Object > convertedMap = new HashMap <>();
55+ originalMap .forEach ((k , v ) -> convertedMap .put (k , v .s ()));
56+ return convertedMap ;
5557 }
5658
5759 @ Override
5860 public void put (String cacheName , String key , Object value ) {
59-
60- // cacheName is serviceId + ":" + jwt or jwk
6161 String applicationId = cacheName .split (":" )[0 ];
6262 String tableName = cacheName .split (":" )[1 ];
6363
6464 LOG .debug ("Updating table entry of applicationId: {}, table name: {}, attribute key: {} and value: {}" , applicationId , tableName , key , value );
6565
66- Table table = tables .get (tableName );
67-
68- var item = table .getItem (HASH_ID_KEY , applicationId );
69-
70- if (item != null && item .getString (key ) != null ) {
71- LOG .debug ("Update spec...." );
72-
73- // TODO - Update string value
66+ Map <String , AttributeValue > itemKey = new HashMap <>();
67+ itemKey .put (HASH_ID_KEY , AttributeValue .builder ().s (applicationId ).build ());
7468
75- } else {
76- /* primary key for item */
77- var itemKey = new HashMap <String , AttributeValue >();
78- itemKey .put (HASH_ID_KEY , new AttributeValue ().withS (applicationId ));
79-
80- /* attribute we are adding to item */
81- var attributeUpdates = new HashMap <String , AttributeValueUpdate >();
82- var update = new AttributeValueUpdate ().withAction (AttributeAction .PUT ).withValue (new AttributeValue ().withS ((String )value ));
83- attributeUpdates .put (key , update );
84-
85- /* send update request */
86- var updateItemRequest = new UpdateItemRequest ().withTableName (tableName ).withKey (itemKey ).withAttributeUpdates (attributeUpdates );
87- var res = this .dynamoClient .updateItem (updateItemRequest );
88- LOG .debug ("RESULT: {}" , res .toString ());
89- }
69+ Map <String , AttributeValueUpdate > attributeUpdates = new HashMap <>();
70+ attributeUpdates .put (key , AttributeValueUpdate .builder ()
71+ .action (AttributeAction .PUT )
72+ .value (AttributeValue .builder ().s ((String ) value ).build ())
73+ .build ());
9074
75+ dynamoClient .updateItem (UpdateItemRequest .builder ()
76+ .tableName (tableName )
77+ .key (itemKey )
78+ .attributeUpdates (attributeUpdates )
79+ .build ());
9180 }
9281
9382 @ Override
9483 public Object get (String cacheName , String key ) {
95- // cacheName is serviceId + ":" + jwt or jwk
9684 String applicationId = cacheName .split (":" )[0 ];
9785 String tableName = cacheName .split (":" )[1 ];
9886
99- /* see if the table contains our application id. */
100- /* If not found, return null because we don't have a cache yet! */
101- Item entry ;
87+ Map <String , AttributeValue > entry ;
10288 try {
103- Table table = tables .get (tableName );
104- entry = table .getItem (HASH_ID_KEY , applicationId );
89+ entry = dynamoClient .getItem (GetItemRequest .builder ()
90+ .tableName (tableName )
91+ .key (Collections .singletonMap (HASH_ID_KEY , AttributeValue .builder ().s (applicationId ).build ()))
92+ .build ()).item ();
10593 if (entry == null )
10694 return null ;
10795 } catch (NullPointerException e ) {
10896 return null ;
10997 }
110- return entry .getString (key );
98+ return entry .get (key ). s ( );
11199 }
112100
113101 @ Override
114102 public void delete (String cacheName , String key ) {
115- // cacheName is serviceId + ":" + jwt or jwk
116- String applicationId = cacheName .split (":" )[0 ];
117- String tableName = cacheName .split (":" )[1 ];
118-
103+ // Implement delete logic if needed
119104 }
120105
121106 @ Override
122107 public void removeCache (String cacheName ) {
123- Table table = tables .get (cacheName );
124- if (table != null ) {
108+ if (tables .containsKey (cacheName )) {
125109 tables .remove (cacheName );
126110 try {
127111 deleteTable (cacheName );
@@ -136,82 +120,55 @@ public int getSize(String cacheName) {
136120 return 0 ;
137121 }
138122
139-
140123 public DynamoDbCacheManager () {
141- if (logger .isInfoEnabled ())
142- logger .info ("DynamoDbCacheManager is constructed." );
124+ if (LOG .isInfoEnabled ())
125+ LOG .info ("DynamoDbCacheManager is constructed." );
143126
144127 this .tableInitiated = false ;
145- this .dynamoClient = AmazonDynamoDBClientBuilder
146- .standard ()
147- .withRegion (System .getenv (LambdaEnvVariables .AWS_REGION ))
128+ this .dynamoClient = DynamoDbClient .builder ()
129+ .region (Region .of (System .getenv (LambdaEnvVariables .AWS_REGION )))
148130 .build ();
149-
150- this .dynamoDB = new DynamoDB (dynamoClient );
151131 }
152132
153-
154- /**
155- * Creates dynamo db table. We check if the table exists before creating one.
156- */
157133 private void createCacheTable (String tableName ) {
158134 LOG .debug ("Attempting to create new cache table '{}'" , tableName );
159- var attributeDefinitions = new ArrayList <AttributeDefinition >();
160- attributeDefinitions .add (new AttributeDefinition ()
161- .withAttributeName (HASH_ID_KEY )
162- .withAttributeType ("S" )
163- );
164-
165- var keySchema = new ArrayList <KeySchemaElement >();
166- keySchema .add (new KeySchemaElement ()
167- .withAttributeName (HASH_ID_KEY )
168- .withKeyType (KeyType .HASH )
169- );
170-
171- var createTableRequest = new CreateTableRequest ()
172- .withTableName (tableName )
173- .withKeySchema (keySchema )
174- .withAttributeDefinitions (attributeDefinitions )
175- .withProvisionedThroughput (new ProvisionedThroughput (0L ,0L ));
176-
177- Table table = this .dynamoDB .createTable (createTableRequest );
135+ CreateTableRequest createTableRequest = CreateTableRequest .builder ()
136+ .tableName (tableName )
137+ .keySchema (KeySchemaElement .builder ()
138+ .attributeName (HASH_ID_KEY )
139+ .keyType (KeyType .HASH )
140+ .build ())
141+ .attributeDefinitions (AttributeDefinition .builder ()
142+ .attributeName (HASH_ID_KEY )
143+ .attributeType (ScalarAttributeType .S )
144+ .build ())
145+ .provisionedThroughput (ProvisionedThroughput .builder ()
146+ .readCapacityUnits (0L )
147+ .writeCapacityUnits (0L )
148+ .build ())
149+ .build ();
150+
151+ dynamoClient .createTable (createTableRequest );
178152 try {
179153 LOG .debug ("Waiting for table status to be active..." );
180- table . waitForActive ( );
181- } catch (InterruptedException e ) {
182- // nothing
154+ dynamoClient . waiter (). waitUntilTableExists ( r -> r . tableName ( tableName ) );
155+ } catch (Exception e ) {
156+ // Handle exception
183157 }
184-
185- //
186158 }
187159
188- /**
189- * DEBUG FUNCTION - will be changed or deprecated in the future.
190- * @param tableName - name of the table
191- * @throws InterruptedException - exception
192- */
193160 public void deleteTable (String tableName ) throws InterruptedException {
194-
195- if (!this .doesTableExist (tableName )) {
161+ if (!doesTableExist (tableName )) {
196162 LOG .debug ("Table does not exist so we do not need to delete it...." );
197163 return ;
198164 }
199165
200- var table = dynamoDB .getTable (tableName );
201- table .delete ();
202- table .waitForDelete ();
166+ dynamoClient .deleteTable (DeleteTableRequest .builder ().tableName (tableName ).build ());
167+ dynamoClient .waiter ().waitUntilTableNotExists (r -> r .tableName (tableName ));
203168 }
204169
205- /**
206- * Checks to see if the table exists.
207- *
208- * @param tableName - name of the table
209- * @return - returns true if the table exists
210- */
211170 public boolean doesTableExist (String tableName ) {
212- var tables = this . dynamoClient .listTables (TABLE_LIST_LIMIT );
213- return tables .getTableNames ().contains (tableName );
171+ ListTablesResponse tables = dynamoClient .listTables (ListTablesRequest . builder (). limit ( TABLE_LIST_LIMIT ). build () );
172+ return tables .tableNames ().contains (tableName );
214173 }
215-
216-
217174}
0 commit comments