35
35
*/
36
36
@ Slf4j
37
37
public class CacheStore {
38
+ /**
39
+ * Cach capacity.
40
+ */
38
41
private static final int CAPACITY =3 ;
39
42
43
+ /**
44
+ * Lru cache see {@link LruCache}.
45
+ */
40
46
private static LruCache cache ;
47
+ /**
48
+ * DbManager.
49
+ */
41
50
private DbManager dbManager ;
42
51
43
- public CacheStore (DbManager dbManager ) {
44
- this .dbManager =dbManager ;
52
+ /**
53
+ * Cache Store.
54
+ * @param dataBaseManager {@link DbManager}
55
+ */
56
+ public CacheStore (final DbManager dataBaseManager ) {
57
+ this .dbManager =dataBaseManager ;
45
58
initCapacity (CAPACITY );
46
59
}
47
60
48
61
/**
49
62
* Init cache capacity.
63
+ * @param capacity int
50
64
*/
51
- public void initCapacity (int capacity ) {
65
+ public void initCapacity (final int capacity ) {
52
66
if (cache ==null ) {
53
67
cache =new LruCache (capacity );
54
68
}else {
@@ -58,8 +72,10 @@ public void initCapacity(int capacity) {
58
72
59
73
/**
60
74
* Get user account using read-through cache.
75
+ * @param userId {@link String}
76
+ * @return {@link UserAccount}
61
77
*/
62
- public UserAccount readThrough (String userId ) {
78
+ public UserAccount readThrough (final String userId ) {
63
79
if (cache .contains (userId )) {
64
80
LOGGER .info ("# Found in Cache!" );
65
81
return cache .get (userId );
@@ -72,8 +88,9 @@ public UserAccount readThrough(String userId) {
72
88
73
89
/**
74
90
* Get user account using write-through cache.
91
+ * @param userAccount {@link UserAccount}
75
92
*/
76
- public void writeThrough (UserAccount userAccount ) {
93
+ public void writeThrough (final UserAccount userAccount ) {
77
94
if (cache .contains (userAccount .getUserId ())) {
78
95
dbManager .updateDb (userAccount );
79
96
}else {
@@ -84,11 +101,13 @@ public void writeThrough(UserAccount userAccount) {
84
101
85
102
/**
86
103
* Get user account using write-around cache.
104
+ * @param userAccount {@link UserAccount}
87
105
*/
88
- public void writeAround (UserAccount userAccount ) {
106
+ public void writeAround (final UserAccount userAccount ) {
89
107
if (cache .contains (userAccount .getUserId ())) {
90
108
dbManager .updateDb (userAccount );
91
- cache .invalidate (userAccount .getUserId ());// Cache data has been updated -- remove older
109
+ // Cache data has been updated -- remove older
110
+ cache .invalidate (userAccount .getUserId ());
92
111
// version from cache.
93
112
}else {
94
113
dbManager .writeToDb (userAccount );
@@ -97,8 +116,10 @@ public void writeAround(UserAccount userAccount) {
97
116
98
117
/**
99
118
* Get user account using read-through cache with write-back policy.
119
+ * @param userId {@link String}
120
+ * @return {@link UserAccount}
100
121
*/
101
- public UserAccount readThroughWithWriteBackPolicy (String userId ) {
122
+ public UserAccount readThroughWithWriteBackPolicy (final String userId ) {
102
123
if (cache .contains (userId )) {
103
124
LOGGER .info ("# Found in cache!" );
104
125
return cache .get (userId );
@@ -116,8 +137,9 @@ public UserAccount readThroughWithWriteBackPolicy(String userId) {
116
137
117
138
/**
118
139
* Set user account.
140
+ * @param userAccount {@link UserAccount}
119
141
*/
120
- public void writeBehind (UserAccount userAccount ) {
142
+ public void writeBehind (final UserAccount userAccount ) {
121
143
if (cache .isFull () && !cache .contains (userAccount .getUserId ())) {
122
144
LOGGER .info ("# Cache is FULL! Writing LRU data to DB..." );
123
145
UserAccount toBeWrittenToDb =cache .getLruData ();
@@ -148,6 +170,7 @@ public void flushCache() {
148
170
149
171
/**
150
172
* Print user accounts.
173
+ * @return {@link String}
151
174
*/
152
175
public String print () {
153
176
return Optional .ofNullable (cache )
@@ -160,22 +183,27 @@ public String print() {
160
183
161
184
/**
162
185
* Delegate to backing cache store.
186
+ * @param userId {@link String}
187
+ * @return {@link UserAccount}
163
188
*/
164
- public UserAccount get (String userId ) {
189
+ public UserAccount get (final String userId ) {
165
190
return cache .get (userId );
166
191
}
167
192
168
193
/**
169
194
* Delegate to backing cache store.
195
+ * @param userId {@link String}
196
+ * @param userAccount {@link UserAccount}
170
197
*/
171
- public void set (String userId ,UserAccount userAccount ) {
198
+ public void set (final String userId ,final UserAccount userAccount ) {
172
199
cache .set (userId ,userAccount );
173
200
}
174
201
175
202
/**
176
203
* Delegate to backing cache store.
204
+ * @param userId {@link String}
177
205
*/
178
- public void invalidate (String userId ) {
206
+ public void invalidate (final String userId ) {
179
207
cache .invalidate (userId );
180
208
}
181
209
}