@@ -95,4 +95,369 @@ spring:
95
95
prefix: ::state
96
96
` ` `
97
97
98
+ # ### Services
98
99
100
+ - `NgxRedisBaseService` : use service for base action on redis
101
+
102
+ ` ` ` java
103
+ /**
104
+ * @apiNote RedisKeys - using this class to get keys type, ex: RedisKeys.getSysConfigKey("test")
105
+ */
106
+ public interface NgxRedisBaseService {
107
+
108
+ /**
109
+ * Get list of basic objects of cache
110
+ *
111
+ * @param pattern string prefix
112
+ * @return object list
113
+ */
114
+ Collection<String> keys(RedisTemplate<String, Object> redisTemplate, String pattern);
115
+
116
+ /**
117
+ * Cache basic objects, integer, string, physical classes, etc.
118
+ *
119
+ * @param key Cache key value
120
+ * @param value Cache
121
+ */
122
+ <T> void setCacheObject(RedisTemplate<String, Object> redisTemplate, String key, T value);
123
+
124
+ /**
125
+ * Cache basic objects, integer, string, physical classes, etc.
126
+ *
127
+ * @param key Cache key value
128
+ * @param value Cache
129
+ * @param timeout time
130
+ * @param timeUnit Time Parts
131
+ */
132
+ <T> void setCacheObject(RedisTemplate<String, Object> redisTemplate, String key, T value, Integer timeout, TimeUnit timeUnit);
133
+
134
+ /**
135
+ * Set effective time
136
+ *
137
+ * @param key Redis button
138
+ * @param timeout timeout time
139
+ * @return true = set success; false = set failed
140
+ */
141
+ boolean expire(RedisTemplate<String, Object> redisTemplate, String key, long timeout);
142
+
143
+ /**
144
+ * Set effective time
145
+ *
146
+ * @param key Redis button
147
+ * @param timeout timeout time
148
+ * @param unit Time Unit
149
+ * @return true = set success; false = set failed
150
+ */
151
+
152
+ boolean expire(RedisTemplate<String, Object> redisTemplate, String key, long timeout, TimeUnit unit);
153
+
154
+
155
+ /**
156
+ * Get the basic object of the cache.
157
+ *
158
+ * @param key Cache key value
159
+ * @return Cache key value corresponding to data
160
+ */
161
+
162
+ <T> T getCacheObject(RedisTemplate<String, Object> redisTemplate, String key);
163
+
164
+ /**
165
+ * Delete a single object
166
+ *
167
+ * @param key -
168
+ */
169
+ boolean deleteObject(RedisTemplate<String, Object> redisTemplate, String key);
170
+
171
+ /**
172
+ * Delete set objects
173
+ *
174
+ * @param collection multiple objects
175
+ * @return integer
176
+ */
177
+
178
+ long deleteObject(RedisTemplate<String, Object> redisTemplate, Collection<String> collection);
179
+
180
+ /**
181
+ * Cache List data
182
+ *
183
+ * @param key Cache key value
184
+ * @param list worthy List data
185
+ * @return object
186
+ */
187
+ <T> long setCacheList(RedisTemplate<String, Object> redisTemplate, String key, List<T> list);
188
+
189
+ /**
190
+ * Get the List object of the cache
191
+ *
192
+ * @param key Cache key value
193
+ * @return Cache key value corresponding to data
194
+ */
195
+ <T> List<T> getCacheList(RedisTemplate<String, Object> redisTemplate, String key);
196
+
197
+ /**
198
+ * Cache SET
199
+ *
200
+ * @param key Cache key value
201
+ * @param dataSet Cache data
202
+ * @return object of cache data
203
+ */
204
+ <T> BoundSetOperations<String, T> setCacheSet(RedisTemplate<String, Object> redisTemplate, String key, Set<T> dataSet);
205
+
206
+ /**
207
+ * Get a cache SET
208
+ *
209
+ * @param key -
210
+ * @return -
211
+ */
212
+ <T> Set<T> getCacheSet(RedisTemplate<String, Object> redisTemplate, String key);
213
+
214
+ /**
215
+ * Cache MAP
216
+ *
217
+ * @param key -
218
+ * @param map -
219
+ */
220
+ <T> void setCacheMap(RedisTemplate<String, Object> redisTemplate, String key, Map<String, T> map);
221
+
222
+ /**
223
+ * Get the MAP of the cache
224
+ *
225
+ * @param key -
226
+ * @return -
227
+ */
228
+ Map<Object, Object> getCacheMap(RedisTemplate<String, Object> redisTemplate, String key);
229
+
230
+ /**
231
+ * Save data from Hash
232
+ *
233
+ * @param key Redis button
234
+ * @param hKey Hash button
235
+ * @param value Value
236
+ */
237
+ <T> void setCacheMapValue(RedisTemplate<String, Object> redisTemplate, String key, String hKey, T value);
238
+
239
+ /**
240
+ * Get data in hash object
241
+ *
242
+ * @param key Redis button
243
+ * @param hKey Hash button
244
+ * @return Hash objects
245
+ */
246
+ <T> T getCacheMapValue(RedisTemplate<String, Object> redisTemplate, String key, String hKey);
247
+
248
+ /**
249
+ * Get data in multiple HASH
250
+ *
251
+ * @param key Redis button
252
+ * @param hKeys Hash key collection
253
+ * @return hash object set
254
+ */
255
+ <T> List<T> getMultiCacheMapValue(RedisTemplate<String, Object> redisTemplate, String key, Collection<Object> hKeys);
256
+
257
+ Collection<String> keys(RedisTemplate<String, Object> redisTemplate);
258
+
259
+ boolean containsKey(RedisTemplate<String, Object> redisTemplate, String key);
260
+
261
+ <T> void publishEvent(RedisTemplate<String, Object> redisTemplate, ChannelTopic channelTopic, T data);
262
+
263
+ <T> void publishEvent(RedisTemplate<String, Object> redisTemplate, RedisPubSubType topic, T data);
264
+
265
+ <T> void publishEvent(RedisTemplate<String, Object> redisTemplate, RedisPubSubLabel topic, T data);
266
+
267
+ Long countExistingKeys(RedisTemplate<String, Object> redisTemplate, Collection<String> keys);
268
+
269
+ Boolean isAvailable(RedisTemplate<String, Object> redisTemplate);
270
+ }
271
+
272
+ ` ` `
273
+
274
+ - `NgxRedisStylesBaseService` : use service for all combine base `NgxRedisBaseService` for action on redis
275
+
276
+ ` ` ` java
277
+ @SuppressWarnings({"UnusedReturnValue"})
278
+ public interface NgxRedisStylesBaseService {
279
+
280
+ Collection<String> keys(String pattern);
281
+
282
+ Collection<String> keys();
283
+
284
+ <T> void setCacheObject(RedisStylesRequest redisStylesRequest, T value);
285
+
286
+ <T> void setCacheObject(RedisStylesRequest redisStylesRequest, T value, Integer timeout, TimeUnit timeUnit);
287
+
288
+ boolean expire(RedisStylesRequest redisStylesRequest, long timeout);
289
+
290
+ boolean expire(RedisStylesRequest redisStylesRequest, long timeout, TimeUnit unit);
291
+
292
+ <T> T getCacheObject(RedisStylesRequest redisStylesRequest);
293
+
294
+ boolean deleteObject(RedisStylesRequest redisStylesRequest);
295
+
296
+ long deleteObject(Collection<String> collection);
297
+
298
+ <T> long setCacheList(RedisStylesRequest redisStylesRequest, List<T> list);
299
+
300
+ <T> List<T> getCacheList(RedisStylesRequest redisStylesRequest);
301
+
302
+ <T> BoundSetOperations<String, T> setCacheSet(RedisStylesRequest redisStylesRequest, Set<T> dataSet);
303
+
304
+ <T> Set<T> getCacheSet(RedisStylesRequest redisStylesRequest);
305
+
306
+ <T> void setCacheMap(RedisStylesRequest redisStylesRequest, Map<String, T> map);
307
+
308
+ Map<Object, Object> getCacheMap(RedisStylesRequest redisStylesRequest);
309
+
310
+ <T> void setCacheMapValue(RedisStylesRequest redisStylesRequest, String hKey, T value);
311
+
312
+ <T> T getCacheMapValue(RedisStylesRequest redisStylesRequest, String hKey);
313
+
314
+ <T> List<T> getMultiCacheMapValue(RedisStylesRequest redisStylesRequest, Collection<Object> hKeys);
315
+
316
+ boolean containsKey(RedisStylesRequest redisStylesRequest);
317
+
318
+ Long countExistingKeys(Collection<String> keys);
319
+
320
+ <T> void publishEvent(ChannelTopic channelTopic, T data);
321
+
322
+ <T> void publishEvent(RedisPubSubType topic, T data);
323
+
324
+ <T> void publishEvent(RedisPubSubLabel topic, T data);
325
+
326
+ SIVAResponseDTO<?> takeValuesFKeys(RedisStylesRequest redisStylesRequest);
327
+
328
+ SIVAResponseDTO<?> takeValuesFKeys(String keyPref);
329
+
330
+ <T> SIVAResponseDTO<?> updateCacheObject(String keyPref, T value);
331
+
332
+ Boolean isAvailable();
333
+ }
334
+ ` ` `
335
+
336
+ - `NgxRedisUtils` : class utils for action on redis
337
+
338
+ ` ` ` java
339
+ @SuppressWarnings({"ConstantConditions", "rawtypes", "All"})
340
+ public class NgxRedisUtils {
341
+
342
+ public static Long increaseKey(RedisTemplate<String, Object> redisTemplate, String key) {
343
+ if (StringUtility.isEmpty(key)) {
344
+ return -1L;
345
+ }
346
+
347
+ return (long) redisTemplate.execute((RedisCallback) connection -> {
348
+ byte[] paramBytes = redisTemplate.getStringSerializer().serialize(key);
349
+ return connection.incr(paramBytes);
350
+ }, true);
351
+ }
352
+
353
+ public static Long increaseKey(RedisTemplate<String, Object> redisTemplate, RedisStylesRequest key) {
354
+ return increaseKey(redisTemplate, RedisStylesUtils.takeRedisKey(key));
355
+ }
356
+
357
+ public static Long decreaseKey(RedisTemplate<String, Object> redisTemplate, String key) {
358
+ if (StringUtility.isEmpty(key)) {
359
+ return -1L;
360
+ }
361
+
362
+ return (long) redisTemplate.execute((RedisCallback) connection -> {
363
+ byte[] paramBytes = redisTemplate.getStringSerializer().serialize(key);
364
+ return connection.decr(paramBytes);
365
+ }, true);
366
+ }
367
+
368
+ public static Long decreaseKey(RedisTemplate<String, Object> redisTemplate, RedisStylesRequest key) {
369
+ return decreaseKey(redisTemplate, RedisStylesUtils.takeRedisKey(key));
370
+ }
371
+
372
+ public static Long increaseKeyBy(RedisTemplate<String, Object> redisTemplate, String key, long value) {
373
+ final String preKey = key;
374
+ final long preValue = value;
375
+ return (long) redisTemplate.execute(new RedisCallback() {
376
+
377
+ public Object doInRedis(RedisConnection connection) {
378
+ byte[] paramBytes = redisTemplate.getStringSerializer().serialize(preKey);
379
+ return connection.incrBy(paramBytes, preValue);
380
+ }
381
+ }, true);
382
+ }
383
+
384
+ public static Long increaseKeyBy(RedisTemplate<String, Object> redisTemplate, RedisStylesRequest key, long value) {
385
+ return increaseKeyBy(redisTemplate, RedisStylesUtils.takeRedisKey(key), value);
386
+ }
387
+
388
+ public static Long decreaseKeyBy(RedisTemplate<String, Object> redisTemplate, String key, long value) {
389
+ final String preKey = key;
390
+ final long preValue = value;
391
+ return (long) redisTemplate.execute(new RedisCallback() {
392
+
393
+ public Object doInRedis(RedisConnection connection) {
394
+ byte[] paramBytes = redisTemplate.getStringSerializer().serialize(preKey);
395
+ return connection.decrBy(paramBytes, preValue);
396
+ }
397
+ }, true);
398
+ }
399
+
400
+ public static Long decreaseKeyBy(RedisTemplate<String, Object> redisTemplate, RedisStylesRequest key, long value) {
401
+ return decreaseKeyBy(redisTemplate, RedisStylesUtils.takeRedisKey(key), value);
402
+ }
403
+
404
+ public static Long increaseKeyEx(RedisTemplate<String, Object> redisTemplate, String key, long timeOut, TimeUnit unit) {
405
+ long value = increaseKey(redisTemplate, key);
406
+ redisTemplate.expire(key, timeOut, unit);
407
+ return value;
408
+ }
409
+
410
+ public static Long increaseKeyEx(RedisTemplate<String, Object> redisTemplate, RedisStylesRequest key, long timeOut, TimeUnit unit) {
411
+ return increaseKeyEx(redisTemplate, RedisStylesUtils.takeRedisKey(key), timeOut, unit);
412
+ }
413
+
414
+ public static Long decreaseKeyEx(RedisTemplate<String, Object> redisTemplate, String key, long timeOut, TimeUnit unit) {
415
+ long value = decreaseKey(redisTemplate, key);
416
+ redisTemplate.expire(key, timeOut, unit);
417
+ return value;
418
+ }
419
+
420
+ public static Long decreaseKeyEx(RedisTemplate<String, Object> redisTemplate, RedisStylesRequest key, long timeOut, TimeUnit unit) {
421
+ return decreaseKeyEx(redisTemplate, RedisStylesUtils.takeRedisKey(key), timeOut, unit);
422
+ }
423
+
424
+ public static Long increaseKeyByEx(RedisTemplate<String, Object> redisTemplate, String key, long value, long timeOut, TimeUnit unit) {
425
+ long keySet = increaseKeyBy(redisTemplate, key, value);
426
+ redisTemplate.expire(key, timeOut, unit);
427
+ return keySet;
428
+ }
429
+
430
+ public static Long increaseKeyByEx(RedisTemplate<String, Object> redisTemplate, RedisStylesRequest key, long value, long timeOut, TimeUnit unit) {
431
+ return increaseKeyByEx(redisTemplate, RedisStylesUtils.takeRedisKey(key), value, timeOut, unit);
432
+ }
433
+
434
+ public static Long decreaseKeyByEx(RedisTemplate<String, Object> redisTemplate, String key, long value, long timeOut, TimeUnit unit) {
435
+ long keySet = decreaseKeyBy(redisTemplate, key, value);
436
+ redisTemplate.expire(key, timeOut, unit);
437
+ return keySet;
438
+ }
439
+
440
+ public static Long decreaseKeyByEx(RedisTemplate<String, Object> redisTemplate, RedisStylesRequest key, long value, long timeOut, TimeUnit unit) {
441
+ return decreaseKeyByEx(redisTemplate, RedisStylesUtils.takeRedisKey(key), value, timeOut, unit);
442
+ }
443
+ }
444
+ ` ` `
445
+
446
+ - `RedisStylesRequest` : model to build redis key
447
+
448
+ ` ` ` java
449
+ private static void onRedisKey() {
450
+
451
+ RedisStylesRequest request = new RedisStylesRequest.RedisStylesRequestBuilder()
452
+ .onMasterKey("parent_key")
453
+ .onRedisKey("mouse_child")
454
+ .onGeolocationType(GeolocationType.VIETNAM_GEOLOCATION)
455
+ .onRedisPropsType(RedisPropsType.ObjectType)
456
+ .onRedisStylesType(RedisStylesType.USER_KEY)
457
+ .onUserKey("self")
458
+ .build();
459
+
460
+ System.out.println("redis key accomplished = " + request.asKey); // call field public to get redis key, o = parent_key:user:self:vietnam:object
461
+ System.out.println("redis key accomplished = " + RedisStylesRequest.asKeyRendered(request)); // call from utils method to get redis key, o = parent_key:user:self:vietnam:object
462
+ }
463
+ ` ` `
0 commit comments