Distributed generation program distributed redis id Id - redis manner

Distributed Id - redis way

 

Benpian sharing is one of the programs on Id distributed generation, in addition to redis program there such as: databases, snowflake algorithm, mogodb (object_id is the database) and other programs, for redis is our common and more contact with the therefore mainly talk about the combination of distributed generation redis id program.

  • Distributed Id Design Flow
  • Id accumulated generating ordered based on the hash automatically increment redis
  • Periodically delete useless hash column

Id distributed design flow (a little rough)

 

Id accumulated generating ordered based on the hash automatically increment redis

Id is generated using redis program, one of the main ways in which the use of increment (increment), regardless of string, hash and so has the way we manage to make it easier to generate key id column here the recommended way of using the hash, the following are based on springboot share;

Of course, the first step we need to create a hash and hkey job, as the business is first accessed to create the hash or automatically create the look and flow of business through service, hkey here is that there are certain rules (of course not confined to sex), and here I do follow key date format, you can have the following code:

Copy the code
1 / * 
 2 * generating an initial daily Id 
 . 3 * @param hashName 
 . 4 @return * 
 . 5 * / 
 . 6 initPrimaryId public String (String hashName) { 
 . 7 Assert.hasLength (hashName, "hashName not empty"); 
 . 8 
 . 9 String = LocalDate.now hashCol () the format (DateTimeFormatter.ofPattern ( "yyyyMMdd"));. 
10 // custom numbering rules 
. 11 String hashColVal hashCol + = "00001"; 
. 12 is redisTemplate.opsForHash () the putIfAbsent (hashName, hashCol, hashColVal); 
13 is return hashCol; 
14}
Copy the code

Above it is easy to understand, hash key in the date format is composed of a day, meaning all you need to generate a new key date every day, to achieve the principle of non-repetition by putIfAbsent added, as hval can customize the number of rules to generate a string of numeric characters ( NOTE: be sure to digital); with the basis of the above, we only need to accumulate increment, i.e. to help us complete Redis hval + 1 operation, the accumulated number of course can be customized, the following code:

Copy the code
 1     /**
 2      * 获取分布式Id
 3      *
 4      * @param hashName
 5      * @return
 6      */
 7     public long getPrimaryId(String hashName) {
 8         try {
 9             String hashCol = initPrimaryId(hashName);
10             return redisTemplate.opsForHash().increment(hashName, hashCol, 1);
11         } catch (Exception ex) {
12             ex.printStackTrace();
13         }
14         return 0;
15     }
Copy the code

Periodically delete useless hash column

Then we set the above every day by hash id only increase the initial value, the hash function hkey layout by automatically expire, so we need to maintain a clear code mechanism hkey, since id is generated based on the date, we can use it to move forward push n days old hkey way to achieve the clear purpose:

Copy the code
1 / * 
 2 * number of days before deleting cols 
 . 3 * @param hashName 
 . 4 * @param lessDay 
 . 5 * @return 
 . 6 * / 
 . 7 removePrimaryByLessDay public Long (String hashName, int lessDay) { 
 . 8 the try { 
 . 9 // current date 
10 LocalDate.now hashCol = String () the format (DateTimeFormatter.ofPattern ( "yyyyMMdd"));. 
. 11 Long Long.valueOf IDL = (hashCol) - lessDay; 
12 is 
13 is String [] = removeCols redisTemplate.opsForHash () entries It (hashName. ) .keyset (.) Stream (). 
14 the Map (Key -> key.toString ().) 
15 filter (Key -> IDL> Long.valueOf (Key).) // start from +1 to avoid deleting data day
16                     toArray(String[]::new);
17 
18             if (ArrayUtils.isNotEmpty(removeCols)) {
19                 return redisTemplate.opsForHash().delete(hashName, removeCols);
20             }
21         } catch (Exception ex) {
22             ex.printStackTrace();
23         }
24         return 0L;
25     }
Copy the code

By date to generate distributed id, id does not achieve the purpose of repeating, which is distributed id (repeat not), if something seems simple fact at high flow impact, much needs to be considered, such as: when to generate an initial Id, in multiple servers to ensure server time as possible under the same circumstances, the number of the reservation date hkey etc;

The above code will do the initial Id is not very good, Id get time, will create to detect and id in the business, so redis interact with on more than once, usually you can use the service to generate a one-time push back the current date n days the hkey, thus avoiding the time id get in the business, but also to verify putIfAbsent once, reducing the number of requests. It is not possible to use lua scripts in a request to do put and increment, you might use:

1             RedisScript script = new DefaultRedisScript("");
2             redisTemplate.execute(script, Arrays.asList(""));

