30
30
import lombok .extern .slf4j .Slf4j ;
31
31
32
32
/**
33
- * Data structure/implementation of the application's cache. The data structure consists of a hash
34
- * table attached with a doubly linked-list. The linked-list helps in capturing and maintaining the
35
- * LRU data in the cache. When a data is queried (from the cache), added (to the cache), or updated,
36
- * the data is moved to the front of the list to depict itself as the most-recently-used data. The
37
- * LRU data is always at the end of the list.
33
+ * Data structure/implementation of the application's cache. The data structure
34
+ * consists of a hash table attached with a doubly linked-list. The linked-list
35
+ * helps in capturing and maintaining the LRU data in the cache. When a data is
36
+ * queried (from the cache), added (to the cache), or updated, the data is
37
+ * moved to the front of the list to depict itself as the most-recently-used
38
+ * data. The LRU data is always at the end of the list.
38
39
*/
39
40
@ Slf4j
40
41
public class LruCache {
41
-
42
+ /**
43
+ * Static class Node.
44
+ */
42
45
static class Node {
43
- String userId ;
44
- UserAccount userAccount ;
45
- Node previous ;
46
- Node next ;
47
-
48
- public Node (String userId , UserAccount userAccount ) {
49
- this .userId = userId ;
50
- this .userAccount = userAccount ;
46
+ /**
47
+ * user id.
48
+ */
49
+ private final String userId ;
50
+ /**
51
+ * User Account.
52
+ */
53
+ private UserAccount userAccount ;
54
+ /**
55
+ * previous.
56
+ */
57
+ private Node previous ;
58
+ /**
59
+ * next.
60
+ */
61
+ private Node next ;
62
+
63
+ /**
64
+ * Node definition.
65
+ * @param id String
66
+ * @param account {@link UserAccount}
67
+ */
68
+ Node (final String id , final UserAccount account ) {
69
+ this .userId = id ;
70
+ this .userAccount = account ;
51
71
}
52
72
}
53
73
54
- int capacity ;
55
- Map <String , Node > cache = new HashMap <>();
56
- Node head ;
57
- Node end ;
74
+ /**
75
+ * Capacity of Cache.
76
+ */
77
+ private int capacity ;
78
+ /**
79
+ * Cache {@link HashMap}.
80
+ */
81
+ private Map <String , Node > cache = new HashMap <>();
82
+ /**
83
+ * Head.
84
+ */
85
+ private Node head ;
86
+ /**
87
+ * End.
88
+ */
89
+ private Node end ;
58
90
59
- public LruCache (int capacity ) {
60
- this .capacity = capacity ;
91
+ /**
92
+ * Constructor.
93
+ * @param cap Integer.
94
+ */
95
+ public LruCache (final int cap ) {
96
+ this .capacity = cap ;
61
97
}
62
98
63
99
/**
64
100
* Get user account.
101
+ * @param userId String
102
+ * @return {@link UserAccount}
65
103
*/
66
- public UserAccount get (String userId ) {
104
+ public UserAccount get (final String userId ) {
67
105
if (cache .containsKey (userId )) {
68
106
var node = cache .get (userId );
69
107
remove (node );
@@ -75,8 +113,9 @@ public UserAccount get(String userId) {
75
113
76
114
/**
77
115
* Remove node from linked list.
116
+ * @param node {@link Node}
78
117
*/
79
- public void remove (Node node ) {
118
+ public void remove (final Node node ) {
80
119
if (node .previous != null ) {
81
120
node .previous .next = node .next ;
82
121
} else {
@@ -91,8 +130,9 @@ public void remove(Node node) {
91
130
92
131
/**
93
132
* Move node to the front of the list.
133
+ * @param node {@link Node}
94
134
*/
95
- public void setHead (Node node ) {
135
+ public void setHead (final Node node ) {
96
136
node .next = head ;
97
137
node .previous = null ;
98
138
if (head != null ) {
@@ -106,8 +146,10 @@ public void setHead(Node node) {
106
146
107
147
/**
108
148
* Set user account.
149
+ * @param userAccount {@link UserAccount}
150
+ * @param userId {@link String}
109
151
*/
110
- public void set (String userId , UserAccount userAccount ) {
152
+ public void set (final String userId , final UserAccount userAccount ) {
111
153
if (cache .containsKey (userId )) {
112
154
var old = cache .get (userId );
113
155
old .userAccount = userAccount ;
@@ -127,25 +169,40 @@ public void set(String userId, UserAccount userAccount) {
127
169
}
128
170
}
129
171
130
- public boolean contains (String userId ) {
172
+ /**
173
+ * Che if Cache cintains the userId.
174
+ * @param userId {@link String}
175
+ * @return boolean
176
+ */
177
+ public boolean contains (final String userId ) {
131
178
return cache .containsKey (userId );
132
179
}
133
180
134
- /**
135
- * Invalidate cache for user.
136
- */
137
- public void invalidate (String userId ) {
181
+ /**
182
+ * Invalidate cache for user.
183
+ * @param userId {@link String}
184
+ */
185
+ public void invalidate (final String userId ) {
138
186
var toBeRemoved = cache .remove (userId );
139
187
if (toBeRemoved != null ) {
140
- LOGGER .info ("# {} has been updated! Removing older version from cache..." , userId );
188
+ LOGGER .info ("# {} has been updated! "
189
+ + "Removing older version from cache..." , userId );
141
190
remove (toBeRemoved );
142
191
}
143
192
}
144
193
194
+ /**
195
+ * Is cache full?
196
+ * @return boolean
197
+ */
145
198
public boolean isFull () {
146
199
return cache .size () >= capacity ;
147
200
}
148
201
202
+ /**
203
+ * Get LRU data.
204
+ * @return {@link UserAccount}
205
+ */
149
206
public UserAccount getLruData () {
150
207
return end .userAccount ;
151
208
}
@@ -161,6 +218,7 @@ public void clear() {
161
218
162
219
/**
163
220
* Returns cache data in list form.
221
+ * @return {@link List}
164
222
*/
165
223
public List <UserAccount > getCacheDataInListForm () {
166
224
var listOfCacheData = new ArrayList <UserAccount >();
@@ -174,10 +232,13 @@ public List<UserAccount> getCacheDataInListForm() {
174
232
175
233
/**
176
234
* Set cache capacity.
235
+ * @param newCapacity int
177
236
*/
178
- public void setCapacity (int newCapacity ) {
237
+ public void setCapacity (final int newCapacity ) {
179
238
if (capacity > newCapacity ) {
180
- clear (); // Behavior can be modified to accommodate for decrease in cache size. For now, we'll
239
+ // Behavior can be modified to accommodate
240
+ // for decrease in cache size. For now, we'll
241
+ clear ();
181
242
// just clear the cache.
182
243
} else {
183
244
this .capacity = newCapacity ;
0 commit comments