├── .gitignore
├── .travis.yml
├── CHANGELOG.md
├── LICENSE
├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── dasburo
│ │ └── spring
│ │ └── cache
│ │ └── dynamo
│ │ ├── DefaultDynamoCacheWriter.java
│ │ ├── DynamoCache.java
│ │ ├── DynamoCacheBuilder.java
│ │ ├── DynamoCacheConfiguration.java
│ │ ├── DynamoCacheManager.java
│ │ ├── DynamoCacheWriter.java
│ │ ├── autoconfigure
│ │ ├── DynamoCacheAutoConfiguration.java
│ │ ├── DynamoCacheProperties.java
│ │ └── DynamoCachePropertiesList.java
│ │ ├── rootattribute
│ │ ├── RootAttribute.java
│ │ ├── RootAttributeConfig.java
│ │ └── RootAttributeReader.java
│ │ ├── serializer
│ │ ├── DynamoSerializer.java
│ │ ├── GZipSerializer.java
│ │ ├── Jackson2JsonSerializer.java
│ │ ├── OxmSerializer.java
│ │ ├── SerializableSerializer.java
│ │ ├── SerializationException.java
│ │ ├── SerializationUtils.java
│ │ └── StringSerializer.java
│ │ └── util
│ │ ├── ByteUtils.java
│ │ └── TableUtils.java
└── resources
│ └── META-INF
│ └── spring.factories
└── test
└── java
└── com
└── dasburo
└── spring
└── cache
└── dynamo
├── DynamoCacheManagerTest.java
├── DynamoCacheTest.java
├── TestConfiguration.java
├── TestDbCreationExtension.java
├── UnitTestBase.java
├── autoconfigure
└── DynamoCacheAutoConfigurationTest.java
├── helper
├── Address.java
├── Company.java
└── NotSerializeable.java
├── rootattribute
├── RootAttributeConfigTest.java
├── RootAttributeReaderTest.java
└── SampleTestClass.java
├── serializer
├── GZipSerializerTest.java
├── Jackson2JsonSerializerTest.java
├── OxmSerializerTest.java
├── SerializableSerializerTest.java
├── SerializationUtilsTest.java
└── StringSerializerTest.java
└── util
└── TableUtilsTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | # Created by https://www.gitignore.io/api/osx,java,linux,maven,windows,intellij,jetbrains+all
3 | # Edit at https://www.gitignore.io/?templates=osx,java,linux,maven,windows,intellij,jetbrains+all
4 |
5 | ### Intellij ###
6 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
7 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
8 |
9 | # User-specific stuff
10 | .idea/**/workspace.xml
11 | .idea/**/tasks.xml
12 | .idea/**/usage.statistics.xml
13 | .idea/**/dictionaries
14 | .idea/**/shelf
15 |
16 | # Generated files
17 | .idea/**/contentModel.xml
18 | native-libs/**
19 |
20 | # Sensitive or high-churn files
21 | .idea/**/dataSources/
22 | .idea/**/dataSources.ids
23 | .idea/**/dataSources.local.xml
24 | .idea/**/sqlDataSources.xml
25 | .idea/**/dynamic.xml
26 | .idea/**/uiDesigner.xml
27 | .idea/**/dbnavigator.xml
28 |
29 | # Gradle
30 | .idea/**/gradle.xml
31 | .idea/**/libraries
32 |
33 | # Gradle and Maven with auto-import
34 | # When using Gradle or Maven with auto-import, you should exclude module files,
35 | # since they will be recreated, and may cause churn. Uncomment if using
36 | # auto-import.
37 | # .idea/modules.xml
38 | # .idea/*.iml
39 | # .idea/modules
40 | # *.iml
41 | # *.ipr
42 |
43 | # CMake
44 | cmake-build-*/
45 |
46 | # File-based project format
47 | *.iws
48 |
49 | # IntelliJ
50 | out/
51 |
52 | # mpeltonen/sbt-idea plugin
53 | .idea_modules/
54 |
55 | # JIRA plugin
56 | atlassian-ide-plugin.xml
57 |
58 | # Cursive Clojure plugin
59 | .idea/replstate.xml
60 |
61 | # Crashlytics plugin (for Android Studio and IntelliJ)
62 | com_crashlytics_export_strings.xml
63 | crashlytics.properties
64 | crashlytics-build.properties
65 | fabric.properties
66 |
67 | # Editor-based Rest Client
68 | .idea/httpRequests
69 |
70 | # Android studio 3.1+ serialized cache file
71 | .idea/caches/build_file_checksums.ser
72 |
73 | ### Intellij Patch ###
74 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
75 |
76 | # *.iml
77 | # modules.xml
78 | # .idea/misc.xml
79 | # *.ipr
80 |
81 | # Sonarlint plugin
82 | .idea/sonarlint
83 |
84 | ### Java ###
85 | # Compiled class file
86 | *.class
87 |
88 | # Log file
89 | *.log
90 |
91 | # BlueJ files
92 | *.ctxt
93 |
94 | # Mobile Tools for Java (J2ME)
95 | .mtj.tmp/
96 |
97 | # Package Files #
98 | *.jar
99 | *.war
100 | *.nar
101 | *.ear
102 | *.zip
103 | *.tar.gz
104 | *.rar
105 |
106 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
107 | hs_err_pid*
108 |
109 | ### JetBrains+all ###
110 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
111 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
112 |
113 | # User-specific stuff
114 |
115 | # Generated files
116 |
117 | # Sensitive or high-churn files
118 |
119 | # Gradle
120 |
121 | # Gradle and Maven with auto-import
122 | # When using Gradle or Maven with auto-import, you should exclude module files,
123 | # since they will be recreated, and may cause churn. Uncomment if using
124 | # auto-import.
125 | # .idea/modules.xml
126 | # .idea/*.iml
127 | # .idea/modules
128 | # *.iml
129 | # *.ipr
130 |
131 | # CMake
132 |
133 | # File-based project format
134 |
135 | # IntelliJ
136 |
137 | # mpeltonen/sbt-idea plugin
138 |
139 | # JIRA plugin
140 |
141 | # Cursive Clojure plugin
142 |
143 | # Crashlytics plugin (for Android Studio and IntelliJ)
144 |
145 | # Editor-based Rest Client
146 |
147 | # Android studio 3.1+ serialized cache file
148 |
149 | ### JetBrains+all Patch ###
150 | # Ignores the whole .idea folder and all .iml files
151 | # See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360
152 |
153 | .idea/
154 |
155 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023
156 |
157 | *.iml
158 | modules.xml
159 | .idea/misc.xml
160 | *.ipr
161 |
162 | # Sonarlint plugin
163 |
164 | ### Linux ###
165 | *~
166 |
167 | # temporary files which can be created if a process still has a handle open of a deleted file
168 | .fuse_hidden*
169 |
170 | # KDE directory preferences
171 | .directory
172 |
173 | # Linux trash folder which might appear on any partition or disk
174 | .Trash-*
175 |
176 | # .nfs files are created when an open file is removed but is still being accessed
177 | .nfs*
178 |
179 | ### Maven ###
180 | target/
181 | pom.xml.tag
182 | pom.xml.releaseBackup
183 | pom.xml.versionsBackup
184 | pom.xml.next
185 | release.properties
186 | dependency-reduced-pom.xml
187 | buildNumber.properties
188 | .mvn/timing.properties
189 | .mvn/wrapper/maven-wrapper.jar
190 |
191 | ### OSX ###
192 | # General
193 | .DS_Store
194 | .AppleDouble
195 | .LSOverride
196 |
197 | # Icon must end with two \r
198 | Icon
199 |
200 | # Thumbnails
201 | ._*
202 |
203 | # Files that might appear in the root of a volume
204 | .DocumentRevisions-V100
205 | .fseventsd
206 | .Spotlight-V100
207 | .TemporaryItems
208 | .Trashes
209 | .VolumeIcon.icns
210 | .com.apple.timemachine.donotpresent
211 |
212 | # Directories potentially created on remote AFP share
213 | .AppleDB
214 | .AppleDesktop
215 | Network Trash Folder
216 | Temporary Items
217 | .apdisk
218 |
219 | ### Windows ###
220 | # Windows thumbnail cache files
221 | Thumbs.db
222 | Thumbs.db:encryptable
223 | ehthumbs.db
224 | ehthumbs_vista.db
225 |
226 | # Dump file
227 | *.stackdump
228 |
229 | # Folder config file
230 | [Dd]esktop.ini
231 |
232 | # Recycle Bin used on file shares
233 | $RECYCLE.BIN/
234 |
235 | # Windows Installer files
236 | *.cab
237 | *.msi
238 | *.msix
239 | *.msm
240 | *.msp
241 |
242 | # Windows shortcuts
243 | *.lnk
244 |
245 | # End of https://www.gitignore.io/api/osx,java,linux,maven,windows,intellij,jetbrains+all
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | jdk:
3 | - openjdk8
4 | script:
5 | - mvn test-compile && mvn test jacoco:report
6 | after_success:
7 | - mvn coveralls:report
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 | All notable changes to this project will be documented in this file.
3 |
4 | The format is based on [Keep a Changelog](http://keepachangelog.com/)
5 | and this project adheres to [Semantic Versioning](http://semver.org/).
6 |
7 | # v2
8 |
9 | ## [Unreleased]
10 | ### Changed
11 | - check TTL for stored items, as items that have expired may still appear
12 |
13 | ## [2.0.0] - 2022-08-17
14 | ### Changed
15 | - use AWS Java SDK v2.x [@derXear](https://github.com/derXear)
16 | - update Spring Boot dependencies [@derXear](https://github.com/derXear)
17 |
18 | # v1
19 |
20 | ## [0.9.6] - 2020-11-13
21 | ### Changed
22 | - add getter for `DynamoCacheWriter` and `DynamoSerializer` in `DynamoCache` [@derXear](https://github.com/derXear)
23 | - update Amazon DynamoDB dependencies [@derXear](https://github.com/derXear)
24 |
25 | ## [0.9.5] - 2020-03-12
26 | ### Changed
27 | - the `DynamoDB::CreateTable` permission is not required anymore if the table already exist,
28 | instead at least `DynamoDB::DescribeTable` is necessary [@derXear](https://github.com/derXear)
29 |
30 | ## [0.9.4] - 2020-01-14
31 | ### Fixed
32 | - fix unit used for ttl from EpochMillis to EpochSeconds [@dnltsk](https://github.com/dnltsk)
33 |
34 | ## [0.9.3] - 2019-12-18
35 | ### Fixed
36 | - fix datatype used for ttl [@dnltsk](https://github.com/dnltsk)
37 |
38 | ## [0.9.2] - 2019-12-04
39 | ### Changed
40 | - fix javadoc and maven-gpg-plugin configuration by [@derXear](https://github.com/derXear)
41 |
42 | ## [0.9.1] - 2019-09-09
43 | ### Fixed
44 | - fix locking DefaultDynamoCacheWriter by [@derXear](https://github.com/derXear)
45 |
46 | ## [0.9.0] - 2019-09-06
47 | ### Added
48 | - initial code base by [@derXear](https://github.com/derXear)
49 |
50 | [Unreleased]: https://github.com/bad-opensource/spring-cache-dynamodb/compare/v2.0.0...HEAD
51 | [2.0.0]: https://github.com/bad-opensource/spring-cache-dynamodb/releases/tag/v2.0.0
52 | [0.9.6]: https://github.com/bad-opensource/spring-cache-dynamodb/releases/tag/v0.9.6
53 | [0.9.5]: https://github.com/bad-opensource/spring-cache-dynamodb/releases/tag/v0.9.5
54 | [0.9.4]: https://github.com/bad-opensource/spring-cache-dynamodb/releases/tag/v0.9.4
55 | [0.9.3]: https://github.com/bad-opensource/spring-cache-dynamodb/releases/tag/v0.9.3
56 | [0.9.2]: https://github.com/bad-opensource/spring-cache-dynamodb/releases/tag/v0.9.2
57 | [0.9.1]: https://github.com/bad-opensource/spring-cache-dynamodb/releases/tag/v0.9.1
58 | [0.9.0]: https://github.com/bad-opensource/spring-cache-dynamodb/releases/tag/v0.9.0
59 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright 2019 Das Büro am Draht GmbH
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # spring-cache-dynamodb
2 |
3 | [![Apache 2.0 License][license-image]][license-url]
4 | [](https://www.travis-ci.com/das-buro-am-draht/spring-cache-dynamodb)
5 | [](https://coveralls.io/github/das-buro-am-draht/spring-cache-dynamodb)
6 |
7 | > Spring Cache implementation based on Amazon DynamoDB
8 |
9 | ## Install
10 |
11 | To integrate this Git repository into your project, simply add the maven dependency
12 |
13 | ```xml
14 |
15 | com.dasburo
16 | spring-cache-dynamodb
17 | 2.0.0
18 |
19 | ```
20 | This release is using the AWS Java SDK v2.x. If you must use v1.x, please use a version prior to 2.0.0 of this library.
21 |
22 | ## Usage
23 |
24 | ### Quick start
25 |
26 | There is an autoconfiguration class that will set up a simple key-value cache for you, provided you have specified the following properties.
27 |
28 | #### Properties
29 |
30 | ```properties
31 | # TTL (in seconds). Default is Duration.ZERO and disables TTL.
32 | spring.cache.dynamo.caches[0].ttl = 10s
33 |
34 | # Cache name for the @Cacheable annotation.
35 | spring.cache.dynamo.caches[0].cacheName = myCache
36 |
37 | # Value that indicates if the cache must be flushed on application start.
38 | spring.cache.dynamo.caches[0].flushOnBoot = true
39 | ```
40 |
41 | #### YAML
42 |
43 | ```yaml
44 | spring:
45 | cache:
46 | dynamo:
47 | caches:
48 | - # TTL.
49 | ttl: 10s
50 | # Cache name for the @Cacheable annotation.
51 | cacheName: myCache
52 | # Value that indicates if the cache table must be flushed when the application starts.
53 | flushOnBoot: true
54 | ```
55 |
56 | ### Custom configuration
57 |
58 | To customize the creation of a cache manager, create a Java Bean in a [configuration class](http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-configuration-classes.html):
59 |
60 | ```java
61 | @Bean
62 | public AwsCredentials awsCredentials() {
63 | return AwsBasicCredentials.create(amazonAWSAccessKey, amazonAWSSecretKey);
64 | }
65 |
66 | @Bean
67 | public AwsCredentialsProvider awsCredentialsProvider(AwsCredentials awsCredentials) {
68 | return StaticCredentialsProvider.create(awsCredentials);
69 | }
70 |
71 | @Bean
72 | public DynamoDbClient amazonDynamoDB(AwsCredentialsProvider awsCredentialsProvider) {
73 | return DynamoDbClient.builder()
74 | .credentialsProvider(awsCredentialsProvider)
75 | .region(Region.of(amazonAWSRegion))
76 | .build();
77 | }
78 |
79 | @Bean
80 | public CacheManager cacheManager(DynamoDbClient ddb) {
81 | List cacheBuilders = new ArrayList<>();
82 | cacheBuilders.add(DynamoCacheBuilder.newInstance(cacheName, ddb)
83 | .withTTL(Duration.ofSeconds(cacheTtl)));
84 |
85 | return new DynamoCacheManager(cacheBuilders);
86 | }
87 | ```
88 |
89 | #### Serializers
90 |
91 | By default, the included `StringSerializer` is used. But it's also possible to define a custom Serializer
92 | of type `DynamoSerializer` for each cache.
93 |
94 | ### How to use the cache?
95 |
96 | #### @Cacheable
97 |
98 | After the cache has been configured, you can use the `Cacheable` [annotation](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html).
99 | In the following example, the cache "myCache" is used like this:
100 |
101 | ```java
102 | @Cacheable(value = "myCache", key = "#id")
103 | public Data getData(String id) {
104 | // ...
105 | }
106 | ```
107 |
108 | The `id` parameter is used as document identifier.
109 | Note that the cache key must be of type `java.lang.String`.
110 | It is also possible to use [SpEL](https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions-beandef-annotation-based) to generate a combined key.
111 |
112 | The `Data` object will be stored in a DynamoDB table for future use (as the TTL has not expired).
113 | Note that cache elements must be serializable (i.e. implement `java.io.Serializable`).
114 |
115 | ## License
116 |
117 | Spring Cache DynamoDB is Open Source software released under the [Apache 2.0 license](https://www.apache.org/licenses/LICENSE-2.0.html).
118 |
119 | [license-image]: https://img.shields.io/badge/license-Apache%202-blue.svg
120 | [license-url]: http://www.apache.org/licenses/LICENSE-2.0
121 | [travis-image]: https://travis-ci.com/bad-opensource/spring-cache-dynamodb.svg?branch=master
122 | [travis-url]: https://travis-ci.com/bad-opensource/spring-cache-dynamodb
123 | [coveralls-image]: https://coveralls.io/repos/github/bad-opensource/spring-cache-dynamodb/badge.svg
124 | [coveralls-url]: https://coveralls.io/github/bad-opensource/spring-cache-dynamodb
125 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.7.2
9 |
10 |
11 |
12 | com.dasburo
13 | spring-cache-dynamodb
14 | 2.0.0
15 |
16 | Amazon DynamoDB for Spring Cache
17 | Spring Cache implementation based on Amazon DynamoDB
18 | https://github.com/bad-opensource/spring-cache-dynamodb
19 | 2019
20 |
21 | Das Büro am Draht GmbH
22 | https://www.dasburo.com/
23 |
24 |
25 |
26 | Apache 2.0 License
27 | http://www.apache.org/licenses/LICENSE-2.0
28 | repo
29 |
30 |
31 |
32 |
33 |
34 | derXear
35 | Georg Zimmermann
36 | Das Büro am Draht GmbH
37 | https://www.dasburo.com/
38 |
39 | Software Developer
40 |
41 |
42 |
43 |
44 |
45 |
46 | scm:git:https://github.com/bad-opensource/spring-cache-dynamodb.git
47 | scm:git:https://github.com/bad-opensource/spring-cache-dynamodb.git
48 | https://github.com/bad-opensource/spring-cache-dynamodb
49 | spring-cache-dynamodb-1.0.0
50 |
51 |
52 | GitHub Issues
53 | https://github.com/bad-opensource/spring-cache-dynamodb/issues
54 |
55 |
56 |
57 |
58 | ossrh
59 | https://oss.sonatype.org/content/repositories/snapshots
60 |
61 |
62 |
63 |
64 | UTF-8
65 | UTF-8
66 | UTF-8
67 | 1.8
68 | 1.8
69 | 1.8
70 | 1.8
71 | github
72 |
73 | 1.16.0
74 | 2.17.253
75 |
76 |
77 |
78 |
79 |
80 | software.amazon.awssdk
81 | bom
82 | ${version.awssdk}
83 | pom
84 | import
85 |
86 |
87 | com.amazonaws
88 | DynamoDBLocal
89 | ${version.dynamodb.local}
90 | test
91 |
92 |
93 |
94 |
95 |
96 |
97 | org.springframework.boot
98 | spring-boot-autoconfigure
99 |
100 |
101 | org.springframework.boot
102 | spring-boot-starter-test
103 |
104 |
105 | org.springframework
106 | spring-tx
107 |
108 |
109 | org.springframework
110 | spring-oxm
111 |
112 |
113 | software.amazon.awssdk
114 | dynamodb
115 |
116 |
117 |
118 | com.fasterxml.jackson.core
119 | jackson-databind
120 |
121 |
122 | commons-beanutils
123 | commons-beanutils
124 | 1.9.4
125 |
126 |
127 | com.amazonaws
128 | DynamoDBLocal
129 | test
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 | org.apache.maven.plugins
138 | maven-dependency-plugin
139 | 2.10
140 |
141 |
142 | copy
143 | test-compile
144 |
145 | copy-dependencies
146 |
147 |
148 | test
149 | so,dll,dylib
150 | ${project.basedir}/native-libs
151 |
152 |
153 |
154 |
155 |
156 | org.jacoco
157 | jacoco-maven-plugin
158 | 0.8.8
159 |
160 |
161 | prepare-agent
162 |
163 | prepare-agent
164 |
165 |
166 |
167 |
168 |
169 | org.eluder.coveralls
170 | coveralls-maven-plugin
171 | 4.3.0
172 |
173 |
174 | org.sonatype.plugins
175 | nexus-staging-maven-plugin
176 | 1.6.13
177 | true
178 |
179 | ossrh
180 | https://oss.sonatype.org/
181 | true
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 | ossrh
190 |
191 | true
192 |
193 |
194 |
195 | release
196 |
197 |
198 |
199 | org.apache.maven.plugins
200 | maven-release-plugin
201 | 2.5.3
202 |
203 | true
204 | false
205 | release
206 | deploy
207 |
208 |
209 |
210 | org.apache.maven.plugins
211 | maven-source-plugin
212 |
213 |
214 | attach-sources
215 |
216 | jar-no-fork
217 |
218 |
219 |
220 |
221 |
222 | org.apache.maven.plugins
223 | maven-javadoc-plugin
224 |
225 |
226 | attach-javadocs
227 |
228 | jar
229 |
230 |
231 |
232 |
233 |
234 | org.apache.maven.plugins
235 | maven-gpg-plugin
236 | 1.6
237 |
238 |
239 | sign-artifacts
240 | verify
241 |
242 | sign
243 |
244 |
245 |
246 | --pinentry-mode
247 | loopback
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 | dynamodb-local-frankfurt
260 | DynamoDB Local Release Repository
261 | https://s3.eu-central-1.amazonaws.com/dynamodb-local-frankfurt/release
262 |
263 |
264 |
265 |
--------------------------------------------------------------------------------
/src/main/java/com/dasburo/spring/cache/dynamo/DefaultDynamoCacheWriter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.dasburo.spring.cache.dynamo;
17 |
18 | import com.dasburo.spring.cache.dynamo.rootattribute.RootAttribute;
19 | import com.dasburo.spring.cache.dynamo.util.TableUtils;
20 | import org.springframework.dao.PessimisticLockingFailureException;
21 | import org.springframework.lang.Nullable;
22 | import org.springframework.util.Assert;
23 | import software.amazon.awssdk.core.SdkBytes;
24 | import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
25 | import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
26 | import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
27 | import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest;
28 | import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest;
29 | import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest;
30 | import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
31 | import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;
32 | import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement;
33 | import software.amazon.awssdk.services.dynamodb.model.KeyType;
34 | import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput;
35 | import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
36 | import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException;
37 | import software.amazon.awssdk.services.dynamodb.model.TimeToLiveSpecification;
38 | import software.amazon.awssdk.services.dynamodb.model.UpdateTimeToLiveRequest;
39 |
40 | import java.time.Duration;
41 | import java.time.Instant;
42 | import java.util.Collections;
43 | import java.util.HashMap;
44 | import java.util.List;
45 | import java.util.Map;
46 | import java.util.NoSuchElementException;
47 | import java.util.Objects;
48 | import java.util.function.Function;
49 |
50 | import static software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType.S;
51 |
52 | /**
53 | * {@link DynamoCacheWriter} implementation capable of reading/writing binary data from/to DynamoDB in {@literal standalone}
54 | * and {@literal cluster} environments. Works upon a given {@link DynamoDbClient} holds the actual connection.
55 | *
56 | * {@link DefaultDynamoCacheWriter} can be used in
57 | * {@link DynamoCacheWriter#lockingDynamoCacheWriter(DynamoDbClient) locking} or
58 | * {@link DynamoCacheWriter#nonLockingDynamoCacheWriter(DynamoDbClient) non-locking} mode. While
59 | * {@literal non-locking} aims for maximum performance it may result in overlapping, non-atomic, command execution for
60 | * operations spanning multiple DynamoDB interactions like {@code putIfAbsent}. The {@literal locking} counterpart prevents
61 | * command overlap by setting an explicit lock key and checking against presence of this key which leads to additional
62 | * requests and potential command wait times.
63 | *
64 | * @author Georg Zimmermann
65 | */
66 | public class DefaultDynamoCacheWriter implements DynamoCacheWriter {
67 |
68 | public static final String ATTRIBUTE_KEY = "key";
69 | public static final String ATTRIBUTE_VALUE = "value";
70 | public static final String ATTRIBUTE_TTL = "ttl";
71 |
72 | private final DynamoDbClient dynamoTemplate;
73 | private final Duration sleepTime;
74 |
75 | /**
76 | * @param dynamoTemplate must not be {@literal null}.
77 | */
78 | DefaultDynamoCacheWriter(DynamoDbClient dynamoTemplate) {
79 | this(dynamoTemplate, Duration.ZERO);
80 | }
81 |
82 | /**
83 | * @param dynamoTemplate must not be {@literal null}.
84 | * @param sleepTime sleep time between lock request attempts. Must not be {@literal null}. Use {@link Duration#ZERO}
85 | * to disable locking.
86 | */
87 | DefaultDynamoCacheWriter(DynamoDbClient dynamoTemplate, Duration sleepTime) {
88 | Assert.notNull(dynamoTemplate, "ConnectionFactory must not be null!");
89 | Assert.notNull(sleepTime, "SleepTime must not be null!");
90 |
91 | this.dynamoTemplate = dynamoTemplate;
92 | this.sleepTime = sleepTime;
93 | }
94 |
95 | @Override
96 | public DynamoDbClient getNativeCacheWriter() {
97 | return dynamoTemplate;
98 | }
99 |
100 | @Override
101 | public void put(String name, String key, byte[] value, @Nullable Duration ttl, @Nullable List rootAttributes) {
102 | Assert.notNull(name, "Name must not be null!");
103 | Assert.notNull(key, "Key must not be null!");
104 |
105 | execute(name, connection -> {
106 | putInternal(name, key, value, ttl, rootAttributes);
107 |
108 | return "OK";
109 | });
110 | }
111 |
112 | @Override
113 | public byte[] get(String name, String key) {
114 | Assert.notNull(name, "Name must not be null!");
115 | Assert.notNull(key, "Key must not be null!");
116 |
117 | return execute(name, connection -> getInternal(name, key));
118 | }
119 |
120 | @Override
121 | public byte[] putIfAbsent(String name, String key, @Nullable byte[] value, @Nullable Duration ttl, @Nullable List rootAttributes) {
122 | Assert.notNull(name, "Name must not be null!");
123 | Assert.notNull(key, "Key must not be null!");
124 |
125 | return execute(name, connection -> {
126 |
127 | if (isLockingCacheWriter()) {
128 | doLock(name);
129 | }
130 |
131 | try {
132 | return getInternal(name, key);
133 | } catch (NoSuchElementException e) {
134 | putInternal(name, key, value, ttl, rootAttributes);
135 | } finally {
136 | if (isLockingCacheWriter()) {
137 | doUnlock(name);
138 | }
139 | }
140 | return null;
141 | });
142 | }
143 |
144 | @Override
145 | public void remove(String name, String key) {
146 | Assert.notNull(name, "Name must not be null!");
147 | Assert.notNull(key, "Key must not be null!");
148 |
149 | execute(name, connection -> {
150 | removeInternal(name, key);
151 | return "OK";
152 | });
153 | }
154 |
155 | @Override
156 | public void clear(String name) {
157 | Assert.notNull(name, "Name must not be null!");
158 |
159 | execute(name, connection -> {
160 | try {
161 | if (isLockingCacheWriter()) {
162 | doLock(name);
163 | }
164 |
165 | List