Benpian sharing is one of the programs on Id distributed generation, in addition to redis program there such as: databases, snowflake algorithm, mogodb (object_id is the database) and other programs, for redis is our common and more contact with the therefore mainly talk about the combination of distributed generation redis id program.

  • Distributed Id Design Flow
  • Id accumulated generating ordered based on the hash automatically increment redis
  • Periodically delete useless hash column

Id distributed design flow (a little rough)

 

Id accumulated generating ordered based on the hash automatically increment redis

Id is generated using redis program, one of the main ways in which the use of increment (increment), regardless of string, hash and so has the way we manage to make it easier to generate key id column here the recommended way of using the hash, the following are based on springboot share;

Of course, the first step we need to create a hash and hkey job, as the business is first accessed to create the hash or automatically create the look and flow of business through service, hkey here is that there are certain rules (of course not confined to sex), and here I do follow key date format, you can have the following code:

Copy the code
1 / * 
 2 * generating an initial daily Id 
 . 3 * @param hashName 
 . 4 @return * 
 . 5 * / 
 . 6 initPrimaryId public String (String hashName) { 
 . 7 Assert.hasLength (hashName, "hashName not empty"); 
 . 8 
 . 9 String = LocalDate.now hashCol () the format (DateTimeFormatter.ofPattern ( "yyyyMMdd"));. 
10 // custom numbering rules 
. 11 String hashColVal hashCol + = "00001"; 
. 12 is redisTemplate.opsForHash () the putIfAbsent (hashName, hashCol, hashColVal); 
13 is return hashCol; 
14}
Copy the code

Above it is easy to understand, hash key in the date format is composed of a day, meaning all you need to generate a new key date every day, to achieve the principle of non-repetition by putIfAbsent added, as hval can customize the number of rules to generate a string of numeric characters ( NOTE: be sure to digital); with the basis of the above, we only need to accumulate increment, i.e. to help us complete Redis hval + 1 operation, the accumulated number of course can be customized, the following code:

Copy the code
 1     /**
 2      * 获取分布式Id
 3      *
 4      * @param hashName
 5      * @return
 6      */
 7     public long getPrimaryId(String hashName) {
 8         try {
 9             String hashCol = initPrimaryId(hashName);
10             return redisTemplate.opsForHash().increment(hashName, hashCol, 1);
11         } catch (Exception ex) {
12             ex.printStackTrace();
13         }
14         return 0;
15     }
Copy the code

Periodically delete useless hash column

Then we set the above every day by hash id only increase the initial value, the hash function hkey layout by automatically expire, so we need to maintain a clear code mechanism hkey, since id is generated based on the date, we can use it to move forward push n days old hkey way to achieve the clear purpose:

Copy the code
1 / * 
 2 * number of days before deleting cols 
 . 3 * @param hashName 
 . 4 * @param lessDay 
 . 5 * @return 
 . 6 * / 
 . 7 removePrimaryByLessDay public Long (String hashName, int lessDay) { 
 . 8 the try { 
 . 9 // current date 
10 LocalDate.now hashCol = String () the format (DateTimeFormatter.ofPattern ( "yyyyMMdd"));. 
. 11 Long Long.valueOf IDL = (hashCol) - lessDay; 
12 is 
13 is String [] = removeCols redisTemplate.opsForHash () entries It (hashName. ) .keyset (.) Stream (). 
14 the Map (Key -> key.toString ().) 
15 filter (Key -> IDL> Long.valueOf (Key).) // start from +1 to avoid deleting data day
16                     toArray(String[]::new);
17 
18             if (ArrayUtils.isNotEmpty(removeCols)) {
19                 return redisTemplate.opsForHash().delete(hashName, removeCols);
20             }
21         } catch (Exception ex) {
22             ex.printStackTrace();
23         }
24         return 0L;
25     }
Copy the code

By date to generate distributed id, id does not achieve the purpose of repeating, which is distributed id (repeat not), if something seems simple fact at high flow impact, much needs to be considered, such as: when to generate an initial Id, in multiple servers to ensure server time as possible under the same circumstances, the number of the reservation date hkey etc;

The above code will do the initial Id is not very good, Id get time, will create to detect and id in the business, so redis interact with on more than once, usually you can use the service to generate a one-time push back the current date n days the hkey, thus avoiding the time id get in the business, but also to verify putIfAbsent once, reducing the number of requests. It is not possible to use lua scripts in a request to do put and increment, you might use:

1             RedisScript script = new DefaultRedisScript("");
2             redisTemplate.execute(script, Arrays.asList(""));

Guess you like

Origin www.cnblogs.com/Leo_wl/p/11834427.html