Commit 5bcb2200 authored by Nicholas J Anderson's avatar Nicholas J Anderson

Fix collision resolution

Previously, this would just exit if a collision was found since it would
recheck the same short url over and over again.

Additionally, it would not increment the cache counter on successful
short generation since the error would cause the rest of the stanza
to no longer execute
parent c7362984
Pipeline #1251 passed with stage
in 1 minute and 17 seconds
......@@ -134,14 +134,13 @@ class URL(models.Model):
"""
if cache.get("hashids_counter") is None:
cache.set("hashids_counter", URL.objects.count())
cache.incr("hashids_counter")
short = HASHIDS.encrypt(cache.get("hashids_counter"))
tries = 1
while tries < 100:
try:
URL.objects.get(short__iexact=short)
short = HASHIDS.encrypt(cache.get("hashids_counter"))
tries += 1
cache.incr("hashids_counter")
URL.objects.get(short__iexact=short)
except URL.DoesNotExist as ex:
print(ex)
return short
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment