preface
The recent “Blind Box friends” is very hot, but as a professional response, the most concerned is whether the implementation of the program is complex, and what is the difficulty.
As you can see from this picture, the interface is simple and easy to understand. Most of the elements are just pictures. It seems that it doesn’t take much effort to develop the interface. Payment and guarantee do not repeat, pay zha door first do not say, say how to ensure that the extraction function of the same person can not take the same number, always can not let people spend money, take the same person, this is too much, so it is necessary to solve.
There are many solutions, the most brutal is to obtain the user ID, each time the micro signal and user ID stored in the data table A, the next extraction, determine whether there is this corresponding record in A table, such as the following statement.
Firstly, obtain the ID of all the records drawn by U1 user, then query all wechat signals, and use not in to remove them, and then randomly pick out one from them.
SELECT * from test.tb_weichat where id not in (SELECT id from test.tb_recored where user_id ='u1')
Copy the code
For example, use the RAND and LIMIT functions to make a random entry, but remember to insert the random entry into the TB_recored table.
SELECT * from test.tb_weichat where id not in (SELECT id from test.tb_recored where user_id ='u1') order by RAND() LIMIT 1
Copy the code
What else is there to do?
Of course there is, and the way I know how to do that is with a bit array.
A bit array is a very long array, except that the contents are 0 except for 1. For example, when user U1 has obtained the wechat record with ID 4, we set the 4th bit to 1, indicating that it has been read.
With this history, the next query can also be culled using not in.
In Java, you can use BitSet, or just use Redis, originally was to take Redis demo, but accidentally found in Java there is an official class, this class can achieve the above operation, the disadvantages are also obvious, but also need to manually save, manually load, unlike Redis can be automatic persistence.
Here’s how BitSet is used
BitSet bitSet = new BitSet();
bitSet.set(4);
bitSet.set(5);
bitSet.set(6);
long[] longs = bitSet.toLongArray();
for (long aLong : longs) {
System.out.println(aLong);
}
System.out.println(bitSet);
Copy the code
ToLongArray returns an array of strange values. The value is the bit you set, and it takes the bit you set and generates a long. You can also parse back from the array, as shown in the following example.
BitSet bitSet = new BitSet();
bitSet.set(4);
bitSet.set(5);
bitSet.set(6);
long[] longs = bitSet.toLongArray();
System.out.println(BitSet.valueOf(longs).get(4));
Copy the code
As for the principle, read the source code, give me the whole won’t, because there are a lot of bit operations, not quite good to understand, but to know, using this method, can reduce a lot of space, improve speed.
However, the final results should be saved locally when the system stops, either in a database or a file. There’s also a Map in the database, key is the user ID, values is this array, and when the system starts up, it reloads that value.
During extraction, the set of ids that have been set in BitSet will also be obtained first and eliminated with not in.
Use the following method to get how many bits have been set previously.
int[] ints = bitSet.stream().toArray();
Copy the code
The overall method is the same as the database, but the speed is certainly very fast.