|
@@ -1,62 +1,62 @@
|
|
|
-package com.diagbot.idc;
|
|
|
-
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
-import org.springframework.dao.DataAccessException;
|
|
|
-import org.springframework.data.redis.connection.RedisConnection;
|
|
|
-import org.springframework.data.redis.core.RedisCallback;
|
|
|
-import org.springframework.data.redis.core.RedisTemplate;
|
|
|
-
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-/**
|
|
|
- * @Description: 不可见id由redis生成
|
|
|
- * @author: gaodm
|
|
|
- * @time: 2018/9/5 9:21
|
|
|
- */
|
|
|
-@SuppressWarnings("unchecked")
|
|
|
-@Slf4j
|
|
|
-public class InvisibleIdCreater extends AbstractIdCreater<Class> {
|
|
|
-
|
|
|
- @Autowired
|
|
|
- @Qualifier("redisTemplateForIdc")
|
|
|
- RedisTemplate redisTemplate;
|
|
|
-
|
|
|
- public InvisibleIdCreater(int machineId, int dataCenterId) {
|
|
|
- super(machineId, dataCenterId);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Long getNextId(Class param) {
|
|
|
- final byte[] redis_key = redisTemplate.getKeySerializer().serialize("id_" + param.getName());
|
|
|
- Object execute = redisTemplate.execute(new RedisCallback() {
|
|
|
- @Override
|
|
|
- public Long doInRedis(RedisConnection connection) throws DataAccessException {
|
|
|
- Long incr = connection.incr(redis_key);
|
|
|
- return incr;
|
|
|
- }
|
|
|
- });
|
|
|
- return (Long) execute;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<Long> getNextIds(Class param, final int size) {
|
|
|
- final byte[] rawKey = redisTemplate.getKeySerializer().serialize("id_" + param.getName());
|
|
|
- //pipeline
|
|
|
- RedisCallback<List<Object>> pipelineCallback = new RedisCallback<List<Object>>() {
|
|
|
- @Override
|
|
|
- public List<Object> doInRedis(RedisConnection connection) throws DataAccessException {
|
|
|
- connection.openPipeline();
|
|
|
- for (int i = 0; i < size; i++) {
|
|
|
- connection.incr(rawKey);
|
|
|
- }
|
|
|
- return connection.closePipeline();
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- List<Long> results = (List<Long>) redisTemplate.execute(pipelineCallback);
|
|
|
- return results;
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+//package com.diagbot.idc;
|
|
|
+//
|
|
|
+//import lombok.extern.slf4j.Slf4j;
|
|
|
+//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+//import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+//import org.springframework.dao.DataAccessException;
|
|
|
+//import org.springframework.data.redis.connection.RedisConnection;
|
|
|
+//import org.springframework.data.redis.core.RedisCallback;
|
|
|
+//import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+//
|
|
|
+//import java.util.List;
|
|
|
+//
|
|
|
+///**
|
|
|
+// * @Description: 不可见id由redis生成
|
|
|
+// * @author: gaodm
|
|
|
+// * @time: 2018/9/5 9:21
|
|
|
+// */
|
|
|
+//@SuppressWarnings("unchecked")
|
|
|
+//@Slf4j
|
|
|
+//public class InvisibleIdCreater extends AbstractIdCreater<Class> {
|
|
|
+//
|
|
|
+// @Autowired
|
|
|
+// @Qualifier("redisTemplateForIdc")
|
|
|
+// RedisTemplate redisTemplate;
|
|
|
+//
|
|
|
+// public InvisibleIdCreater(int machineId, int dataCenterId) {
|
|
|
+// super(machineId, dataCenterId);
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public Long getNextId(Class param) {
|
|
|
+// final byte[] redis_key = redisTemplate.getKeySerializer().serialize("id_" + param.getName());
|
|
|
+// Object execute = redisTemplate.execute(new RedisCallback() {
|
|
|
+// @Override
|
|
|
+// public Long doInRedis(RedisConnection connection) throws DataAccessException {
|
|
|
+// Long incr = connection.incr(redis_key);
|
|
|
+// return incr;
|
|
|
+// }
|
|
|
+// });
|
|
|
+// return (Long) execute;
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public List<Long> getNextIds(Class param, final int size) {
|
|
|
+// final byte[] rawKey = redisTemplate.getKeySerializer().serialize("id_" + param.getName());
|
|
|
+// //pipeline
|
|
|
+// RedisCallback<List<Object>> pipelineCallback = new RedisCallback<List<Object>>() {
|
|
|
+// @Override
|
|
|
+// public List<Object> doInRedis(RedisConnection connection) throws DataAccessException {
|
|
|
+// connection.openPipeline();
|
|
|
+// for (int i = 0; i < size; i++) {
|
|
|
+// connection.incr(rawKey);
|
|
|
+// }
|
|
|
+// return connection.closePipeline();
|
|
|
+// }
|
|
|
+// };
|
|
|
+//
|
|
|
+// List<Long> results = (List<Long>) redisTemplate.execute(pipelineCallback);
|
|
|
+// return results;
|
|
|
+// }
|
|
|
+//
|
|
|
+//}
|