Using Redis’s Bitmap is very suitable to solve this problem, taking “photos” as an example
"Photo" creates a bitmap for the key. The bit with a value of 1 represents that it has been used (no need to create it, just query it directly)
Find the index of the leftmost bit with 0: bitpos "photo" 0
The index found is the smallest numerical space
Note: If there are multiple servers and multi-threads concurrently, you can consider using the distributed lock implemented by Redis, or use the watch command The pseudo code is as follows
The hash table uses the file name as the key. Since it generates an ordered list of [1..MaxNum], MaxNum takes a number that you think is reasonable. Synchronize hash tables. Every time you create a file, check the hash table to see if there is the key, and if so, use the first value of list.pop. Every time you delete a file, push the numbers in the file() back to the list The hash table controls the size. Excessive data can be stored in the file system [database, etc.]. Every time you query the hash and cannot find the data, you need to go to the file system. Find the call. Similar to the memory paging cache mechanism.
import os
def make_dir(filename,i=1):
fullname = '%s (%s)' % (filename,i)
if not os.path.exists(filename):
os.mkdir(filename)
return filename
if os.path.exists(fullname):
i += 1
return make_dir(filename,i)
else:
os.mkdir(fullname)
return fullname
for i in range(3):
print(make_dir('照片'))
Using Redis’s Bitmap is very suitable to solve this problem, taking “photos” as an example
"Photo" creates a bitmap for the key. The bit with a value of 1 represents that it has been used (no need to create it, just query it directly)
Find the index of the leftmost bit with 0: bitpos "photo" 0
The index found is the smallest numerical space
Note: If there are multiple servers and multi-threads concurrently, you can consider using the distributed lock implemented by Redis, or use the watch command
The pseudo code is as follows
The hash table uses the file name as the key. Since it generates an ordered list of [1..MaxNum], MaxNum takes a number that you think is reasonable. Synchronize hash tables.
Every time you create a file, check the hash table to see if there is the key, and if so, use the first value of list.pop.
Every time you delete a file, push the numbers in the file() back to the list
The hash table controls the size. Excessive data can be stored in the file system [database, etc.]. Every time you query the hash and cannot find the data, you need to go to the file system. Find the call. Similar to the memory paging cache mechanism.
The simplest idea is to maintain two sets of data
Pseudo code